Legacy GM [SOLVED] Problem with grid and clearing cells, it won't work

Pretorg

Member
Hello! after a long abscense i am back trying to finish my first projec, it is a 2d topdown game set in space and one of the enemies is supposed to be able to navigate towards the player without hitting anything in the way (pathfinding) because both the player and the enemy ships get damaged if they collide.

It wasn't perfect sometimes the enemy ships collide anyway but that's probably because of it's size compared to the grid and those obstacles, anyway i could live with that.

The problem is that the player can destroy those obstacles and when i use the mp_grid_clear_cell function nothing happens, the AI still detects the cells as occupied.

I have been searching but couldn't find anything here nor reddit about this issue.

This is the code i have been using in the destroy event:
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix+32 / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy+32 /32));
mp_grid_clear_cell(global.grid, floor(mix+32 / 32), floor(miy+32 /32));​
  • I have tried:
  • several things one of those being mp_grid_clear_all and that works, at this point i tought that the problem had something to do with the coordinates i was using that's the reason for mix and miy, they are variables i made to get an updated x and y coordinates.

  • I even tought that it might have something to do with the project itself being buggy it has happened to me before so i installed game maker on a different pc and started a new project to test this with nothing but the neccesary objects and still the same problem.

  • But after some more testing i found out that if i create the grid on the step event (for this i have a grid object so every enemy can access it) then and only then the mp_grid_clear_cell works, but i think i remember something about that being a way to have a memory leak or a performance drop as it takes too much resources, and i have seen strategy games made with game maker, correct me if i am wrong.

So is there any way to clear those cells without having to create the grid multiple times?
Is there something i am not seeing? afterall the obstacles in the grid get cleared or "updated" with the clear_all function.

Apologies for any badly written parts, english is not my mother tongue :)
 

NightFrost

Member
You say that clear_all works but clear_cell does not, which suggests that your enemies are reading the correct grid, but either you are getting the coordinates wrong or some process immediately sets the cells back as occupied. Seeing you use separate variables (mix, miy) to get a grid position also suggests your coordinate systems don't have the same zero position or scale, and mistakes are creeping in during the translation.
 

Pretorg

Member
You say that clear_all works but clear_cell does not, which suggests that your enemies are reading the correct grid, but either you are getting the coordinates wrong or some process immediately sets the cells back as occupied.
That is what i also tought at first and the reason to have mix and miy to make sure that i am getting the x and y if somehow they were being change after the instances were created by the room, because previously i wasn't using separate variables, trusting that the clear_cell function would get the right coordinates, it's not even a moving object so i shouldn't have to update it's position... but no matter what i store the x and y in the create event as a separate variable, or store update them trough the step event and then store them or just use the x and y by default works, that made me think that something else was causing the error and so i started a new projet on a clean install with just the grid, the enemy, and the objects it should check for.
Thank you for the quick answer
 

Pretorg

Member
Ok, so i just tried something else, instead of having in the grid object the "obstacle" added trough mp_grid_add_instances i did it manually on the obstacle's create event:
mp_grid_add_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_add_cell(global.grid, floor(mix+32 / 32), floor(miy /32));
mp_grid_add_cell(global.grid, floor(mix / 32), floor(miy+32 /32));
mp_grid_add_cell(global.grid, floor(mix+32 / 32), floor(miy+32 /32));
And now it works, but i still don't understand what is going on, if it has somehitng to do with the coordinates or not, using mp_grid_add_instances seems to be getting the right coordinates the occupied cells are there, somehow it is rewriting that information to the grid when i try to clear them and that's the reason i can't do it?
This is the grid create event:
var cell_width = 32;
var cell_height = 32;

var hcells = room_width div cell_width;
var vcells = room_height div cell_height;

global.grid = mp_grid_create(0,0,hcells,vcells,cell_height,cell_width);
//mp_grid_add_instances(global.grid,obj_obstaculo,false);
mp_grid_add_instances is right now commented for that test.
 

