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

Getting into Lua

Started by cardmime, 04 October 2012 - 09:26 PM
cardmime #1
Posted 04 October 2012 - 11:26 PM
Okay so I'm new here, Hi! Anyways I just recently found the computercraft mod and I'm loving the idea of it. I've never programmed in Lua before today just Java and C++. I tried giving Lua a shot for the first time today and here's what I wrote:



function refuel()
if turtle.getFuelLevel()<10 then
turtle.refuel(4)
end
return true
end
function dig()
if turtle.compare() then
turtle.dig()
end
return true
end
while not turtle.detect() do
refuel()
dig()
turtle.forward()
end

I tried running the program on my turtle and I keep getting "attempt to call nil" and I can't figure out what it means. I even tried rewriting it a couple other ways. Anyways if anyone is willing to help me out a bit it'd be much appreciated. I'll see ya'll around,

- Cardmime
Cranium #2
Posted 04 October 2012 - 11:30 PM
What is the number for "attempt to call nil"?
cardmime #3
Posted 04 October 2012 - 11:52 PM
It's 2.
Kingdaro #4
Posted 05 October 2012 - 12:00 AM
Alright, I see the problem.

The functions in the turtle API are global, and when you overwrite them, you won't be able to use them anymore.

All you have to do is make your functions local:

local function refuel()
if turtle.getFuelLevel()<10 then
turtle.refuel(4)
end
return true
end
local function dig()
if turtle.compare() then
turtle.dig()
end
return true
end
while not turtle.detect() do
refuel()
dig()
turtle.forward()
end
cardmime #5
Posted 05 October 2012 - 12:05 AM
Alright, I see the problem.

The functions in the turtle API are global, and when you overwrite them, you won't be able to use them anymore.

All you have to do is make your functions local:

local function refuel()
if turtle.getFuelLevel()<10 then
turtle.refuel(4)
end
return true
end
local function dig()
if turtle.compare() then
turtle.dig()
end
return true
end
while not turtle.detect() do
refuel()
dig()
turtle.forward()
end

Changing them to local didn't work I still get the nil problem… Thanks for trying though.
Orwell #6
Posted 05 October 2012 - 12:08 AM
Alright, I see the problem.

The functions in the turtle API are global, and when you overwrite them, you won't be able to use them anymore.

All you have to do is make your functions local:

local function refuel()
if turtle.getFuelLevel()<10 then
turtle.refuel(4)
end
return true
end
local function dig()
if turtle.compare() then
turtle.dig()
end
return true
end
while not turtle.detect() do
refuel()
dig()
turtle.forward()
end

That's wrong. The metatable 'turtle' is defined global, not the functions in it.

Cardmime, you're program runs just fine for me. Are you using CC 1.41 or higher?
MysticT #7
Posted 05 October 2012 - 12:08 AM
What version of ComputerCraft are you using? Fuel was added in 1.4, so if you're using an older version the getFuelLevel and refuel functions doesn't exist.
Kingdaro #8
Posted 05 October 2012 - 12:23 AM
That's wrong. The metatable 'turtle' is defined global, not the functions in it.
I used to think this too, before I took a look at some of the APIs:


function waitForAny( ... )
    local routines = { create( ... ) }
    return runUntilLimit( routines, #routines - 1 )
end

function waitForAll( ... )
    local routines = { create( ... ) }
runUntilLimit( routines, 0 )
end
MysticT #9
Posted 05 October 2012 - 12:26 AM
That's wrong. The metatable 'turtle' is defined global, not the functions in it.
I used to think this too, before I took a look at some of the APIs:


function waitForAny( ... )
	local routines = { create( ... ) }
	return runUntilLimit( routines, #routines - 1 )
end

function waitForAll( ... )
	local routines = { create( ... ) }
runUntilLimit( routines, 0 )
end
Yes, they are global, but when loaded (with os.loadAPI) in an environment that is then copied to a global table (in this case turtle), so the global variables (and functions, since they are also variables) in the file are stored in that table.
cardmime #10
Posted 05 October 2012 - 01:36 AM
I'm using Tekkit for Computercraft and I just looked and I guess it's only v1.33 so I guess that explains the problem, haha. Thank you! I'll have to add the mod so I can mess around with all the new stuff.
ChaddJackson12 #11
Posted 05 October 2012 - 02:24 AM
Alright, I see the problem.

The functions in the turtle API are global, and when you overwrite them, you won't be able to use them anymore.

All you have to do is make your functions local:

local function refuel()
if turtle.getFuelLevel()<10 then
turtle.refuel(4)
end
return true
end
local function dig()
if turtle.compare() then
turtle.dig()
end
return true
end
while not turtle.detect() do
refuel()
dig()
turtle.forward()
end
How does this change anything? I mean local does something, but what? Also, I have tried doing both, if I do a local function, this problem happens… If I have 2 functions call each other at some point in time, one has to be above the one that calls it, if one is local. If you don't make them local then this doesn't happen. I don't know why, but it is weird. :(/>/>
Fatal_Exception #12
Posted 05 October 2012 - 02:30 AM
http://www.lua.org/pil/4.2.html

"It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones."