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

Is it possible to run two loops in one file?

Started by MarcoPolo0306, 17 April 2018 - 10:46 PM
MarcoPolo0306 #1
Posted 18 April 2018 - 12:46 AM
For example, having buttons at the same time as running a textbox, time shower, etc.

Thanks!
CLNinja #2
Posted 18 April 2018 - 12:58 AM
http://www.computercraft.info/wiki/Parallel_(API)
Luca_S #3
Posted 18 April 2018 - 05:24 PM
Also if you just need to capture multiple events(e.g. Click event for Buttons, Key events for textboxes, timer events, etc.) you can use something like this:

while true do
  e = {os.pullEvent()}
  if e[1] == "key" then
	--Handle key event
	local key = e[2]
  elseif e[1] == "timer" then
	--Handle timer event
	local timerID = e[2]
  end
  --ETC.
end
Edited on 20 April 2018 - 04:05 PM
Jummit #4
Posted 18 April 2018 - 06:08 PM
Also if you just need to capture multiple events(e.g. Click event for Buttons, Key events for textboxes, timer events, etc.) you can use something like this:

while true do
  e = {os.pullEvent}
  if e[1] == "key" then
	--Handle key event
	local key = e[2]
  elseif e[1] == "timer" then
	--Handle timer event
	local timerID = e[2]
  end
  --ETC.
end
The os.pullEvent stuff makes no sense.
  1. you need to call the function, not just use the function in the table
  2. this function will only get the event that you specify as argument, and you didn't specify one.
  3. I think you want to use os.pullEventRaw to get every event type without filter
Don't take this personally ;)/>, its just a critic on the code.
Edited on 18 April 2018 - 04:09 PM
Dog #5
Posted 18 April 2018 - 06:25 PM
Also if you just need to capture multiple events(e.g. Click event for Buttons, Key events for textboxes, timer events, etc.) you can use something like this:

while true do
  e = {os.pullEvent}
  if e[1] == "key" then
	--Handle key event
	local key = e[2]
  elseif e[1] == "timer" then
	--Handle timer event
	local timerID = e[2]
  end
  --ETC.
end
The os.pullEvent stuff makes no sense.
  1. you need to call the function, not just use the function in the table
  2. this function will only get the event that you specify as argument, and you didn't specify one.
  3. I think you want to use os.pullEventRaw to get every event type without filter
Don't take this personally ;)/>, its just a critic on the code.

The only error I see in Luca_S' code was the lack of brackets after os.pullEvent. It should look like this…

local e = { os.pullEvent() }

I'm not sure I understand your second point, but if I am understanding correctly then I think you are misunderstanding what is happening with the code. Using e as a table to capture all the values returned by os.pullEvent() is perfectly valid and would work fine as posted in Luca_S' code block. I prefer to use separate variables, but capturing everything in a table works just as well. os.pullEvent() doesn't require an event/filter be specified.

As for capturing all events without filter (except terminate events), you don't need os.pullEventRaw - that's only ever necessary (as far as I know) if you need/want to capture or ignore terminate events. For most cases, os.pullEvent() is fine for pulling events without using a filter.

My apologies if I've misunderstood what you were saying.


Having said all that - without having a better idea of what OP is trying to do, I can't say which approach would be better (parallel api or just a looped event listener).
Edited on 18 April 2018 - 04:28 PM
Bomb Bloke #6
Posted 19 April 2018 - 10:53 AM
As for capturing all events without filter (except terminate events), you don't need os.pullEventRaw - that's only ever necessary (as far as I know) if you need/want to capture or ignore terminate events.

Just to be clear on this, while it's true that both os.pullEvent() and os.pullEventRaw() may return most event types when called without a filter, os.pullEventRaw() doesn't "ignore" terminate events. In fact, if they show up in the event queue, it'll return them even if you pass in a filter asking for a different type!
Luca_S #7
Posted 20 April 2018 - 06:07 PM
Also if you just need to capture multiple events(e.g. Click event for Buttons, Key events for textboxes, timer events, etc.) you can use something like this:

while true do
  e = {os.pullEvent}
  if e[1] == "key" then
	--Handle key event
	local key = e[2]
  elseif e[1] == "timer" then
	--Handle timer event
	local timerID = e[2]
  end
  --ETC.
end
The os.pullEvent stuff makes no sense.
  1. you need to call the function, not just use the function in the table
  2. this function will only get the event that you specify as argument, and you didn't specify one.
  3. I think you want to use os.pullEventRaw to get every event type without filter
Don't take this personally ;)/>, its just a critic on the code.
Added the brackets, thanks for pointing that out. To be honest, I think your other points show that you lack an understanding of what the difference between os.pullEvent and os.pullEventRaw is.

I added this solution because in some cases this might be better than using the Parallel API as was suggested before.
Jummit #8
Posted 21 April 2018 - 06:45 AM
To be honest, I think your other points show that you lack an understanding of what the difference between os.pullEvent and os.pullEventRaw is.
Yeah, I was completely wrong about the os.pullEvent stuff. It just didn't make much sense to me that there where two functions that do the exact same thing, just that os.pullEventRaw gets terminate events too. I thought os.pullEventRaw gets events raw without a filter, and os.pullEvent gets a specifc event.
Edited on 21 April 2018 - 04:46 AM