Ran into an issue that was hard to track down.
I have a two-dimensional array of boolean values, which I use to gather information about the level.
I then use the world position of an object, and convert it to the grid coordinates, when moving objects in the grid.
The bug was that, I got this error, when trying to look for a specific index, based on the position of an object, like this:
[code]function M.get_wall_at( pos )
if M.is_out_of_bounds(pos) then
return true
end
return M.walls[pos.x][pos.y]
end[/code]
But I kept getting this error:
I made sure M was not null, I made sure walls had values, I made sure the position was at an integer value, and so on.
But it was still giving me the error.
I had no idea what was going on, until I noticed that the vector printing seems to round the component values, but only when appended to an existing string
print(pos)
print("Pos = " .. pos)
print(pos.x .. ", " .. pos.y)
Outputs:
DEBUG:SCRIPT: vmath.vector3(2, 1.0000001192093, 0)
DEBUG:SCRIPT: Pos = [2.000000, 1.000000, 0.000000]
DEBUG:SCRIPT: 2, 1.0000001192093
Notice that the center one rounds the decimal values off. This is the one I used to debug, and it took me a long time to see that the components themselves were off-integer.
Now, that function shouldn’t even have been called when the position of the object is off-grid.
I set a bool to true in a function, which is called when the move animation is finished. However, it seems like it’s possible that the callback is sent too early, because if I spam-click to move, in some rare cases, I got this error, where the callback was too early, and a non-integer position slipped through. This is most likely just one frame early, or similar.
So, my take on this:
- The error when sending a decimal value into the array brackets should be much more descriptive
- I recommend showing the full precision of the vector values when appending a vector3 to a string, and only truncate if you have repeating zeroes at the end, like the first and last print method
- Make sure that the animations are completely finished before running the callback. If I animate a position, I expect it to be at the target location when I read it. Note that this is a guess of mine, but I can’t seem to find any other reason as for why I would be able to run that function and get decimal position reads
Long post, but, thanks for reading, and cheers!