Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

[Solved] Tile layers break on Room Reload

A topic by cookiedraggy created Nov 22, 2020 Views: 319 Replies: 3
Viewing posts 1 to 3
(1 edit)

I have the following setup:

  • Room reloading is setup as per manual, and is working. If I paint tiles, it reflects in the running game without any issues.
  • I created a function "create_collisions" which creates obj_collision instances for each solid tile.
  • The function gets called in the create event of obj_make_collision
  • I have placed one instance in the actual room and it does it's job without issues when I launch the game.
  • After a few seconds the room reloads (even if I didn't make any changes) and all solid instances disappear.
  • I tried to fix the problem by calling the function also in the room start event, but it didn't work.
  • To further investigate, I put the function call into the Begin Step event, but it would not create any collision objects.
  • I investigated further using show_debug_message and found the following:
    • After reloading layer_get_id gives back a different value.
    • layer_tilemap_get_id switches to giving back -1 upon the refresh, which means after reload I cannot get access to the the tilemap data anymore.

Here's my function for reference:

function create_collisions()
{
    var _layer = layer_get_id("Tiles_1");
    var _tileLayer = layer_tilemap_get_id(_layer);
    
    show_debug_message(_layer);

    var wt = tilemap_get_width(_tileLayer);
    var ht = tilemap_get_height(_tileLayer);

    for(var i = 0; i < wt; i++)
    {
        for(var j = 0; j < ht; j++)
        {
            var tile = tilemap_get(_tileLayer, i, j);
        
            if(tile == 1)
            {
                instance_create_layer(i*32, j*32, "Instances", obj_collision);
            }
        }
    }
}

I'd be happy to send a small test project.

If I just run "show_debug_message(layer_tilemap_get_id("Tiles_1"));" in the step event of an object it outputs an ID "4" at first and after the reload it switches to -1. But if I enumrate through all layer elements with layer_get_all_elements for Tiles_1 it tells me there is still a tile layer.

Developer

As per GM manual, layer_get_tilemap does not work for anything created at runtime, which includes GMLive. I was at one point told that this will be addressed in 2.3, but that has not happened just yet.

A workaround would be like:

function layer_tilemap_get_id_fixed(_layer) {
    var els = layer_get_all_elements(_layer);
    var n = array_length_1d(els);
    for (var i = 0; i < n; i++) {
        var el = els[i];
        if (layer_get_element_type(el) == layerelementtype_tilemap) {
        	return el;
        }
    }
    return -1;
}

Ah sorry for wasting your time, I completely missed that! Thank you!