"attempted to index field '?' (a nil value)"

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
LaserGuns
Prole
Posts: 33
Joined: Sun Apr 29, 2012 12:55 am

"attempted to index field '?' (a nil value)"

Post by LaserGuns »

Hey, it's laserguns, back with another problem with my game.
If you read my last post, you would know that in my game (as of right now) it determines which block is selected by the X position of the white selection box on the hot bar. This means that the "ID"s of the tiles are random numbers such as 202 or 372, so in order to make it easier to use, I made variables for each tile, like this:

Code: Select all

planks = 202
emeraldore = 236
dirt = 270
stone = 304
grass = 338
wood = 372
leaves = 406
I am trying to make a basic terrain generator, and I am using the same method to generate tiles as I am for placing/breaking tiles. For example, to spawn a grass tile, I'm trying to use:

Code: Select all

blockdata[block_y][block_x] = grass
but right now It's giving me the following error:

Code: Select all

main.lua:195 attempted to index field '?' (a nil value)
There are no question marks on that line, so I assume that means it doesnt understand something?
What am I doing wrong? It uses the exact same technique to place tiles, I don't understand why it's not working.

Before you download the .love file, here are some things to note while you are looking at it:
in love.update(), the map_load() function is set to only repeat 256 times, (which would give 256 columns of terrain).
I do this by having a variable (called "swag" for now) that increases every cycle, and as long as it's less than 256, it repeats the map_load() every cycle.
I also have it so the block placing function (love.keypressed()) is only functional while "swag" is GREATER than 256, which means you can't place tiles until the world is finished generating.
Just some extra info in case you need it.

I've marked where the error is with --ERROR HERE, at line 195 in main.lua.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: "attempted to index field '?' (a nil value)"

Post by Robin »

Line 193 is the problem:

Code: Select all

block_y = 224
You don't know if blockdata[224] exists, yet you don't even check for it.

Not sure what line 192-205 is even supposed to be doing.
Help us help you: attach a .love.
LaserGuns
Prole
Posts: 33
Joined: Sun Apr 29, 2012 12:55 am

Re: "attempted to index field '?' (a nil value)"

Post by LaserGuns »

Robin wrote:Line 193 is the problem:

Code: Select all

block_y = 224
You don't know if blockdata[224] exists, yet you don't even check for it.

Not sure what line 192-205 is even supposed to be doing.
It's supposed to be generating terrain.
and block_y = 224, as in the block_y coordinate. Is that still wrong?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: "attempted to index field '?' (a nil value)"

Post by Robin »

It is. blockdata[n] is only defined for n <= maxY == math.ceil(love.graphics.getHeight()/size) == 20
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests