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

event within while loop

Started by Kiomadria, 10 January 2015 - 02:33 AM
Kiomadria #1
Posted 10 January 2015 - 03:33 AM
I am attempting to write a program to use computercraft, monitors and bloodmagic. I have it set that when a particalar slate is detected it will move it out to the chest.

I am using a while loop to get it to keep checking but the problem I have is I either have to hit control in the terminal to step the while loop or I can't change the slate on the monitor

code:

function blankSlate()
	button.toggleButton("Blank Slate")
	repeat
	state = button.getState("Blank Slate")
		contents = altar.getAllStacks()
		for i,j in pairs(contents) do
			slot = i
			name = j["display_name"]
			print(name)
		end
	
		if name == "Blank Slate" then
			altar.pushItem("west", 1, 1)
		end
		sleep(1)
	until state ~="Blank Slate"
	event, side, x, y= os.pullEvent()
	if event == "monitor_touch" then
		button.checkxy(x,y)
	end
end
I suspect I need a if statement that checks for a pullevent: monitor_touch but I can't figure out how to put that in an in if statement.

Any ideas?

Thanks
HPWebcamAble #2
Posted 10 January 2015 - 07:27 AM
Whose button API do you use here?
And is this the full code? That would really help.

Without the full code, I really don't know, but this is what I might do:


function blankSlate()
  while true do
	event, side, x, y= os.pullEvent()
	if event == "monitor_touch" then
      button.checkxy(x,y)
	end
  end
end
Bomb Bloke #3
Posted 10 January 2015 - 07:41 AM
I suspect I need a if statement that checks for a pullevent: monitor_touch but I can't figure out how to put that in an in if statement.

You've already got that: what you want to add is a check for a different event, one that you know will occur on a regular basis. This is where timers come in:

function blankSlate()
	button.toggleButton("Blank Slate")
	repeat
		state = button.getState("Blank Slate")
		contents = altar.getAllStacks()
		for i,j in pairs(contents) do
			slot = i
			name = j["display_name"]
			print(name)
		end

		if name == "Blank Slate" then
			altar.pushItem("west", 1, 1)
		end
		
		local myTimer = os.startTimer(1)
		
		while true do
			local myEvent = {os.pullEvent()}
			if myEvent[1] == "monitor_touch" then
				button.checkxy(myEvent[3],myEvent[4])
			elseif myEvent[1] == "timer" and myEvent[2] == myTimer then
				break
			end
		end
	until state ~="Blank Slate"
end
Kiomadria #4
Posted 10 January 2015 - 05:26 PM
Thanks Bomb Bloke the timer was exctually what I needed.

Just to futher my education, buecause all my recent programing experience has been python and most of that was do screen scrapes, why put it in a while true loop?

Also I am using DW20's button API.
Bomb Bloke #5
Posted 10 January 2015 - 09:22 PM
If you're asking "why keep looping there", it's because lots of different types of events could occur - we need to keep checking them until we get the ones we want.

If you're asking "why that specific loop", it doesn't really matter. You could do much the same thing with a repeat loop for eg, like this:

.
.
.
                local myTimer = os.startTimer(1)

                repeat
                        local myEvent = {os.pullEvent()}
                        if myEvent[1] == "monitor_touch" then button.checkxy(myEvent[3],myEvent[4]) end
                until myEvent[1] == "timer" and myEvent[2] == myTimer
.
.
.