This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
RandomDuwd's profile picture

Annoying bug

Started by RandomDuwd, 15 July 2014 - 09:55 AM
RandomDuwd #1
Posted 15 July 2014 - 11:55 AM
Hey guys,

im writing a game (not very far into it, you'll see :)/> ), but im having a litte bug that i just cant fix!
code: http://pastebin.com/d16RN75N
error: game:38: attempt to index a nil value

Hope you guys can help me out
GamerNebulae #2
Posted 15 July 2014 - 01:18 PM
You're not declaring a function the right way. The correct way is this:

function load(path)
  --#Code here...
end
theoriginalbit #3
Posted 15 July 2014 - 01:32 PM
that's not the problem at all. functions can be declared like that GamerNebulae. A function name is just a variable that stores a memory pointer to the actual function, therefore you can assign a function into a variable, just like RandomDuwd is doing. It is also necessary to define functions that way when you're declaring them in a table.

the problem is that you're referencing the level table from within the function before the table exists, this is not allowed as the table is nil at this point. as such you need to declare that function outside the table after the table has been created i.e. you've closed it with }

like so

level = {
  --# declare functions and values for the table
}

level.getTile = function(x,y)
  i = level.tiles[y][x]
  return i
end
GamerNebulae #4
Posted 15 July 2014 - 01:44 PM
-snip-

Pardon me then. I have been trying to learn Java and C# which are consistent languages, whilst Lua has 3 ways to write something. Read theoriginalbit's answer please.