TheouAegis

Member
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix+32 / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy+32 /32));
mp_grid_clear_cell(global.grid, floor(mix+32 / 32), floor(miy+32 /32));
I think I found your problem. SOME of the cells are getting cleared, but not all of them. The ones that aren't getting cleared are because you have a syntax error in your code. As they should still be teaching you in school, * and / have higher operational priority than + and -. So mix+32/32 is the same as mix+1 and miy+32/32 is the same as miy+1. You need to use parentheses around the + operations, or replace /32 with >>5 (but in terms of general programming practice, the parentheses is the better way to go).
Code:
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix / 32), floor((miy+32) /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor((miy+32) /32));
 

Pretorg

Member
I think I found your problem. SOME of the cells are getting cleared, but not all of them. The ones that aren't getting cleared are because you have a syntax error in your code. As they should still be teaching you in school, * and / have higher operational priority than + and -. So mix+32/32 is the same as mix+1 and miy+32/32 is the same as miy+1. You need to use parentheses around the + operations, or replace /32 with >>5 (but in terms of general programming practice, the parentheses is the better way to go).
Code:
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix / 32), floor((miy+32) /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor((miy+32) /32));
I am sorry for not saying anything about it on the first post but i have been trying so many different things that i completly forgot, let me fix that here:
  • One of the first things i tried was that (also using div instead of / and floor and some different variations of parentheses), using what many years ago i was taught in school :) and place all the parentheses where they should be, and the funny thing is that in my game i think the code still has those parentheses, but when i created the new project which is the one i am using to test the grid i just copied the example code from the game maker documenation and added the summs, a mistake on my part indeed, but just in case i tested it again with the right syntax and doesn't work.
  • It is also worth mentioning that i made a "box" of obstacles around the enemy both in the game project and the new one, and when i destroy enough of them as you said some but just some of the cells are cleared, that is one of the reasons i tried to get the coordinates with separate variables, and also change the grid's instance creation order inside the room.
 
Last edited:

Pretorg

Member
You say that clear_all works but clear_cell does not, which suggests that your enemies are reading the correct grid, but either you are getting the coordinates wrong or some process immediately sets the cells back as occupied. Seeing you use separate variables (mix, miy) to get a grid position also suggests your coordinate systems don't have the same zero position or scale, and mistakes are creeping in during the translation.
I think I found your problem. SOME of the cells are getting cleared, but not all of them. The ones that aren't getting cleared are because you have a syntax error in your code. As they should still be teaching you in school, * and / have higher operational priority than + and -. So mix+32/32 is the same as mix+1 and miy+32/32 is the same as miy+1. You need to use parentheses around the + operations, or replace /32 with >>5 (but in terms of general programming practice, the parentheses is the better way to go).
Code:
mp_grid_clear_cell(global.grid, floor(mix / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor(miy /32));
mp_grid_clear_cell(global.grid, floor(mix / 32), floor((miy+32) /32));
mp_grid_clear_cell(global.grid, floor((mix+32) / 32), floor((miy+32) /32));
I finally got it to work, if only i had used the mp_grid_draw function before :oops: turns out everything was off by one cell starting from the bottom right corner (my object occupies 4 cells) for some reason i tought the grid started at 1,1 so the calculations made sense to me, now with this code everything works fine:
mp_grid_clear_cell(global.grid, floor((x-32) / 32), floor((y-32) / 32)); //esq sup izq
mp_grid_clear_cell(global.grid, floor(x / 32), floor((y-32) / 32)); //esq sup der
mp_grid_clear_cell(global.grid, floor((x-32) / 32), floor(y / 32)); //esq inf izq
mp_grid_clear_cell(global.grid, floor(x / 32), floor(y / 32)); //esq inf der
I also saw what happens without the parenthesees on the right places, only the bottom right cell it's added.
Thank you both for your help.
 
Top