GML Game Starts Lagging Greatly

I have been using GMS2 for about a year now, and I have come across a problem I cannot seem to fix. My game starts lagging a lot after about 10 seconds after running the game, and I think that the problem is the grid around the blocks being drawn every frame, but idk. Thanks!
Video:

Grid drawing code:
Code:
//---------Create Grid
//---Up
if !place_meeting(x, y - 16, oWall) {
    instance_create_depth(x, y - 16, 0, oGrid);
}

//---Down
if !place_meeting(x, y + 16, oWall) {
    instance_create_depth(x, y + 16, 0, oGrid);   
}

//---Left
if !place_meeting(x - 16, y, oWall) {
    instance_create_depth(x - 16, y, 0, oGrid);   
}

//---Right
if !place_meeting(x + 16, y, oWall) {
    instance_create_depth(x + 16, y, 0, oGrid);
}
 

rIKmAN

Member
I have been using GMS2 for about a year now, and I have come across a problem I cannot seem to fix. My game starts lagging a lot after about 10 seconds after running the game, and I think that the problem is the grid around the blocks being drawn every frame, but idk. Thanks!
Video:

Grid drawing code:
Code:
//---------Create Grid
//---Up
if !place_meeting(x, y - 16, oWall) {
    instance_create_depth(x, y - 16, 0, oGrid);
}

//---Down
if !place_meeting(x, y + 16, oWall) {
    instance_create_depth(x, y + 16, 0, oGrid);
}

//---Left
if !place_meeting(x - 16, y, oWall) {
    instance_create_depth(x - 16, y, 0, oGrid);
}

//---Right
if !place_meeting(x + 16, y, oWall) {
    instance_create_depth(x + 16, y, 0, oGrid);
}
That code isn't drawing your grids, it's creating upto 4 new grids every step (depending on the place_meeting() result) which is why your game starts to lag - there will be hundreds / thousands of grid objects.

I would assume you want to create one single oGrid object and then have that draw using code in the Draw Event / DrawGUI Event.
 
That code isn't drawing your grids, it's creating upto 4 new grids every step (depending on the place_meeting() result) which is why your game starts to lag - there will be hundreds / thousands of grid objects.

I would assume you want to create one single oGrid object and then have that draw using code in the Draw Event / DrawGUI Event.
So you mean like to do things things in the draw event and not the step? I'm not REALLY grasping what your saying.

I say I've been using it for a year, but I haven't done too much. More just starting projects and not finishing them (That's changing recently)
 

samspade

Member
If oGrid is not a child of oWall, and the code you posted is in any type of event which runs every frame, you are creating hundreds, or possibly thousands, tens of thousands, of oGrid objects. That is what rIKmAN is saying. Based on the video that seems to be the case. You could verify this pretty easily by running the debugger either in real time or pausing it and checking how many instances of oGrid there are.
 
If oGrid is not a child of oWall, and the code you posted is in any type of event which runs every frame, you are creating hundreds, or possibly thousands, tens of thousands, of oGrid objects. That is what rIKmAN is saying. Based on the video that seems to be the case. You could verify this pretty easily by running the debugger either in real time or pausing it and checking how many instances of oGrid there are.
Alright, that makes sense. I just tested to see how many were on the screen after 10 seconds, and its about 300k. Any good idea on how to fix this?
 

rIKmAN

Member
So you mean like to do things things in the draw event and not the step? I'm not REALLY grasping what your saying.

I say I've been using it for a year, but I haven't done too much. More just starting projects and not finishing them (That's changing recently)
You have an oGrid object, which I assume draws your grid in one of it's Draw Events?
You only need to create a single instance of that oGrid object (drag it into the room or create it with code) and the grid will draw according to the code that you write for that object.

The code you wrote above I assume is in a Draw Event (as you think it draws your grids) but also has "Create Grid" as a comment so maybe it's in a Create Event but given the symptoms of lag I doubt it.
Either way, you are creating a new oGrid object every time one of those place_meeting() calls is true, and as you aren't assigning the returned instance_id to any variable you are also losing the ability to do anything with them - including delete them which is a memory leak.

Let's say only 2 of those collisions are true each frame and you are running at 60fps, that means that 120 oGrid objects are being created every second.
After 10 seconds you have 1200 oGrid objects... you can see where this is going.

Put the following code into a Draw event of an object that is not oGrid but is in the room and see what number it reports.
Adjust the x/y position if needed so you can see it.

Code:
draw_text(5, 5, "Number of oGrids: " + string(instance_number(oGrid)))
 
You have an oGrid object, which I assume draws your grid in one of it's Draw Events?
You only need to create a single instance of that oGrid object (drag it into the room or create it with code) and the grid will draw according to the code that you write for that object.

The code you wrote above I assume is in a Draw Event (as you think it draws your grids) but also has "Create Grid" as a comment so maybe it's in a Create Event but given the symptoms of lag I doubt it.
Either way, you are creating a new oGrid object every time one of those place_meeting() calls is true, and as you aren't assigning the returned instance_id to any variable you are also losing the ability to do anything with them - including delete them which is a memory leak.

Let's say only 2 of those collisions are true each frame and you are running at 60fps, that means that 120 oGrid objects are being created every second.
After 10 seconds you have 1200 oGrid objects... you can see where this is going.

Put the following code into a Draw event of an object that is not oGrid but is in the room and see what number it reports.
Adjust the x/y position if needed so you can see it.

Code:
draw_text(5, 5, "Number of oGrids: " + string(instance_number(oGrid)))
The oGrid is in a step event.
 
No, both draw and step would have the same issue. The problem is that you are constantly creating more and more oGrid objects. rIKmAN just meant that your going about things incorrectly, not that you should move that code to the draw event. Essentially you should only be running the instance_create object once, not every step. A simple thing that would probably work for you would be to just first check if that oGrid oGrid object exists first, and then if not add it.

if( !place_meeting(x, y - 16, oWall) && !place_meeting(x, y - 16, oGrid) ){
instance_create_depth(x, y - 16, 0, oGrid);
}

you would need to add that second !place_meeting for each of the chunks. But, in general I get the feeling your going about this the wrong way. I'm not sure exactly what your intentions are, but generally speaking you would store a ds_grid that holds all the actual placements of the walls, then either in the draw event you loop through the grid and draw the walls with draw_sprite or you loop through the grid once and generate wall objects correlating to the grid your looping through.
 
No, both draw and step would have the same issue. The problem is that you are constantly creating more and more oGrid objects. rIKmAN just meant that your going about things incorrectly, not that you should move that code to the draw event. Essentially you should only be running the instance_create object once, not every step. A simple thing that would probably work for you would be to just first check if that oWall oGrid object exists first, and then if not add it.

if( !place_meeting(x, y - 16, oWall) && !place_meeting(x, y - 16, oGrid) ){
instance_create_depth(x, y - 16, 0, oGrid);
}

you would need to add that second !place_meeting for each of the chunks. But, in general I get the feeling your going about this the wrong way. I'm not sure exactly what your intentions are, but generally speaking you would store a ds_grid that holds all the actual placements of the walls, then either in the draw event you loop through the grid and draw the walls with draw_sprite or you loop through the grid once and generate walls correlating to the grid your looping through.
Thank you robproctor83 and rlKmAN for your help! It is no longer lagging!
 

rIKmAN

Member
The oGrid is in a step event.
I assumed a Draw Event as you said it was drawing your grids - you can't draw anything in a Step Event - but it's an event that runs every frame so the effect is the same as rob said.

Edit: Cool, glad you sorted it.
 
Top