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

Pausing and resuming loops

Started by ihatetn931, 24 August 2014 - 01:37 AM
ihatetn931 #1
Posted 24 August 2014 - 03:37 AM
Is it possible to stop a while loop when a value hits nil then resuming after it's no longer nil?

What i'm trying to do is, say a chest slot becomes empty, it return "nil".

I want a while loop to stop at the slot id, call a function and put a item in that slot, then resume again.

So….Example

Chest has 6 slots, slot 4 becomes empty and returns "nil" while loop pauses at that slot and calls a function to put soomthing in that slot, once slot is filled the while loop resumes.

I'm using rs.setOutput for a test
I tried


while var do
That just ends the program

Also tried

if var == nil then
	rs.setOutput("top",false)
else
	rs.setOutput("top",true)
end

But that just toggles the redstone off when it hits nil then turns it back on when it goes to next slot
KingofGamesYami #2
Posted 24 August 2014 - 03:51 AM
Easy enough with turtles… I assume this is what you are trying to do?

for i = 1, 16 do
  if turtle.getItemCount( i ) == 0 then
    print( "Place item into slot " .. i )
    while true do --#repeats forever
     os.pullEvent("turtle_inventory")
     if turtle.getItemCount( i ) ~= 0 then
       break --#ends the while loop
     end
    end
  end
end
ihatetn931 #3
Posted 24 August 2014 - 03:56 AM
I'm using a turtle yes, but the slot ids are not in the turtle, they're in a reactor.

Base code i'm using


local reactorLzh = peripheral.wrap("front")
reactorSlots = {1 , 4 , 7 , 9 , 10 , 13 , 16 , 18 , 19 , 22 , 25 , 27 , 28 , 31 , 34 , 36 , 37 , 40 , 43 , 45 , 46 , 49 , 52 , 54}

local r = 1

while true do
	r = r + 1
	if r > 24 then r = 1 end
	local reactor = reactorLzh.getStackInSlot(reactorSlots[r])#--want it so stop when one of these id's is nil
	sleep(0)
end
Edited on 24 August 2014 - 01:57 AM
KingofGamesYami #4
Posted 24 August 2014 - 04:00 AM
Here's a shortened and improved version of what you have.

while true do
  for _, r in ipairs( reactorSlots ) do
    while not reactorLzh.getStackInSlot( r ) do
      print( "nothing in slot " .. r )
      sleep( 1 )
    end
    sleep( 0 )
  end
end
Edited on 24 August 2014 - 02:11 AM
ihatetn931 #5
Posted 24 August 2014 - 04:08 AM
Oh whoa i didn't even know that was possible, also didn't think it would be that easy lol, gonna give it a try right now.


Edit.

Ran in a little bit of an issue.

When i press ctrl+t or ctrl+s to reboot or shutdown the turtle it gives me an error


nil: java.lang.InterruptedException


while true do
	for _, r in ipairs( reactorSlots ) do
		while not reactorLzh.getStackInSlot( r ) do
			clearScreen()
			print( "nothing in slot " .. r )
			rs.setOutput("top",false)
			sleep(0)
		end
		if reactorLzh.getStackInSlot( r ) ~= nil then
			rs.setOutput("top",true)
		end
			clearScreen()
			print(r)
			sleep( 0 )
	end
end

I even tried

while true do
	for _, r in ipairs( reactorSlots ) do
		if reactorLzh.getStackInSlot( r ) ~= nil then
			rs.setOutput("top",true)
		end
		while not reactorLzh.getStackInSlot( r ) do
			clearScreen()
			print( "nothing in slot " .. r )
			rs.setOutput("top",false)
			sleep(0)
		end
		clearScreen()
		print(r)
		sleep( 0 )
	end
end
and same issue.

Only way to make the turtle responsive again is logout of the server and back in. I'm using cc 1.57
Edited on 24 August 2014 - 03:11 AM
ihatetn931 #6
Posted 24 August 2014 - 08:03 PM
Oh whoa i didn't even know that was possible, also didn't think it would be that easy lol, gonna give it a try right now.


Edit.

Ran in a little bit of an issue.

When i press ctrl+t or ctrl+s to reboot or shutdown the turtle it gives me an error


nil: java.lang.InterruptedException


while true do
  for _, r in ipairs( reactorSlots ) do
    while not reactorLzh.getStackInSlot( r ) do
      print( "nothing in slot " .. r )
      sleep( 1 )
    end
    sleep( 0 )
  end
end

Only way to make the turtle responsive again is logout of the server and back in. I'm using cc 1.57

Can't figure out this issue, anytime i add a new piece of code i have to restart my game, this piece of code dosen't seem to be very stable with cc 1.57
KingofGamesYami #7
Posted 24 August 2014 - 10:34 PM
java.lang.InterruptedException is a strange error, I've never seen it before. I'd guess the reactorLzh.getStackInslot part causes it, since I've used everything else.
natedogith1 #8
Posted 24 August 2014 - 10:59 PM
I think you get those sort of errors when a peripheral does something weird it's not supposed to (which might be because things like to forget to validate their input), so I'd blame the mod/mod version that adds the peripheral you're trying to use.
Bomb Bloke #9
Posted 25 August 2014 - 03:08 AM
I guess getStackInSlot() is using os.pullEvent(), but should be using os.pullEventRaw() to prevent it from being interrupted.

You can probably work around it by making the script halt when that function isn't active (by having it listen for taps to the q/x/whatever keys). The parallel API does simplify things somewhat here:

local running = true

local function getInput()
	local myEvent
	
	while true do
		myEvent = {os.pullEvent("key")}
		
		if myEvent[2] == keys.q or myEvent[2] == keys.x then
			running = false
		end
	end
end

local function doLoop()
	while running do
		for _, r in ipairs( reactorSlots ) do
			while not reactorLzh.getStackInSlot( r ) do
				print( "nothing in slot " .. r )
				sleep( 1 )
				
				if not running then return end
			end
			sleep( 0 )
			if not running then return end
		end
	end
end

parallel.waitForAny(getInput, doLoop)