Legacy GM Tile checking error

Fixer90

Member
So I have this code in the Room Start event, and the object is persistent:
Code:
var walls = tile_get_ids_at_depth(999990);

for(var w = array_length_1d(walls), i = 0; i < w; i ++)
{
    instance_create(tile_get_x(walls[i]), tile_get_y(walls[i]), obj_collision);
}
What this does is makes it to where every tile in every room that has a depth of 999990 gets its own collision object. This works perfectly... but if I have a room with no tiles at that depth, the game crashes and gives me an error message. I tried putting an if check before the for check:
Code:
if(array_length_1d(walls) >= 1)
but even this didn't work. The same error message pops up: "Tile does not exist. Yada-yada-yada, instance_create(tilet_get_x(walls), tile_get_y(walls), obj_collision);"

TL;DR: The code works great, but if I don't have tiles on that depth, it crashes the game. How do I make it work even if there are no tiles on that depth?
 

Simon Gust

Member
Try this
Code:
var walls = tile_get_ids_at_depth(999990);

for(var w = array_length_1d(walls), i = 0; i < w; i ++)
{
   var tile = walls[i];
   if (tile == -1) break;
   instance_create(tile_get_x(tile), tile_get_y(tile), obj_collision);
}
 

Fixer90

Member
Try this
Code:
var walls = tile_get_ids_at_depth(999990);

for(var w = array_length_1d(walls), i = 0; i < w; i ++)
{
   var tile = walls[i];
   if (tile == -1) break;
   instance_create(tile_get_x(tile), tile_get_y(tile), obj_collision);
}
Same error came up.
 

Simon Gust

Member
just tried, you are right.
For me when I display the whole array, its just "0" which is stupid because if the depth would exist, there is always a tile with the index 0.
Which means that the size of the array is exactly 1. So instead of checking if the size is greater or equal than 1, only check if its greater than 1.
 

TheouAegis

Member
If you're just flooding the room with objects, why even use tiles at all? Why not just make multiple objects taht use their own sprites and make all but one of the objects children of that other object, then use those objects just like you would the tiles? The game would run a lot faster than what you're doing now...
 

Fixer90

Member
If you're just flooding the room with objects, why even use tiles at all? Why not just make multiple objects taht use their own sprites and make all but one of the objects children of that other object, then use those objects just like you would the tiles? The game would run a lot faster than what you're doing now...
You don't know my game well enough to know that wouldn't work. And it's not that I'm just trying to fix this specific chunk of code, but by fixing this I'll know how to fix other problems similar to this in my game's code.
 
Top