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

Touchscreen to change 'page'

Started by twoopi, 13 April 2013 - 08:35 AM
twoopi #1
Posted 13 April 2013 - 10:35 AM
Hey guys, I've been messing around the the Nuclear Information Reader from MiscPeripherals (I play Mindcrack FTB) for a few hours, I've decided to set up three 'pages' on info for different systems.

On page 1 I have my various reactors with details, page 2 has my eu/t from my solars, steam turbine, reactors and lightning rod along with MFSU charge stats, and page 3 has my tank details.

I have most of it working to an extent, but I had an issue with the info updating (This is my first time making something with CC), and figured out it was the os.pullevent i use to change pages, so I looked it up and saw that it halts everything until it gets an event. I have a redstone pulse going to it every few seconds at the moment to solve this, but is there another way I can look for touchscreen events without halting the loop to refresh the details on screen?
LBPHacker #2
Posted 13 April 2013 - 10:43 AM
First of all :D/> Away with that redstone pulse. Use timers!

local timer = os.startTimer(1) -- starts a timer (this one will fire in 1 second) (timer now contains the new timer's reference)

while true do -- infinite loop FTW
    local eventData = {os.pullEvent()} -- pull the next event and put everything into a table
    -- eventData[1] is the event
    if eventData[1] == "timer" and eventData[2] == timer then -- timers fire an event called "timer" - what a surprise
        -- os.pullEvent will return the timer's reference with the second value

        -- update stuff here

        timer = os.startTimer(1) -- create a new timer
    end
    if eventData[1] == "monitor_touch" then -- handle monitor_touch

        -- page stuff here

    end
end

I hope you know what to replace –page stuff and –update stuff with. But if you don't, just ask.
twoopi #3
Posted 14 April 2013 - 03:53 AM
That worked wonderfully, thanks :D/>
Sariaz #4
Posted 14 April 2013 - 08:40 PM
Also if having that one second delay was a problem you could have another function running with the parallel api that sets variables for the event when it gets them that your main loop reads to see if they exist every time it refreshes. Since refreshing it more then once per secondish causes annoying flickers for me i wouldn't advise it, but knowledge is power and the more options people have the better I think.
LBPHacker #5
Posted 14 April 2013 - 11:35 PM
-snip-
You mean that he should use two coroutines:
1) the Details screen itself
2) a function that communicates with the Nuclear Information Reader every eg. 0.2 seconds?

In that case, no need of coroutines (parallel API). You just have to set up a 0.2 second timer instead of an 1 second one, process the informations in the loop, and update the screen only if something really happens.

Don't forget, parallel API is your last hope. Use it only if you can't do what you want without it.