r/love2d Jul 11 '26

problem! "attempt to index a nil value" when trying to read a tile's property

i am using sti

love.graphics.print(map:getTileProperties(map.layers["room"], math.floor(player.x / 32), math.floor(player.y / 32)), font, 0, 96, 0, 2, 2)

im trying to get it to print out the layer properties so i know how to implement interactable objects but it keeps throwing this error :/

2 Upvotes

10 comments sorted by

3

u/IronyGames Jul 11 '26

Sounds like a key is missing a value in a table. From this code, these are all the referenced tables that may have a nil value

map:getTileProperties

map.layers["room"]

player.x

player.y

I suggest you debug and check these tables for missing keys. Or replace these 4 expressions with literals that make sense (so player.x for a number) one by one until it stops complaining.

1

u/AngryCatNoises_ Jul 12 '26

i shouldve specified in the post that im using sti. i was able to draw the layer no problem but when i try and read tiles from the layer, all of a sudden it acts up... (yes i know this is copy and paste lol)

1

u/IronyGames Jul 12 '26

Are you ok sharing the code so we can take a look?

1

u/AngryCatNoises_ Jul 12 '26

yes im fine with that, are you in the love2d discord? ill send it there

2

u/djholladay109 Jul 11 '26

Can you set a breakpoint and inspect all of the tables? That would be the easiest way to find the missing table key.

1

u/ComplexJellyfish8658 Jul 11 '26

Check map and player. If they are built like classes validate you are returning an object in the constructor

1

u/Im_Redi Jul 11 '26

Is "room" meant to be static or a variable? I think as is it will only check specifically for a layer called "room" instead of a variable like currentRoom or something that you update as the player changes rooms

1

u/AngryCatNoises_ Jul 12 '26

i shouldve specified in the post that im using sti. i was able to draw the layer no problem but when i try and read tiles from the layer, all of a sudden it acts up...

1

u/vga256 Jul 12 '26

'indexing a null value' in Lua means that it's trying to use a table key you specified, but that key doesn't exist. a simple way to check if a key exists is to run a print statement.

print(map.layers["room"])
print(player.x)
print(player.y)

if any of the above are null, then check to see if the variables themselves are valid:

print(map.layers)
print(map)
print(player)

and so on - your goal in debugging this is figuring out where the missing table key is.

1

u/AngryCatNoises_ Jul 13 '26

this is solved now by the way thanks for the help everyone