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

Attempt to Index a nil value

Started by EnergyMatter, 12 August 2013 - 02:28 PM
EnergyMatter #1
Posted 12 August 2013 - 04:28 PM
Title: Attempt to Index a nil value

"t" API: http://pastebin.com/hUTwD0CR
Stairs: http://pastebin.com/krVPWqw3

bios:339: [string "t"]:89: 'end'
expected (to close 'function' at line 86)
stairs:17: attempt to index ? (a nil
value)

I'm trying to make a program that builds a downwards staircase, and encases it if the walls or floor has a hole in it.
I wrote an API to make certain things easier, such as moving when there is a block/gravel proofing, and I think I did something wrong with the way I wrote the API.

Can anybody explain what I did wrong? Thanks in advance!
campicus #2
Posted 12 August 2013 - 09:26 PM
This is a guess (and I'm sure a pro will come in and blast me) but in this function:


function comL(x)
	lt()
	return com(x)
	rt()
end

does having the return in the middle like that stop "rt()" being run? It was my understanding (limited, I agree lol) that "return" will end the function it is contained in. If a pro can come and clear this up that would be great for me :)/>

Maybe change it to:


function comL(x)
	lt()
	rt()
    return com(x)
end
Lord_Spelunky #3
Posted 13 August 2013 - 12:42 AM
I can not help you, seen as I do not have pastebin at the moment, so if you could put the code in the thread. You don't have to, but then I could help you.
EnergyMatter #4
Posted 13 August 2013 - 01:01 AM
campicus i think doing

function comL(x)
		lt()
		rt()
	return com(x)
end
would be the same as my com(x) function because lt() and rt() are just shortened turtle.turnLeft() and turtle.turnRight()

I can not help you, seen as I do not have pastebin at the moment, so if you could put the code in the thread. You don't have to, but then I could help you.
sure!
"t" API
Spoiler



--TURNING
function lt()
		turtle.turnLeft()
end
function rt()
		turtle.turnRight()
end
function ll()
		lt()
		lt()
end
function rr()
		rt()
		rt()
end
function around()
		if math.random == 0 then
				ll()
		else
				rr()
		end
end
--INVENTORY MANAGEMENT
function slot(x)
		turtle.select(x)
end
function size(x)
		return (turtle.getItemCount(x))
end
function space(x)
		return(turtle.getItemSpace(x))
end
--DETECTING
function det()
		if turtle.detect() == true then
				return true
		end
end
function detU()
		if turtle.detectUp() == true then
				return true
		end
end
function detD()
		if turtle.detectDown() == true then
				return true
		end
end
function detL()
		lt()
		det()
		rt()
end
function detR()
		rt()
		det()
		lt()
end
function detB()
		around()
		det()
		around()
end
--COMPARING
function com(x)
		x = tonumber(x)
		if x ~= nil then
				slot(x)
		end
		return turtle.compare()
end
function comU(x)
		x = tonumber(x)
		if x ~= nil then
				slot(x)
		end
		return turtle.compareUp()
end
function comD(x)
		x = tonumber(x)
		if x ~= nil then
				slot(x)
		end
		return turtle.compareD()
end
function comL(x)
		lt()
		return com(x)
		rt()
end
function comR(x)
		rt()
		return com(x)
		lt()
end
function comB(x)
		around()
		return com(x)
		around()
end
--DIGGING
function dig()
		repeat
				turtle.dig()
				sleep(1)
		until det() == false
end
function digU()
		repeat
				turtle.digU()
				sleep(5)
		until detU() == false
end
function digD()
		turtle.digDown()
		if detD() == true then
				return "bedrock"
		end
end
function digL()
		lt()
		dig()
		rt()
end
function digR()
		rt()
		dig()
		lt()
end
function digB()
		around()
		dig()
		around()
end
--PLACING
function place(x)
		if com(x) == false then
				dig()
				if turtle.place() == false then
						repeat
								sleep(1)
						until turtle.place() == true
				end
		end
end
function placeU(x)
		if comU(x) == false then
				digU()
				if turtle.placeUp() == false then
						repeat
								sleep(1)
						until turtle.placeUp() == true
				end
		end
end
function placeD(x)
		if comD(x) == false then
				if digD() ~= ("bedrock") then
						if turtle.placeDown() == false then
								repeat
										sleep(1)
								until turtle.placeDown() == true
						end
				else return ("bedrock")
				end
		end
end
function placeL(x)
		lt()
		place(x)
		rt()
