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

Creating an if/then or repeat within a function

Started by Boshwah, 23 December 2012 - 03:38 PM
Boshwah #1
Posted 23 December 2012 - 04:38 PM
Hi, I am new to lua code, and was wondering if I can make an If/then statement within a function.
Right now I am trying to make a turtle refuel itself within a program.
Here is my code so far:

function fuel() return
if turtle.getFuelLevel() < 300 then
  turtle.refuel(1)
  fuel()
else
  print("Fueled Up!")
end
end

print "Checking Fuel..."
fuel()

Or I was thinking of loops in VB language and came across the repeat, so here is another idea:

function fuel() return
repeat turtle.refuel(1) until
  turtle.getFuelLevel() > 300
end
print("Checking Fuel")
fuel()
print("Fueled Up!")

So, first off, can I put an if/then or repeat statement within a function? If so how would I correct my code?
Thank you in advance!
ChunLing #2
Posted 23 December 2012 - 04:49 PM
Yes, you can!

But neither of your functions currently do anything because you are returning immediately. Nothing after the return gets executed.
Boshwah #3
Posted 23 December 2012 - 05:07 PM
Oh, so if I remove the return they would work? I guess that makes sense, now that I think about it more. I was looking up how to write lua and such and the example was:

function foo(n) return n*2
or something close to that…

Return would be used to make it come back with a value such as turtle.getFuelLevel(), correct?
Fizzgig #4
Posted 23 December 2012 - 05:14 PM
Yes you can most defiently use If statements and loops inside functions and nest them any other way you can think of!

for your first example you will need to remove the return directly after the function name. This will make the function do nothing at all. The rest of it is basically correct however, you may want to think about selecting a fule slot? unles you intend it to be in the default slot. It is also not really great practice to recurse the function itself. it will technically work but it is unnecessary.

your second example is a much better choice with a repeat loop in there. I will correct the code alittle for you:

function fuel() --return (do not return here or else the code wont do anything)
  repeat
	turtle.refuel(1)
  until turtle.getFuelLevel() > 300
  return true --(it is good practice to return a bool)
end

print("Checking Fuel")
fuel()
print("Fueled Up!")
as you can see the repeat loop is alittle different from vb style but it is basically the same thing. If you return a bool like i suggested in the code you could attempt something like this:

if fuel() then print("Fueled Up!") end
since the bool is added to the function the if statement will return true

not the best example of bool returns since the function has no error checking to return a false..however i hope it gives you the right idea.
Boshwah #5
Posted 23 December 2012 - 05:16 PM
Alright, thank you! That cleared up a lot for me!
ChunLing #6
Posted 23 December 2012 - 05:25 PM
I remember that movie!