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

LUA Parallel Functions Issues

Started by Brucey_Bonus, 15 December 2014 - 08:18 PM
Brucey_Bonus #1
Posted 15 December 2014 - 09:18 PM
Hey guys I'm trying to write a ComputerCraft program that will interface with the BigReactors mod, and am using Direwolf20's touchscreen monitor API to help me do so.

One issue through my programming, is that I am attempting to update the screen with information such as the power generation of the reactor, but the main loop that runs all of my code is waiting for me to press the screen.

Because of this, the generation display only updates once the getClick() function has completed and doesn't display a real-time monitoring.

How can I get both functions to run simultaneously in my while true loop?

Button code:

http://pastebin.com/rzx9hdxh

Startup code:

http://pastebin.com/e1lkmryw



Thanks for any help!
KingofGamesYami #2
Posted 15 December 2014 - 11:51 PM

while true do
   updateActivity()
   updateGeneration()
   fillTable()
   drawInformation()
   getClick() --#this waits for an event
   m.clear() --#this clears the monitor
end

Could you provide a quick summery of the uncommented portion of this code?
Bomb Bloke #3
Posted 16 December 2014 - 12:50 AM
The "simple" fix would be to alter your getClick function to make use of a timer:

local function getClick()
   local timerID = os.startTimer(1)

   while true do
      local event,side,x,y = os.pullEvent()

      if event == "monitor_touch" then
         button.checkxy(x,y)
         break
      elseif event == "timer" and side == timerID then break end
   end
end

In this manner, the function will only wait up to a second before allowing the rest of your script to continue. Note that it is a requirement that the script pause every now and then - due to ComputerCraft's rule of killing any script that doesn't yield at least once every ten seconds - so there's no real benefit in getting the click-listening code to run in "parallel" with the rest of it.
Edited on 15 December 2014 - 11:51 PM
Brucey_Bonus #4
Posted 16 December 2014 - 04:49 PM

while true do
   updateActivity()
   updateGeneration()
   fillTable()
   drawInformation()
   getClick() --#this waits for an event
   m.clear() --#this clears the monitor
end

Could you provide a quick summery of the uncommented portion of this code?

Sure!

So the updateActivity function first draws the state of the reactor, (where it is active or not).
The updateGeneration function draws the power generation of the reactor.
The fillTable function draws out the touchscreen buttons, and
the drawInformation function draws the names of each displayed information, (the activity and generation at the moment) to the left of the drawn values.
hbomb79 #5
Posted 17 December 2014 - 06:21 PM
you can just use an event loop, this is what I did in my security suite reactor program. Simply have a loop listening for events, is the event is a monitor touch then check the coords using a function, and then do whatever you want with that. If it's a timer and the id is refresh then refresh the screen and queue another timer of that same delay, causing an infinite timer loop keeping the screen refreshed. You will also need to have the timer call at the start of the function. when I get on my PC I'll write a code snippet from my reactor program to show you. Or you can look here; https://github.com/hbomb79/securitySystemPro/blob/master/systemFiles/Programs/reactor