Jump to content




ComputerCraft & BigReactor, Auto-Refresh bug



4 replies to this topic

#1 trafgan

  • Members
  • 3 posts

Posted 29 April 2017 - 04:45 PM

Bug:
Hello, the auto refresh of my program does not work (It does not automatically update)

My code:
ReactorName = "BigReactors-Reactor"
Nom = "Mon reactor"
NomColor = colors.yellow
BackGroundColor = colors.lightGray
InfoColor = colors.gray







ecran = peripheral.wrap("monitor_0")
machine = peripheral.find(ReactorName)
print("Loading...")
sleep(1)
print("Systeme by trafgan")

while true do
    ecran.setBackgroundColor(BackGroundColor)
    ecran.clear()
    ecran.setCursorPos(17.5, 1)
    ecran.setTextColor(NomColor)
    ecran.write(Nom)
    ecran.setCursorPos(1, 5)
    ecran.setTextColor(InfoColor)
    ecran.write("Energie Stockee: "..machine.getEnergyStored().." Rf")
    ecran.setCursorPos(1, 6)
    ecran.write("Energie par tick: "..machine.getEnergyProducedLastTick().." Rf/T")
    ecran.setBackgroundColor(colors.red)
    ecran.setCursorPos(9, 12)
    ecran.setTextColor(colors.white)
    ecran.write("Eteindre")
    ecran.setBackgroundColor(colors.green)
    ecran.setCursorPos(1, 12)
    ecran.setTextColor(colors.white)
    ecran.write("Allumer")
    event, side, x, y = os.pullEvent("monitor_touch")
    if x > 1 and x < 7 and y == 12 then
	    ecran.clear()
	    machine.setActive(true)
    end
    if x > 9 and x < 17 and y == 12 then
	    ecran.clear()
	    machine.setActive(false)
    end
    sleep(0.2)
end

Hello, the auto refresh of my program does not work (It does not automatically update)

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,363 posts

Posted 29 April 2017 - 08:02 PM

Moved to Ask a Pro.

Your script waits for a monitor_touch event on each iteration of the loop.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 6,444 posts
  • LocationTasmania (AU)

Posted 30 April 2017 - 03:25 AM

Instead of this sort of thing:

    event, side, x, y = os.pullEvent("monitor_touch")
    if x > 1 and x < 7 and y == 12 then
            ecran.clear()
            machine.setActive(true)
    end
    if x > 9 and x < 17 and y == 12 then
            ecran.clear()
            machine.setActive(false)
    end
    sleep(0.2)

You're really wanting to do something along these lines:

    local myTimer = os.startTimer(0.2)
    event, side, x, y = os.pullEvent()
    if event == "monitor_touch" then
        if x > 1 and x < 7 and y == 12 then
            ecran.clear()
            machine.setActive(true)
        elseif x > 9 and x < 17 and y == 12 then
            ecran.clear()
            machine.setActive(false)
        end
    end

This'll hence allow your loop to repeat on any event, using os.startTimer() to ensure that one's always pending. It could be neatened up a bit to only repeat in response to your timers / clicks (as opposed to all the other event sources, eg rednet messages, keyboard input, etc), though.

I'd say five updates a second is a bit excessive. Personally I'd be going at something like one per five.

#4 trafgan

  • Members
  • 3 posts

Posted 30 April 2017 - 06:46 AM

AAAA thanks :)
I'm going to try

I'm going to try

Okeeey good, Why my screens blink?
Why my screens blink

Edited by Bomb Bloke, 30 April 2017 - 07:28 AM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 6,444 posts
  • LocationTasmania (AU)

Posted 30 April 2017 - 07:31 AM

Because machine.getEnergyStored() and machine.getEnergyProducedLastTick() don't execute immediately. Because you call them while redrawing the screen, the process is slowed enough that you can see it happening.

If you called them before clearing the display, that wouldn't be such a problem:

while true do
    local energy, energyLastTick = machine.getEnergyStored(), machine.getEnergyProducedLastTick()
    ecran.setBackgroundColor(BackGroundColor)
    ecran.clear()
    .
    .
    .
    ecran.write("Energie Stockee: "..energy.." Rf")
    .
    .
    .






2 user(s) are reading this topic

1 members, 1 guests, 0 anonymous users