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

Does break work as intended?

Started by LeQwasd, 17 April 2013 - 11:30 PM
LeQwasd #1
Posted 18 April 2013 - 01:30 AM
Hello, my first post, my first real application for turtle. For XP turtle! I am using ultimate pack.
So i wrote my program to enchant stuff when turtle has enough XP.
http://pastebin.com/jHgxjdqT
Here is the code.
Got infinite loop running and checking if xp is enough, if items are in inventory. I see strange behavior - my application terminates in

if isSlotStack(1) then
block. My concerns are - there are function in which i use break. To stop looping for loop. I feel like it is breaking not only for loop, but entire while true loop.
Could someone check my program and maybe point some mistakes?
Mads #2
Posted 18 April 2013 - 02:29 AM
The breaks shouldn't be an issue, but try replacing the with return.
Ray_Anor #3
Posted 18 April 2013 - 02:43 AM
1. Better use local count = ……. in your functions, but this is not main error. :)/>
2. As in 1st p = peripheral.wrap("right") better to do local p = peripheral.wrap("right") , but this is not main error too :D/>
2. elseif isSlotSingle then must be a elseif isSlotSingle(1) then this is first crash error of your code ;)/>
3. elseif isSlotEmpty then must be too elseif isSlotEmpty(1) then this is second crash error of your code B)/>
after that corrections all must start working… maybe :rolleyes:/>
Ray_Anor #4
Posted 18 April 2013 - 02:45 AM
and YES… break may be changed to return too
theoriginalbit #5
Posted 18 April 2013 - 03:49 AM
2. elseif isSlotSingle then must be a elseif isSlotSingle(1) then this is first crash error of your code ;)/>
3. elseif isSlotEmpty then must be too elseif isSlotEmpty(1) then this is second crash error of your code B)/>
they shouldn't cause crashes, as it checks if the function exists, which it does, so it evaluates the if statement as true. So not a crash, just will have unexpected results… also you have 2 #2's :P/>

Now for OP.
With all your if statements where you check the conditional (which is true/false) then all you do is return a boolean (true/false), please, please, please just do this

return count == 0
instead of

if count == 0 then return true
  else return false
end
repeating for all the isX functions
actually functions like


function isSlotEmpty(slot)
  count = turtle.getItemCount(slot)
  if count == 0 then return true
    else return false
  end
end
could just become


function isSlotEmpty(slot)
  return turtle.getItemCount(slot) == 0
end

also you may want to put your declaration for the p variable at the top of the code, as it may cause problems with the isEnoughLevels function
otherwise, no there shouldn't be any problems with those breaks, but as Mads said try return. Is there an error message when the program stops?
Ray_Anor #6
Posted 18 April 2013 - 10:08 AM
they shouldn't cause crashes
logical crashes dude! LOGICAL!!! <_</>