• 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!

GameMaker Place array of instances?

M

Marcus12321

Guest
Can I place instances of objects into a room and have them be an array?

In other words, I want to place 5 objects in a room, and I want them to be obj[0], obj[1], obj[2], etc... when I am coding.

Is this possible without having to write some code? I can't change the name of an instance to obj[0], I tried.

I see that when I place an object in a room, there is a place for instance creation code, and I guess I could just have each instance assign itself to its variable in the array. But I was hoping there was a better way of doing it.

I could just have one script at startup find the instances and assign all of the instances to an array, but then you get into the whole race condition thing with which instances haven't been created yet... So the code couldn't be in a create event, it would have to be in an event that runs after all of the instances have been created.

Is there an event that runs after all of the instances have been created, like a room start event or something?

Thanks for any help. I am still wrapping my head around some of the finer details of GMS2.
 
M

Marcus12321

Guest
I see that the room has an instance creation order list. I guess I could have the very last instance do all of the work, and I would just have to make sure which instance was last.

Edit - The room creation code happens AFTER all of the instances have been created. I think this is my answer...

Sorry to have wasted your time... ;)
 
M

Marcus12321

Guest
Hmmm, still not quite sure I have this right...

I created 8 instances of my stack object called iStack0, iStack1, iStack2... iStack7.

Then in my room creation code I am trying to find those instances and I am trying to assign them to a variable stacks[0] - stacks[7]. But it seems I can only find instances of objects, but I have no way of telling them apart from each other.

Is there no way progmatically to tell them apart, without assigning a variable to them in the room editor that I can read? Is there no way to get an Instances name? I can get an instance id and an object name, but that does not tell me which of the instances in the room it is.

Thanks for any help...
 
M

Marcus12321

Guest
Whoa... The room create event can access those objects by name, as though the names were variables... That solves my problem, and actually makes it easier than I was thinking it would be.
 
Last edited by a moderator:

TheouAegis

Member
FYI there is no race with instances placed in the room; The instances exist from the very start before any events even run, but their variables don't exist. And since the Instance Order tab dictates in what order those instances will run their events, it's really more of a pole position lineup than a race.
 
M

Marcus12321

Guest
FYI there is no race with instances placed in the room; The instances exist from the very start before any events even run, but their variables don't exist. And since the Instance Order tab dictates in what order those instances will run their events, it's really more of a pole position lineup than a race.
Thanks, as I explored this issue today I realized that. I had no idea there was a list so you could control the order they were created.

I also didn't know about the room creation code. All of these things have made my life easier. I had no idea they existed.
 
M

Marcus12321

Guest
Actually, it looks like the names of those instances are global through the room and all of its objects... Very interesting. This just gets easier and easier.

Now if I could just make the instances I put in the room be an array, then I wouldn't have to do anything.

But, adding them to an array in the room create code is easy enough.
 

TheouAegis

Member
Well, there is a little trick you could potentially pull off. It's frowned upon, since it's not actually documented behavior.

Put them all in the room. Sort them in order by name/number in the Instance Order list. Click OK to close the list. Make sure there are no instances between them. Now all the instances can be referenced by iStack0+n where n is whatever variable you'd use to look through the intended array.
 
Top