Hello Guest

Author Topic: IndexOutOfRangeException FindChunkAndCoordinate  (Read 3506 times)

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
IndexOutOfRangeException FindChunkAndCoordinate
« on: January 15, 2014, 07:10:50 am »
I receive the following error randomly when trying to modify a tile programatically. It is difficult because I cannot replicate it with any one tile, each time it seems to happen to a different tile. When the error does occur, tk2d stops working altogether and the scene has to be stopped and restarted.

Code:
Code: [Select]
tileMap.GetTileAtPosition(whichBlockToCheck,  out x, out y);
tileMap.SetTile(x, y, 0, -1);
tileMap.Build();

Error:
Code: [Select]
IndexOutOfRangeException: Array index is out of range.
tk2dRuntime.TileMap.Layer.FindChunkAndCoordinate (Int32 x, Int32 y, System.Int32& offset) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:372)
tk2dRuntime.TileMap.Layer.GetRawTileValue (Int32 x, Int32 y, System.Int32& value) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:384)
tk2dRuntime.TileMap.Layer.GetTileFlags (Int32 x, Int32 y) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:428)
tk2dRuntime.TileMap.Layer.SetTile (Int32 x, Int32 y, Int32 tile) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:450)
PLTileGridManager+<MineBlock>c__IteratorA.MoveNext () (at Assets/Scripts/PLTileGridManager.cs:606)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
« Last Edit: January 15, 2014, 10:38:30 pm by Euthyphro »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: IndexOutOfRangeException FindChunkAndCoordinate
« Reply #1 on: January 15, 2014, 11:15:02 am »
What is x & y when it happens?

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: IndexOutOfRangeException FindChunkAndCoordinate
« Reply #2 on: January 15, 2014, 11:55:20 pm »
It is x and y 32 that is causing the issue as the tileset is only 32 wide/high, thus anything over 31 would be out of bounds. With that said, why would it return 32 from "GetTileAtPosition"? in the first place? Also, might I suggest in a future release that instead of an out of bounds tile causing tk2d to fail (i.e. tk2d functions no longer work after this type of error), instead maybe a try/catch? Essentially if this is called out of bounds, like I said before, you have to restart the entire scene since tk2d tilemap dies out.

px,py = on screen coordiantes, x,y are the coordinates returned from tileMap.GetTileAtPosition(whichBlockToCheck, out x, out y); "whichBlockToCheck" originates from the world coordinates of a mouse click.

Code: [Select]
px,py:256,844  x,y:32,22
UnityEngine.Debug:Log(Object)

Here in another case:
Code: [Select]
px,py:201,833  x,y:32,23
UnityEngine.Debug:Log(Object)

Code: [Select]
x,y:222,810  px,py:21,32
UnityEngine.Debug:Log(Object)
« Last Edit: January 16, 2014, 12:01:58 am by Euthyphro »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: IndexOutOfRangeException FindChunkAndCoordinate
« Reply #3 on: January 16, 2014, 10:52:42 am »
The main bug here is GetTileAtPosition returning out of bounds - this is in line tk2dTileMap.cs, line 337. it should read
Code: [Select]
return (x >= 0 && x < width && y >= 0 && y < height);instead of
Code: [Select]
return (x >= 0 && x <= width && y >= 0 && y <= height);
I've been really trying to avoid bounds checks all over the place - this whole thing goes through many layers - SetTile(x,y) calls Layer.SetTile(..) which in turn calls Layer.SetRawTile(...). It'd have to bounds check all the way through in multiple places. Much more efficient to bounds check just once at the caller. Hopefully with GetTileAtPosition fixed (it returns false if out of bounds), you won't have to even check if its in bounds.

Code: [Select]
if (tileMap.GetTileAtPosition(whichBlockToCheck,  out x, out y)) {
tileMap.SetTile(x, y, 0, -1);
tileMap.Build();
}