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

"build.house():1: index expected, got nil" error. Sorry, I'm new in this.

Started by Vermillion_Catus, 06 December 2017 - 11:37 AM
Vermillion_Catus #1
Posted 06 December 2017 - 12:37 PM
Hi, I'm pretty new programming whit computercraft, I don't have all the knowledge yet. I wanted to make a program for a turtle to build a little house, whit the purpose of testing what I have learned, but I keep getting an error and I don't know what to do. Please help me.
The error is the following: build.house():1: index expected, got nil
And here is the code of the program, sorry if is a little messy, I tried my best to organize all the lines to be readable:

function floor.ceiling()
turtle.placeDown()
for a = 1,6 do
turtle.forward()
turtle.placeDown()
end
end

function wall.tile()
for a = 1,3 do
turtle.up()
turtle.placeDown()
end
end

function position()
turtle.turnLeft()
turtle.forward()
for a = 1,3 do
turtle.down()
end
end

function wall()
for a = 1,5 do
wall.tile()
turtle.forward()
for a = 1,3 do
turtle.down()
end
end

wall.tile()
position()
end

term.clear()
term.setCursorPos(1,1)
print ("Building…")

turtle.turnRight()
turtle.forward()
turtle.up()
turtle.select(1)
for a = 1,3 do
floor.ceiling()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
floor.ceiling()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end

floor.ceiling()
turtle.turnLeft()
turtle.forward()
turtle.select(2)
wall()
wall()
wall()
for b = 1,6 do
wall.tile()
turtle.forward()
for a = 1,3 do
turtle.down()
end
end

turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.down()
turtle.down()
turtle.down()
turtle.turnLeft()
turtle.select(4)
turtle.place()
turtle.up()
turtle.turnLeft()
turtle.dig()
turtle.select(3)
turtle.place()
turtle.down()
turtle.turnLeft()
turtle.forward()
turtle.select(5)
turtle.place()
turtle.up()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.dig()
turtle.select(3)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.down()
turtle.turnRight()
turtle.turnRight()
turtle.select(5)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.up()
turtle.dig()
turtle.select(3)
turtle.place()
turtle.down()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.select(6)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.place()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.up()
turtle.dig()
turtle.select(3)
turtle.place()
turtle.down()
turtle.turnRight()
turtle.forward()
turtle.select(7)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.back()
turtle.back()
turtle.select(8)
turtle.place()
turtle.back()
turtle.down()
turtle.back()
turtle.select(9)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
for c = 1,5 do
turtle.up()
end

turtle.forward()
turtle.select(1)
for d = 1,3 do
floor.ceiling()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
floor.ceiling()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
end

floor.ceiling()
turtle.turnRight()
for e = 1,7 do
turtle.forward()
end

turtle.turnRight()
for f = 1,6 do
turtle.forward()
end

term.clear()
print ("Successful")
sleep(4)
term.clear()
term.setCursorPos(1,1)
Bomb Bloke #2
Posted 06 December 2017 - 01:24 PM
On the very first line, you're trying to define a function pointer against the key named "ceiling" within a table (an "index") defined as "floor". Catch is, "floor" doesn't point to a table: you never actually defined any value for that variable, so it's still set as nil.

Adding "local floor = {}" to the top of the script would resolve that, but you'd still have much the same issue with the next function, which you're attempting to record against a "tile" key within a non-existent table dubbed "wall". And then the fourth function you define you also store as "wall" - if "wall" was rigged to point to a table, that'd replace it with a function pointer instead, losing you access to the "tile" key within that table.

Since you're only placing single keys within these wall / floor tables, there's not much point is setting them up as tables at all. Better to just remove the periods that tell Lua to treat them as indexes, creating new unique variable names instead - eg, instead of floor.ceiling / wall.tile, instead have makeCeiling, buildTile…
Edited on 06 December 2017 - 12:26 PM
Vermillion_Catus #3
Posted 06 December 2017 - 05:15 PM
On the very first line, you're trying to define a function pointer against the key named "ceiling" within a table (an "index") defined as "floor". Catch is, "floor" doesn't point to a table: you never actually defined any value for that variable, so it's still set as nil.

Adding "local floor = {}" to the top of the script would resolve that, but you'd still have much the same issue with the next function, which you're attempting to record against a "tile" key within a non-existent table dubbed "wall". And then the fourth function you define you also store as "wall" - if "wall" was rigged to point to a table, that'd replace it with a function pointer instead, losing you access to the "tile" key within that table.

Since you're only placing single keys within these wall / floor tables, there's not much point is setting them up as tables at all. Better to just remove the periods that tell Lua to treat them as indexes, creating new unique variable names instead - eg, instead of floor.ceiling / wall.tile, instead have makeCeiling, buildTile…

It worked, thanks. It was a pretty silly mistake hehe.