• 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 [SOLVED] How do I fill a room with objects like how MP_Grid creates a grid?

Dr_Nomz

Member
You know how MP_Grid creates a grid across the entire room?

mp_grid_create(0,0,room_width/50,room_height/50,50,50);

How do I do that, but for an object? I'd like to place an object every 50 pixels in my room sort of creating a grid of objects.
 

kburkhart84

Firehammer Games
I'm not sure where you got the idea of mp_grid functions creating objects. It is for motion planning, not object creation. Just because it has a draw function doesn't mean it creates objects...it draws based off of the motion planning grid that gets created.

What you want is simply a combination of a couple for() loops and instance_create()/instance_create_depth()/instance_create_layer().

Code:
for(var i = 0; i <= 500; i += 50)
{
    for(var j = 0; j <= 500; j += 50)
    {
        instance_create_depth(i, j, 0, objSomeObj);
    }
}
That code loops the variables i and j, starting at 0 and incrementing 50 each round. It then uses i and j to position the new objects. If your room is taller and wider, you can change the max values. If you wanted the objects at larger or smaller distances from each other, you can change the increment.

If you are new to coding, I HIGHLY recommend you learn and really understand that little bit of code. This method will save you all over the place.
 

Dr_Nomz

Member
Yeah I know it's just used for motion planning, I just used it as the closest example I could think of.

That for loop works great, thanks for the help. :D (i = x and j = y for anyone wondering)
 
Top