The world is a table/array with the x coord, then y coord, the a table with information about the block, [1] the id, [2] name and [3] colour. It says the third table, with info about the block is nil, and therefore doesn't exist. On line 27, this is where the info about the block is assigned to the coordinates, at line 11/12 is where the info about block is made, calling on the method at line 4.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Table error, attempt to index nil value
Started by MatthewGB, 19 May 2014 - 10:20 PMPosted 20 May 2014 - 12:20 AM
I keep getting an error in line 50 of pastebin.com/B7DMEc28 , the table seems to reset itself, resulting in the nil value error. How do I fix this?
The world is a table/array with the x coord, then y coord, the a table with information about the block, [1] the id, [2] name and [3] colour. It says the third table, with info about the block is nil, and therefore doesn't exist. On line 27, this is where the info about the block is assigned to the coordinates, at line 11/12 is where the info about block is made, calling on the method at line 4.
The world is a table/array with the x coord, then y coord, the a table with information about the block, [1] the id, [2] name and [3] colour. It says the third table, with info about the block is nil, and therefore doesn't exist. On line 27, this is where the info about the block is assigned to the coordinates, at line 11/12 is where the info about block is made, calling on the method at line 4.
Edited on 20 May 2014 - 03:46 PM
Posted 20 May 2014 - 05:47 PM
Edited post to help users understand the code.
Posted 20 May 2014 - 09:02 PM
I haven't had extensive time to go through the code, but a quick tip that helps is to use variable names that are self-descriptive. Instead of using i or j, use width or depth. Things of that nature help others follow you code. Also, we may need to see the api you're using as well. I'm having a similar issue with my program losing values. I'll get back to you if I figure it out.
Posted 20 May 2014 - 09:36 PM
First of all in line 23 and 26 you have to replace the "not xy == yx" with "xy <= yx", so ot looks like this "while i<=49 do", because it first "checks" the not.
So it just checks if the variable isn't nil, which, of course, it is not.
You can find the Precedence here: http://www.lua.org/manual/5.1/manual.html#2.5.6
Also in line 6 you write "block[2] = name", however paintutils.drawPixel needs x, y and color.
By calling world[j][2] you are returning the name, which is a string.
Just replace it with world[j][3].
Fixed code:
http://pastebin.com/Q9wid76u
Also, you could shorten your code as following
It's easier to read and even shorter.
You also don't need j = j + 1 and i = i + 1 in for loops, they add 1 automatically.
So it just checks if the variable isn't nil, which, of course, it is not.
You can find the Precedence here: http://www.lua.org/manual/5.1/manual.html#2.5.6
Also in line 6 you write "block[2] = name", however paintutils.drawPixel needs x, y and color.
By calling world[j][2] you are returning the name, which is a string.
Just replace it with world[j][3].
Fixed code:
http://pastebin.com/Q9wid76u
Also, you could shorten your code as following
function initb(id, name, colour)
block = {id}
block[2] = name
block[3] = colour
return block
end
blocks = {}
block0 = initb(0, "Player", 1)
block1 = initb(1, "Stone", 14)
blocks[1] = block0
blocks[2] = block1
to
blocks = {
["block0"] = {
0, "Player", 1
};
["block1"] = {
1, "Stone", 14
};
}
It's easier to read and even shorter.
You also don't need j = j + 1 and i = i + 1 in for loops, they add 1 automatically.
Posted 21 May 2014 - 05:12 PM
Thanks so much!
Posted 21 May 2014 - 09:04 PM
A few mins after editing a bit, got the same problem again! What did I do wrong this time???
Posted 21 May 2014 - 10:57 PM
Post the updated code for us to check it out please
Posted 22 May 2014 - 09:06 AM
Same pastebin file
Posted 22 May 2014 - 01:12 PM
Would actually be nice if you update the line the program errors in the next time. (I had to find it out for myself)
So it's line 42 which looks like this
"programname:42:index expected, got nil"
(The table call needs a valid index)
Unfortunately I can't fix this, because I don't know how it's intended to work and why.
If I got this correct, this would extend the world when you move forward.
If so, then you would only set 1 pixel (because only the pixel px, py will be set).
Instead of that I would write a function, which sets the entire row to that block.
So it's line 42 which looks like this
world[px][py] = blocks[1]
Before that line you haven't defined px and py yet, this is, why it errors with"programname:42:index expected, got nil"
(The table call needs a valid index)
Unfortunately I can't fix this, because I don't know how it's intended to work and why.
If I got this correct, this would extend the world when you move forward.
If so, then you would only set 1 pixel (because only the pixel px, py will be set).
Instead of that I would write a function, which sets the entire row to that block.
Posted 22 May 2014 - 06:53 PM
Px = Player X Coord
Py = Player Y Coord
It was only mean't to set one pixel to the current player pixel, but if you take that line out, you still get an error in the same place as before.
Py = Player Y Coord
It was only mean't to set one pixel to the current player pixel, but if you take that line out, you still get an error in the same place as before.
Posted 26 May 2014 - 04:13 AM
Bump.
Posted 27 May 2014 - 11:59 AM
So, the first things I saw were in line 22, 25, 27 and 29.
In the table "blocks" you never defined 1-5, you defined block1, block2 etc..
So you have to call it either with
Your "w" and "s" keys are reversed (if w is pressed, it should subtract 1. If s is pressed, it should add 1, not vice versa).
For now I would leave out the os.startTimer(0.05), because the screen updates on any event triggered.
That's it, fixed code here. (Note: I don't fix the code so you can paste it and it works. You should read it and see, what your mistakes were and how you probably could have fixed them)
http://pastebin.com/s9BsGAP1
In the table "blocks" you never defined 1-5, you defined block1, block2 etc..
So you have to call it either with
blocks.block1
or withblocks["block1"]
Also, as I already mentioned, you haven't set px and py in your program yet. Put it wherever you want, but put it above your main loop.Your "w" and "s" keys are reversed (if w is pressed, it should subtract 1. If s is pressed, it should add 1, not vice versa).
For now I would leave out the os.startTimer(0.05), because the screen updates on any event triggered.
That's it, fixed code here. (Note: I don't fix the code so you can paste it and it works. You should read it and see, what your mistakes were and how you probably could have fixed them)
http://pastebin.com/s9BsGAP1
Edited on 27 May 2014 - 09:59 AM