• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML How to set an object to the array?

E

Edwin

Guest
Hello.

I want to set all objects in room to the array. How to do it?
vegetables_count.png

And also, maybe whether it is possible to make so that one object did not repeat in an array? I want to make something like this on every vegetable in room, so player can pick it up.
vegetables_interface.png

I don't want to make this on Draw Event on vegetable object because I want it to be something other like an interface.

Pixel Art created by Vicente Nitti (@vnitti)
 

Relic

Member
WHEN do you want the vegetables to be added to the array? At the start of the room?

WHERE is the array stored? In some controller object?

If the answer is "yes" to my questions, then in the create event of the vegetables, add it to the array stored in the controller object.

CREATE EVENT
Code:
var i = array_length_1d(obj_controller.array)

obj_controller.array[i]=id
I have also assumed that when you said
I want to set all objects in room to the array
you meant "I want to set all instances in room to the array". If I've been an ass in this assumption then swap "id" in the code above with "object_index" - but then you still have to deal with repeat entries in the array.
 
B

Bayesian

Guest
If you need to update the array at some point, after clearing it you can do this in the controller object
Code:
with(oVegParrent){
    var i = array_length_1d(other.array)
    other.array[i]=id
}
 
Top