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

[LUA][ERROR] t.lib :55: attempt to index ? (a nil value)

Started by tedraic, 27 April 2012 - 02:15 AM
tedraic #1
Posted 27 April 2012 - 04:15 AM
I am having trouble getting Clueless's flatterrain program to work. I have copied the text just as it is in the programs section but I keep getting the error message t.lib:55: attempt to index ? ( a nil value)

I have double and tripple checked the code from 50 through 60 and everything is exactly as he has it written. can some one plaease help me to get this to work? I am going to post all of the code below just in case it is something other then what is in t.lib

Thank you in advance for any help that you can offer… Also on a side note, I can not get cc-get to work at all. I have something called pastebin but can not seem to get cc-get to load no matter what I do. I have already enabled the HTTP api in the config file and merged the cc-get ComputerCraft folder with the original ComputerCraft folder. I am playing MineCraft 1.2.5 and using ComputerCraft 1.33
Once again, thanks in advance.


the files are t.lib, common.lib, and flatterrain.

** t.lib** ——————————————————————————————————————————————————

Spoiler

--[[
		Main turtle utils

		x growh to left,
		y growh to up,
		z is height
		angle is anticlockwise, pi == 2, 0 is up
		and we allways start at 0,0,0,0
]]
moveMatrix = {
		{0,1}, -- up
		{1,0},  -- left
		{0,-1},  -- down
		{-1,0}  -- right
}

t = {} -- singleton

function t.init()
		t.x=0
		t.y=0
		t.z=0
		t.a=0
		t.m=moveMatrix[1]
end


function t.left()
		t.a=t.a+1
		t.a = t.a % 4
		t.m = moveMatrix[t.a+1] -- fucked lua table indexes
		turtle.turnLeft()
end
t.l = t.left


function t.right()
		t.a=t.a-1
		t.a = t.a % 4
		t.m = moveMatrix[t.a+1]
		turtle.turnRight()
end
t.r = t.right


function t.tryForward()
		if turtle.forward() then
				t.x = t.x + t.m[1]
				t.y = t.y + t.m[2]
				return true
		end
		return false
end
t.tf = t.tryForward


function t.digForward()
		repeat
				local _ = turtle.detect() and turtle.dig()
		until t.tryForward()
end
t.df = t.digForward


function t.tryUp()
		if turtle.up() then
				t.z = t.z + 1
				return true
		end
		return false
end
t.tu = t.tryUp


function t.digUp()
		repeat
				local _ = turtle.detectUp() and turtle.digUp()
		until t.tryUp()
end
t.du = t.digUp


function t.tryDown()
		if turtle.down() then
				t.z = t.z - 1
				return true
		end
		return false
end
t.td = t.tryDown


function t.digdown()
		repeat
				local _ = turtle.detectDown() and turtle.digDown()
		until t.tryDown()
end
t.dd = t.digdown


function t.faceTo(direction)
		local turnfunc = (direction > t.a) and t.l or t.r
		while(direction ~= t.a) do turnfunc() end
end
t.ft = t.faceTo


function t.hasSpace()
		return(turtle.getItemCount(9) == 0)
end
t.hs = t.hasSpace


function t.strCoord()
		return t.x .. ", " .. t.y .. " (" .. t.z .. ") a:" .. t.a
end

function t.getPos()
		return {x=t.x, y=t.y, z=t.z, a=t.a}
end

t.init()


**common.lib** ——————————————————————————————————————————-

Spoiler


common = {}

function common.dump(obj)
		print(obj,"n", textutils.serialize(obj))
end



**flatterrain** ———————————————————————————————————————————-

Spoiler


--[[
		flatterns some space, dump mined at 0,0,0 facing down
		so, obsidian pipe must be at 0,-1,-1 and payload allways be safe
]]

local args = {...}
if #args == 0 then
		print("flatterrain <widthx> <widthy>")
		shell.exit()
