GameMaker Tile Collision only working sometimes

MeltingCat

Member
Hi, I'm working on a top down game that uses tile collision to detect certain information about the floor the player is standing on. I have made a script for this using tile collision and it works just great - apart from in some rooms.
There are certain rooms in my game where it seems to just not trigger a "true" where there definitely is the tile of question in place.

Here is the code:


GML:
//Room start event

if layer_exists("WaterCollision") {
  
    WaterCollisionLayer = layer_get_id("WaterCollision")
    WaterCollisionMapId = layer_tilemap_get_id(WaterCollisionLayer)
  
}

Code:
//step event

IsInWater = Water_Tile_Collision(WaterCollisionMapId, WaterCollisionLayer)
Code:
// script 'Water_Tile_Collision'

///@arg Tilemap
///@arg Layer

var _TileMap = argument0
var _Layer = argument1
var _InWater = false

if ! layer_exists(_Layer) {
  
    _InWater = false
  
    if layer_exists("WaterCollision") {      
        WaterCollisionLayer = layer_get_id("WaterCollision")
        WaterCollisionMapId = layer_tilemap_get_id(WaterCollisionLayer)            
    }
  
    show_debug_message("no layer existing")
  
} else {

    if layer_tilemap_exists(_Layer, _TileMap) {

        if tilemap_get_at_pixel(_TileMap, x, y) == 1 {
  
            _InWater = true
  
        } else _InWater = false

    } else {
      
        if layer_exists("WaterCollision") { 
            WaterCollisionLayer = layer_get_id("WaterCollision")
            WaterCollisionMapId = layer_tilemap_get_id(WaterCollisionLayer)           
        }
      
        show_debug_message("no tilemap existing")
        _InWater = false;
    }

}

return _InWater

debug messages show that in the beginning of the room for about 20 steps the layer doesn't exist according to the script. But after that it's not saying that anymore but the collision keeps working only partly.
In game it's an area with 'water' for most of the room, but for certain patches it is not detected as such.

I'm not quite certain on how to handle the layer_id and collision map id, and I have a feeling that these errors occur when the room has been duplicated and edited, but also this is inconsistent.
If there is something I've overlooked, any advice would be helpful. If the code looks all good, it seems like a GMS2 bug to me, but I'd be very happy to find out that it's not.

Thank you for all answers in advance.
 
Top