Hello Guest

Author Topic: Check against TileMap.TileInfo.stringVal freezes Unity  (Read 3993 times)

maaton

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Check against TileMap.TileInfo.stringVal freezes Unity
« on: June 01, 2014, 12:16:58 am »
Hi,

First of all I must say that I am really satisfied with 2dtoolkit as it has really saved me a lot of hassle putting together a tileMap based IOS game. Lately I've been trying to implement a simple custom A* pathfinding algorithm with 2d Toolkit Tilemap and everything
seems to be working fine without any walls to bump into. However, every time the algorithm checks against tiles tagged as a walls (a string value "wall" was set in the editor's data tab) Unity freezes for good and has to be restarted. Here is the method that checks if there is a wall at a given tile coordinate:

private bool isWallAtTileCoord (Vector2 tileCoord)
      {
            int tileId = tileMap.GetTile ((int)tileCoord.x, (int)tileCoord.y, 0);
   
            tk2dRuntime.TileMap.TileInfo tileInfo = tileMap.GetTileInfoForTileId (tileId);
            
            return tileInfo.stringVal == "wall";   
      }

After several debugging attempts to find out what's happening it all came down to this method, this is the only place where the problem can be, without this check for "walls" the algorithm works fine (The character finds the shortest path on the tileMap and follows it). This information (tileInfo.stringVal == "wall" true or false) is passed to another method that looks for adjacent tiles in four directions, determines if a tile is walkable, creates a list out of the walkable tiles based on their tile coordinates and later on in another method the scores are calculated for each tile etc. (as in any basic A* algorithm).

I was able to simply Debug.Log() the string values of the tiles with given "wall" data without any problems, but every time I want this method to actually return true (find the "wall" string in the tileInfo) and choose a path where a wall eventually has to be avoided, Unity freezes without any error messages as it would with an infinite loop. Do you guys have any idea what on earth could be going on here? I would really appreciate any help as it is driving me crazy by now. If necessary I can provide the rest of the code.

Marton


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Check against TileMap.TileInfo.stringVal freezes Unity
« Reply #1 on: June 01, 2014, 06:40:03 pm »
No idea, this is a string comparison, you'd expect it to work...
Anyway I suppose you could try stringVal.GetHashCode() == "wall".GetHashCode() to see if that works better?

maaton

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Check against TileMap.TileInfo.stringVal freezes Unity
« Reply #2 on: June 01, 2014, 07:30:51 pm »
Thank you for your reply. I isolated the problem to a separate scene to find out if it is really the string comparison that is causing the problem and everything worked fine. With this exact same method I was able to print out messages if I click on a "wall tile" so I figured out this has nothing to do with the string comparison. It must be something with my algorithm which I don't really get because if a "wall" check returns false everything works fine. The problem only occurs when it eventually returns true. I tried to trace the root of the problem so I modified my code a bit to get a glimpse what's going on. After more debugging I managed to get an error

IndexOutOfRangeException: Array index is out of range.
tk2dRuntime.TileMap.Layer.GetRawTileValue (Int32 x, Int32 y, System.Int32& value) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:387)...

So apparenrly this is not 2dtoolkit issue after all... Thank you, anyway!