Jump to content




Pausing and resuming loops


  • You cannot reply to this topic
8 replies to this topic

#1 ihatetn931

  • Members
  • 75 posts
  • LocationCookeville, Tn

Posted 24 August 2014 - 02: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

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 24 August 2014 - 02: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


#3 ihatetn931

  • Members
  • 75 posts
  • LocationCookeville, Tn

Posted 24 August 2014 - 02: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 by ihatetn931, 24 August 2014 - 02:57 AM.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 24 August 2014 - 03: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 by KingofGamesYami, 24 August 2014 - 03:11 AM.


#5 ihatetn931

  • Members
  • 75 posts
  • LocationCookeville, Tn

Posted 24 August 2014 - 03: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 by ihatetn931, 24 August 2014 - 04:11 AM.


#6 ihatetn931

  • Members
  • 75 posts
  • LocationCookeville, Tn

Posted 24 August 2014 - 07:03 PM

 ihatetn931, on 24 August 2014 - 03:08 AM, said:

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

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 24 August 2014 - 09: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.

#8 natedogith1

  • Members
  • 110 posts

Posted 24 August 2014 - 09: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.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 August 2014 - 02: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)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users