Error Message attempt to index a nil val

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MalinGraham
Prole
Posts: 3
Joined: Wed Jan 25, 2017 2:22 pm

Error Message attempt to index a nil val

Post 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
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Error Message attempt to index a nil val

Post 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.

Code: Select all

tiles[tileNumber] = { }
MalinGraham
Prole
Posts: 3
Joined: Wed Jan 25, 2017 2:22 pm

Re: Error Message attempt to index a nil val

Post 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.

Code: Select all

tiles[tileNumber] = { }
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?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Error Message attempt to index a nil val

Post 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.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Error Message attempt to index a nil val

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests