Page 1 of 1
Error Message attempt to index a nil val
Posted: Wed Jan 25, 2017 2:25 pm
by MalinGraham
When running this code, line 13 causes the error message.
Code: Select all
tiles = {}
--tiles{{"tileNumber","xCoordinate", "yCoordinate"}}
tileNumber = 0
wHeight = love.graphics.getHeight()
wWidth = love.graphics.getWidth()
function tileCreate()
for x=0, wWidth, 50 do
for y=0, wHeight, 50 do
tiles[tileNumber][0] = tileNumber
tiles[tileNumber][1] = x
tiles[tileNumber][2] = y
tileNumber = tileNumber + 1
end
end
end
Re: Error Message attempt to index a nil val
Posted: Wed Jan 25, 2017 2:34 pm
by raidho36
Hello and welcome to the forums!
Your problem is really simple: you have "tiles" table, but not "tiles[tileNumber]" table. You have to create it before you can put values into it.
Re: Error Message attempt to index a nil val
Posted: Wed Jan 25, 2017 2:38 pm
by MalinGraham
raidho36 wrote:Hello and welcome to the forums!
Your problem is really simple: you have "tiles" table, but not "tiles[tileNumber]" table. You have to create it before you can put values into it.
I am using tileNumber to find an index in the tiles table, I didn't want two tables.
I want to store all of these values in the tiles table where
tiles[tileNumber] is the location of the tile I want to talk to.
Am I just confused?
Re: Error Message attempt to index a nil val
Posted: Wed Jan 25, 2017 2:50 pm
by raidho36
MalinGraham wrote:
Am I just confused?
Yes, probably. If you do it the way you do it, you'd have not one but a whole lot of tables. First bracket would access "tiles" table, but the second bracket would access another table stored in "tiles[tileNumber]" slot. So you'll have master table plus one small table per every single tile.
It's not bad to have many tables (although having huge amounts of tiny tables is wasteful compared to few large tables). In fact, most programs are built around storing all information in variety of tables, making them plentiful. If you need a table for any reason - just go ahead and use one. The single consideration you need to know about it is that tables you no longer grip to become garbage and contribute to garbage collector processing payload, making memory sweeps take more time, so don't just create and discard them in huge numbers, try reusing them.
If you want to be really technical, there are other two: B) tables have minimum memory footprint of having enough space allocated to store 4 integer-keyed values and 4 arbitrary-keyed values i.e. total 8 slots, 4 in array and 4 in hashtable, so creating tables with only 1 or 2 values stored in them is wasteful, and C) each table access takes some time, so data access over longer chain of brackets and dots will take more time. But those two are nothing you should be actually worried about because their impact is usually minuscule.
Re: Error Message attempt to index a nil val
Posted: Wed Jan 25, 2017 3:32 pm
by zorg
In short, you're missing one line:
Code: Select all
tiles = {}
--tiles{{"tileNumber","xCoordinate", "yCoordinate"}}
tileNumber = 0
wHeight = love.graphics.getHeight()
wWidth = love.graphics.getWidth()
function tileCreate()
for x=0, wWidth, 50 do
for y=0, wHeight, 50 do
tiles[tileNumber] = {} -- this line, otherwise the below 3 lines will error.
tiles[tileNumber][0] = tileNumber
tiles[tileNumber][1] = x
tiles[tileNumber][2] = y
tileNumber = tileNumber + 1
end
end
end