end
function placeR(x)
		rt()
		place(x)
		lt()
end
function placeB(x)
		around()
		place(x)
		around()
end
--STRAFE
function f(x)
		x = tonumber(x)
		if x == nil then
				x = 1
		end
		for i = 1, x do
				if det() == true then
						dig()
				end
				if turtle.forward() == false then
						repeat
								sleep(1)
						until turtle.forward() == true
				end
		end
end
function u(x)
		x = tonumber(x)
		if x == nil then
				x = 1
		end
		for i = 1, x do
				if detU() == true then
						digU()
				end
				if turtle.up() == false then
						repeat
								sleep(1)
						until turtle.up() == true
				end
		end
end
function d(x)
		x = tonumber(x)
		if x == nil then
				x = 1
		end
		for i = 1, x do
				if detD() == true then
						return digD()
				end
				if turtle.down() == false then
						repeat
								sleep(1)
						until turtle.down() == true
				end
		end
end
function l(x)
		lt()
		f(x)
		rt()
end
function r(x)
		rt()
		f(x)
		lt()
end
function b(x)
		around()
		f(x)
		around()
end
--GO
function goL(x)
		lt()
		f(x)
end
function goR(x)
		rt()
		f(x)
end
function goB(x)
		around()
		f(x)
end

[/SPOILER]

Stairs

Spoiler

os.loadAPI("t")
tArgs = {...}
local stair = tArgs[1]
stair = tonumber(stair)
if stair == nil then
		stair = 64
end
local torch = tArgs[2]
torch = tonumber(torch)
if torch == nil then
torch = 7
end
if torch > 7 then
		torch = 7
end
local torchReq = math.ceil(stair / torch)
local torchCount = t.size(1)
local torchErr = false
local stairReq = stair
local stairCount = t.size(2)
local stairErr = false
local blockReq = 8 * stair
local blockCount = t.size(3)
local blockErr = false
local counter = 0
function bedrock()
		for i = 1, counter do
				t.b()
				t.u()
		end
end
function light()
		if counter % torch == 0 then
				t.placeU(1)
		end
end
function wall()
		t.placeL(3)
		t.placeR(3)
end
function step()
		if t.d() ~= ("bedrock") then
				wall()
				t.placeD(3)
				t.f()
				wall()
				t.u()
				t.place(3)
				wall()
				t.d()
				light()
				counter = counter + 1
		else
				bedrock()
		end
end
--START CHECKS
function torchCheck()
		if torchCount < torchReq then
				torchErr = true
		else
				torchErr = false
		end
end
function stairCheck()
		if stairCount < stairReq then
				stairErr = true
		else
				stairErr = false
		end
end
function blockCheck()
		if blockCount < blockReq then
				blockErr = true
		else
				blockErr = false
		end
end
function startCheck()
		torchCheck()
		stairCheck()
		blockCheck()
end
--START RESOLVES
function torchResolve()
		if torchErr == true then
				print("I need "..torchReq - torchCount.." more torches in SLOT1")
		end
end
function stairResolve()
		if stairErr == true then
				print("I need "..stairReq - stairCount.." more stairs in SLOT2")
		end
end
function blockResolve()
		if blockErr == true then
				print("I need "..blockReq - blockCount.." more blocks in SLOT3")
		end
end
function startResolve()
		torchResolve()
		stairResolve()
		blockResolve()
end
--ERROR CLEARING LOOP
function startErr()
		startCheck()
		if  (torchErr or stairErr or blockErr) == true then
				repeat
						startCheck()
						startResolve()
						sleep(10)
				until (torchErr and stairErr and blockErr) == false
		end
end

EDIT: I didn't realize my tabs from notepad++ would show up as 12 spaces
LBPHacker #5
Posted 13 August 2013 - 06:28 AM
-snip-
Yup, you were right.

As this document explains, it really makes Lua mad if you're trying to execute code after a return statement. A return statement which you are sure will be executed (so no IF conditions and the like) must be the last statement in a chunk. Put the return value of com into a local variable, call rt, and return the local variable.

12 spaces
That's just IP.Board messing up the spaces. I suppose you edited your reply twice. When posting here, keep in mind to replace all tabulators with four spaces: Ctrl-H: "\t" to <four spaces> (Search mode: Extented). If you paste the code into the code tags again, that somehow fixes this.
EnergyMatter #6
Posted 13 August 2013 - 11:24 AM
Thanks for the help!