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

Legacy GM Random generation on a tile

J

James7285

Guest
I wanted to generate some items onto my level but I want them to snap to a specific grid. I tried the code below but nothing appears when you start the game (there are no errors either).

gridx[0] = 96
gridx[1] = 160
gridx[2] = 224
gridx[3] = 288
gridx[4] = 352
gridx[5] = 416
gridy[0] = 96
gridy[1] = 160
gridy[2] = 224
gridy[3] = 288
gridy[4] = 352
gridy[5] = 416
while (instance_number(obj_energy) < 4)
{
randx = irandom(5)
randy = irandom(5)
instance_create(gridx[randx],gridy[randy],obj_energy)
}

My only other thought with that is that I suppose it's possible for 2 of the same item to appear on top of each other. Is it easy to check that first?

Thanks for your time.

James
 

jazzzar

Member
how much of obj_energy do you have at the start of the level, i mean did you put any in the room editor?
 
J

James7285

Guest
I'm such an idiot!! Sorry, I completely forgot to put one in the room! Thank you, that's solved it!

While I have the thread though, what would be the easiest way to add in a check to see if one already exists and move it if it does. I just tried changing it all to this:

gridx[0] = 96
gridx[1] = 160
gridx[2] = 224
gridx[3] = 288
gridx[4] = 352
gridx[5] = 416
gridy[0] = 96
gridy[1] = 160
gridy[2] = 224
gridy[3] = 288
gridy[4] = 352
gridy[5] = 416
while (instance_number(obj_energy) < 4)
{
randx = irandom(5)
randy = irandom(5)
with(instance_create(gridx[randx],gridy[randy],obj_energy))
{
while (!place_empty(x, y))
{
x = gridx[randx];
y = gridy[randy];
}
}
}

But I get an error saying gridx not set.
 
Top