Posted 03 September 2012 - 09:13 PM
Hello,
I've just started getting into Lua, the last real ('real') programming I did was back in high school on my TI-83.
I'm looking forward to making my turtles do all kinds of crazy stuff.
I'm having problems getting a while loop to work properly
The idea I'm currently working with is using a Turtle as a resource manager in a tube system.
The turtle has to divide the items it gets in a 1:7 ratio and if it gets a redstone signal (possibly rednet, but I'd rather have both options), it will have to step forward, thus breaking the loop. I thought of doing the 1:7 ratio with giving redstone signals to connected transposers. So, in the example below, it has a transposer to it's left and to it's back and is getting input from a (redstone) tube on the right.
I'm having problems, because If I understand correctly I can't use pullEvent() as that yields (pauses, right?) the entire program. And for some reason, every time I add sleep() to my program it just doesn't do anything anymore. I tried a couple of different codes, one seemed to acutally be working (it cleaned out the inventory in the correct ratio), except when the items were put in too fast (it only checked the first inventory spot), so I went to work with it again, but now I can't find back what the working one was..
TL;DR
Sleep() breaks my program for some reason, but I need the while loop
EDIT: With a sleep between the rs.setOutputs and a sleep after the while, it does not go too long without yielding, but it splits the items evenly instead of 7:1…
Also I can't seem to find the code on my Mac.. I found the map with the API's and such, but my code does not seem to show up there…
I've just started getting into Lua, the last real ('real') programming I did was back in high school on my TI-83.
I'm looking forward to making my turtles do all kinds of crazy stuff.
I'm having problems getting a while loop to work properly
The idea I'm currently working with is using a Turtle as a resource manager in a tube system.
The turtle has to divide the items it gets in a 1:7 ratio and if it gets a redstone signal (possibly rednet, but I'd rather have both options), it will have to step forward, thus breaking the loop. I thought of doing the 1:7 ratio with giving redstone signals to connected transposers. So, in the example below, it has a transposer to it's left and to it's back and is getting input from a (redstone) tube on the right.
I'm having problems, because If I understand correctly I can't use pullEvent() as that yields (pauses, right?) the entire program. And for some reason, every time I add sleep() to my program it just doesn't do anything anymore. I tried a couple of different codes, one seemed to acutally be working (it cleaned out the inventory in the correct ratio), except when the items were put in too fast (it only checked the first inventory spot), so I went to work with it again, but now I can't find back what the working one was..
TL;DR
Sleep() breaks my program for some reason, but I need the while loop
EDIT: With a sleep between the rs.setOutputs and a sleep after the while, it does not go too long without yielding, but it splits the items evenly instead of 7:1…
x=math.random(8) --randomness not really important
function alot()
if x<8 then --pulse the left transposer
rs.setOutput("left", true)
rs.setOutput("left" false)
x=x+1
else --back transposer, reset counter
rs.setOutput("back",true)
rs.setOutput("back",false)
x=1
end
end
while true do
sleep(0)
for n=1,9 do --check if there is something in the inventory
if turtle.getItemCount(n)>0 then
alot()
end
end
if rs.getInput("right") then --look for redstone signal
break
end
end
turtle.forward()
Also I can't seem to find the code on my Mac.. I found the map with the API's and such, but my code does not seem to show up there…