GML Area Spawning

Hey everyone!

So I'm working on our third GM game and needed a problem solved. Not sure if I did it right but it works for my purposes.

We wanted destructable ground tiles but placing 8x8 px tiles all over the place was a pain. I build this simple object spawner. You place a ground marker in the room and scale as desired. The code spawns the ground objects and stops when the area is full.


Area Spawner Download

Area Spawner Guidelines
-Your room grid should match your ground tile. This prevents you from scaling in a way that cuts a tile in half. Not sure the code would work if that happened.
-Just set the height and width of your ground object in the ground marker. It should do the rest.

Let me know if you fins any bugs. As I said, we are still only stratching the surface of GM I'm sure. I'm also fairly certain there is a more elegant way of doing this. :)
 
Last edited:

Joe Ellis

Member
What's the problem?
I was gonna say that the ground tiles should be dealt with with an array if they're using collision and each one is destroyable. But I'm not sure if you're making actual gm tiles from a larger scaled rectangle(or the marker thing) But if each tile is an instance, I think you should def use an array instead
 
Huh, never used an array. I'll have to give it a look. Thanks!

But to your point the marker rectangles are placed and scaled so you can draw where you want the ground. The code then spawn the ground to fill the area of the marker.

Can an array length be automatically determined by an instance's scale?
 
Last edited:

Joe Ellis

Member
There would just be one array for the area of the whole level, like a grid, with either empty or filled cells. And you'd have to have a surface for the drawing of the square, or use tiles. Then when a square is destroyed you'd update the array setting the cell it fills to empty, and update the surface, erasing the destroyed squares.
It might be a bit complicated or confusing to set up and get working, especially with collision, but it might be worth it if performance is an issue, and\or you don't want hundreds of instances for each square
 
Okay, that makes sense but it does sound much more complicated. In the current state, spawning a full screen of blocks, only drops the avg fps by about 10 and a level will never have that many blocks.

Thanks for the tip on arrays though! I'm sure it will come in handy!
 
M

maxdax5

Guest
I feel like if you are going to make a couple "maps"/"levels" the best way for you would be to create a "map maker" or "level designer". (call it how you want). In this mode, you could set a couple easy lines like this
Code:
//Look for the position of the mouse and if "click" place a tile and save it.
if (mouse_check_button(mb_left)){
    if (position_empty(mouse_x,mouse_y)){
        mx = mouse_x div 5;//Will lock on a grid for every 5.
        my = mouse_y div 5;
        instance_create(mx,my,asset_get_index(selected_object));
        ini_open(whatever);
        //You can either use map name as the file name or as the section name.
        total_object = ini_read_real('map_name','total_object',0);
        ini_write_real('map_name','total_object',total_object+1);
        ini_write_real('map_name','mx',mx);
        ini_write_real('map_name','my',my);
        ini_write_string('map_name','object',selected_object);
        ini_close();
    }
}
That way you will be able to hold your click and place blocks very quickly.
To load you map, you will have to execute a script in the creation code of a room. Like "scr_map_napalm();" and in this script you look for the total object and make a loop to create them all.
 
Top