M
meme
Guest
Hey guys so I have this code right here:
playerGridX,Y is the player's position snapped on a grid. playerPrevious is just the previous grid location of the player. So I this code is used for optimization, its kinda complicated to explain how it works, but this "gridOptimizer" is the one that has a problem. "gridOptimizer" is a 2d array that will be updated when player's grid position changed (e.g. player move to other grid). It will replace a 3x3 square in this grid to be -1 for previous position(playerPrevious) and then create replace 3x3 square by adding 1 for current position (playerGridX, playerGridY). At the end of everything, a loop will check through each value of gridOptimizer, and if -1 then deactivate that grid, if 1 then activate.
PROBLEM: The "gridOptimizer" is a 2d array and "+= 1" does not execute properly, BUT if I do this:
It works properly and as expected...Yea the only difference is the "deb_grid" which is basically print a 2d array beautifully:
I was stuck with this situation for almost 2 hours now. If you have any suggestion feel free to leave a reply!
GML:
var _yLength = LevelManager.gridY;
var _xLength = LevelManager.gridX;
// Indices
// _horIndexMin, _horIndexMax, _verIndexMin, _verIndexMax
var _indexPrevious = [max(playerPrevious[0] - 1, 0), min(playerPrevious[0] + 1, _xLength-1),
max(playerPrevious[1] - 1, 0), min(playerPrevious[1] + 1, _yLength-1)];
var _indexCurrent = [max(playerGridX - 1, 0), min(playerGridX + 1, _xLength-1),
max(playerGridY - 1, 0), min(playerGridY + 1, _yLength-1)];
for (var i = _indexPrevious[2];i <= _indexPrevious[3];i++)
{
for (var j = _indexPrevious[0];j <= _indexPrevious[1];j++)
{
gridOptimizer[i][j] = -1;
}
}
// Set the grid optimizer using CURRENT POSITION
for (var i = _indexCurrent[2];i <= _indexCurrent[3];i++)
{
for (var j = _indexCurrent[0];j <= _indexCurrent[1];j++)
{
gridOptimizer[i][j] += 1;
}
}
PROBLEM: The "gridOptimizer" is a 2d array and "+= 1" does not execute properly, BUT if I do this:
GML:
// Set the grid optimizer using CURRENT POSITION
for (var i = _indexCurrent[2];i <= _indexCurrent[3];i++)
{
for (var j = _indexCurrent[0];j <= _indexCurrent[1];j++)
{
gridOptimizer[i][j] += 1;
}
deb_grid(gridOptimizer); // Just to print 2d grid beautifully
}
It works properly and as expected...Yea the only difference is the "deb_grid" which is basically print a 2d array beautifully:
GML:
function deb_grid(_grid){
for (var i = 0;i < array_length(_grid);i++)
show_debug_message(_grid[i]);
}
I was stuck with this situation for almost 2 hours now. If you have any suggestion feel free to leave a reply!
Last edited by a moderator: