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

Refueling Function Help

Started by Raron70, 19 December 2012 - 03:02 PM
Raron70 #1
Posted 19 December 2012 - 04:02 PM
hello i am new to computercraft
i made a program that automatic refuel the turtle
but it wont work

here the script


function fuel()
if turtle.getFuelLevel <= 5 then
turtle.select(12)
turtle.refuel(1)
turtle.select(1)
end
end
Edited by
ChunLing #2
Posted 19 December 2012 - 04:14 PM
What happens?
anonimo182 #3
Posted 19 December 2012 - 04:27 PM
hello i am new to computercraft
i made a program that automatic refuel the turtle
but it wont work

here the script

function fuel()
if turtle.getFuelLevel <= 5 then
turtle.select(12)
turtle.refuel(1)
turtle.select(1)
end
end
Don't forget to call the function
remiX #4
Posted 20 December 2012 - 12:28 AM
I think turtle.refuel(1) only uses one of the items in the slot, remove the 1 to use all the items in the desired slot. But you didn't really state what happens
Raron70 #5
Posted 20 December 2012 - 05:23 PM
oh the turtle wont fuel it self
Raron70 #6
Posted 20 December 2012 - 05:31 PM
one more thing how to add code tags?
theoriginalbit #7
Posted 20 December 2012 - 05:32 PM
use

without the spaces of course. :)/>
Orwell #8
Posted 20 December 2012 - 05:33 PM
put under the code and
 above. (I give them in that order so that I don't trigger it myself).

Edit: you ninja...
Lyqyd #9
Posted 20 December 2012 - 06:09 PM
Come now, gents, surely it's not that hard to simply type [co<b></b>de] and , is it?
ChunLing #10
Posted 20 December 2012 - 06:16 PM
Clever use of empty tags, there.

Okay, the expected behavior of this function is that, when you call it, if the turtle has a fuel level of 5 or less, it will select slot 12 and attempt to burn one item in that slot as fuel, then it will select slot 1.

So, some questions: when do you call the function? What is the fuel level when you call it? What is in slot 12? Does any of it get used? Does the selection return to slot 1? We need to know before we can identify what has gone wrong.
Raron70 #11
Posted 20 December 2012 - 06:26 PM
when do you call the function = at the very top ,fuel level = 0, slot 12= coal , does any coal it get use = no,does the selection return to slot 1 = yes
Lyqyd #12
Posted 20 December 2012 - 06:31 PM
You have to declare the function before you can call it. For instance, if your function was:


function goForward()
  turtle.forward()
end

You couldn't call the function before you declared it, you could only call it afterward:


goForward() --this tries to call the function, but it has not been defined yet, so it would cause an error.

function goForward()
  turtle.forward()
end
--Now we have defined the function.

goForward() --this calls the function successfully.  Of course, the program wouldn't actually make it here with the first function call in place, since it would have errored.
ChunLing #13
Posted 20 December 2012 - 07:00 PM
Actually, it only errors if the function were declared local. If the function is declared globally, the identifier and type is reserved first, so there's no error. But, the function isn't defined until the definition, so it's an empty function call.

There are uses for this, and it makes Lua more "forgiving". I've used it in the past to good effect…but there's really nothing you can do with it that you can't do by using a local declaration and then defining the function later.

Anyway, this is probably one of those cases where posting the full code first would have helped.
Lyqyd #14
Posted 20 December 2012 - 07:01 PM
Not sure what you mean, ChunLing. Do you disagree that in my example code, the first instance of calling goForward() would error?
ChunLing #15
Posted 20 December 2012 - 07:18 PM
Hmm…I'm not sure. I know you could define a function that used goForward, and then define goForward later, and it would work if goForward were defined global rather than local. We should see the whole code, because that isn't erroring but also isn't working.
Orwell #16
Posted 21 December 2012 - 06:26 AM
Lyqyd's code would indeed error because of calling a nil value. A lua code is just a function, so everything gets called sequential. This means that a function needs to be defined before calling it. For example, this would work:

function a()
  b()
end

function b()
end

a()
Because b was defined before a was ever executed. But this won't work:

function a()
  b()
end

a()

function b()
end
Because b wasn't defined when a is first executed.
Lyqyd #17
Posted 21 December 2012 - 06:34 AM
Neither of your examples work, IIRC. I believe you need to do something vaguely thus:


local b

function a()
  b()
end

function b()
  print("Hello World!")
end

a()
ChunLing #18
Posted 21 December 2012 - 06:35 AM
We need to see the full code, because that isn't reported to be giving an error.

Unless this is fixed, in which case I suppose it will just have to remain a mystery.

Note: Orwell's first example does work, that's the case I described (and have used). It works fine to put the local declaration at the top and then do the definition later, but if the scope is global then there is no need to declare first.
Edited on 21 December 2012 - 05:39 AM
Orwell #19
Posted 21 December 2012 - 06:38 AM
Neither of your examples work, IIRC. I believe you need to do something vaguely thus:


local b

function a()
  b()
end

function b()
  print("Hello World!")
end

a()
You don't need forward declaration in this case, it works perfectly for me in CC Emu.
Lyqyd #20
Posted 21 December 2012 - 06:41 AM
Ah, interesting. Wonder why I had thought that, then.
ChunLing #21
Posted 21 December 2012 - 07:18 AM
Probably because scope and allocation are usually (not necessarily always) tied together in good programming. Using a global definition just to avoid clearly scoped declaration is a bit sloppy, but it works.
Raron70 #22
Posted 21 December 2012 - 06:07 PM
ok i found the problem i did wrong

i did this


function fuel
end

while true do
turtle.dig()
turtle.forward()
turtle.digUp()

end

if turtle.getFuelLevel() <= 5 then
turtle.select(12)
turtle.refuel(1)
turtle.select(1)
end
end

thank for all your help
ChunLing #23
Posted 21 December 2012 - 06:47 PM
How was that not throwing an error? Oh well, if it's working now it's working.
Raron70 #24
Posted 22 December 2012 - 04:11 PM
i meant before i was doing that
ChunLing #25
Posted 22 December 2012 - 04:53 PM
Yeah, that wouldn't have worked.