end
local maxx,maxy = tonumber(args[1]), tonumber(args[2])

		

if not t then shell.run("t.lib") end
if not common then shell.run("common.lib") end
t.init()

local empty_levels = 0 -- levels without digging anything

function LOG(...) print(t.strCoord(), " ", ...) end


function ReturnToBase()
		while(t.z > 0) do t.dd() end
		t.ft(3)
		while(t.x > 0) do t.df() end
		t.ft(2)
		while(t.y > 0) do t.df() end
end


function GoToPosition(p)
		t.ft(0);
		while(t.y < p.y) do t.df() end
		t.ft(1)
		while(t.x < p.x) do t.df() end
		t.ft(p.a)
		while(t.z > p.z) do t.du() end
end


function DumpStomach()
		local i
		for i=1,9 do
				turtle.select(i)
				turtle.drop()
		end
		turtle.select(1)
end


function EmptyRun()
		local p = t.getPos()
		ReturnToBase()
		DumpStomach()
		GoToPosition(p)
end


t.df()
while(empty_levels < 3) do -- until there is only air
		for x=1, maxx do
				for y=2, maxy do -- alredy digged 1 block
						t.df()
						if not t.hs() then LOG("emptyRun"); EmptyRun(); end
				end
				local turnfunc = (x % 2 == 1) and t.l or t.r
				turnfunc(); t.df(); turnfunc()
		end
		local z = t.getPos().z
		if turtle.getItemCount(1) == 0 then empty_levels = empty_levels + 1 end
		ReturnToBase()
		DumpStomach()
		LOG("Level " .. z .." done");
		t.ft(0)
		for i=1,z+1 do t.du() end
		t.df()
end

ReturnToBase()


LOG("All done")


——————————————————————————————————————————————————-
libraryaddict #2
Posted 27 April 2012 - 04:41 AM
First off.
Use
 tags
Then spoilers.

nil means you tried to call on something nil.

t.lib:55: attempt to index ? ( a nil value)

You tried to call on something nil at line 55 in file t.lib

I cant see anything at line 55 aside from return true &amp; end.
Those wouldnt error it
But then. I cant view this in the proper way as you didnt use the tags.
tedraic #3
Posted 27 April 2012 - 05:00 AM
Sorry, I don't know what
 tags I am supposed to use nor do I know how to creat a spoiler.. If you could show me how then I can try to edit this post and fix it like you say it should be done. thanks
Luanub #4
Posted 27 April 2012 - 06:42 AM
do this without the *'s for tags

[*spoiler][*code]
insert coding
[*/code][*/spoiler]

Oh and your missing ()'s on line 54 and several other lines


t.tf = t.tryForward

should be

t.tf = t.tryForward()
Edited on 27 April 2012 - 05:44 AM
Cloudy #5
Posted 27 April 2012 - 11:39 AM
Oh and your missing ()'s on line 54 and several other lines


t.tf = t.tryForward

should be

t.tf = t.tryForward()

No, he isn't. That code is correct - he is assigning the value of t.tf to the function t.tryForward so can be called easier.
Luanub #6
Posted 27 April 2012 - 10:10 PM
Okay so if I'm seeing this correct, it looks like you're trying to load t.lib and common so that their functions will be available to flatterrain? Probably the best way to do this in Lua is to load them as API's instead of using shell.run().

Even when you do that doing this something like this t.tf = t.tryForward in the API or lib will still generate the nil error. It will only work if you call it from within the program that it is declared in. Its best to probably just name them what you want to call and then add a comment to the side to make notes so you can remember what they are.

Do your functions like:

function l() -- turn left
				t.a=t.a+1
				t.a = t.a % 4
				t.m = moveMatrix[t.a+1]
				turtle.turnLeft()
end

Then load the api and call the function like this(you will want to rename the file to just t instead of t.lib):

os.loadAPI("t")
t.l()
os.unloadAPI("t")