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

[LUA] Terminate a loop with a rs signal?

Started by GewoonDaan, 03 September 2012 - 07:13 PM
GewoonDaan #1
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…


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…
OmegaVest #2
Posted 03 September 2012 - 09:23 PM
So, I think sleep has a minimum that it needs to run in a while loop, otherwise I think it's supposed to throw an error.

Also, on a Mac, your programs will be in the world save folder. Look there.
GewoonDaan #3
Posted 03 September 2012 - 09:28 PM
I tried with 0.1…
And it doesn't throw an error, it just does not do anything with the inventory. It only responds to the redstone signal..

And does that mean that in SMP they are on server side? Oh hang on I think I read that somewhere now that I.. ah well..

Well thanks for the quick reply anyway!
GewoonDaan #4
Posted 04 September 2012 - 12:42 AM
If the code looks clean, please comment, too, maybe it's something to do with the transposers rather than the code?
Luanub #5
Posted 04 September 2012 - 02:02 AM
Try using os.pullEvent and a timer.


while true do
local timer = os.startTimer(.1)  -- this will probably need adjusting so it has time to pickup the rs signal.
local evt, p1 = os.pullEvent()
if evt == "timer" then
  for n=1,9 do --check if there is something in the inventory
  if turtle.getItemCount(n)>0 then
   alot()
  end
end
elseif evt == "redstone" and rs.getInput("right") then
  break
end
end
GewoonDaan #6
Posted 04 September 2012 - 10:04 AM
Thanks, sounds like a good idea!
I'll report back when I tried it.

Just to help me with my understanding of Lua; what is the significance of p1 here? Do I need it and what will it's value be in both cases..?
Luanub #7
Posted 04 September 2012 - 10:58 AM
p1 is just the first parameter returned from os.pullEvent(). I put it in there out of habit. Sometimes with timers I'll use it as a redundancy check. You probably don't need it.

If you ever are curious about what it is returning(this will help if you need to debug any) just throw in a print and print it to the screen.