0 votes

I have recently created a 2d chest and would like to know how i can put objects in it that you can take out. The object will not be a random occurrence, but if you could I would also like to know how to make random objects appear in chests.

in Engine by (308 points)

1 Answer

0 votes

you can make a pool of objects you want inside chests and put them inside an array
lets say:

var chestitems_array = [item1,item2,etc]

than you randomize the array and pick the 1st item on it, as an example

func _random_item():
     return chestitem_array[0]

you can call that function with another var

var the_item = _random_item()

now for the chest to be usable just add an area to it and detect player entering and give him the item, or some key input or click.

If you want multiple slots on the chest you can just run this function for each slot or have another array with the slots and iterate thru the items array

by (371 points)
edited by

This isn't a simple chest that you walk up to and get given the object. You have to click on the chest which should bring you to the chest's own inventory. I just don't know how to do that.

you can show the items inside the array to the player on click

without more details, ill assume the chest has 6 slots and you have 100 random items that could be there. you can just run that same function to each slot or make those 6 slots another array and iterate with the items array

In the specific chest I am talking about, there are no random items in there. Just a key.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.