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

[Solved] menu becoming laggy after a while

Started by Kuchiha, 23 April 2013 - 06:49 PM
Kuchiha #1
Posted 23 April 2013 - 08:49 PM
Hi all,

I'm new in lua and computercraft. After reading many informations in the wiki, i've wrote a little program to pilot my nuclear reactor.

Here is the code (uncommented) : http://pastebin.com/QTjVeYuA

After a while this program is running, the menu become laggy.

Some infos :
m refers to the wireless modem
r refers to the nuclear information reader
cb refers to the chat box

the menu has 3 items :
"Demarrer" for starting the nuclear reactor
"Arreter" for stopping it
"Quitter" to stop the program

In the main loop, I use this line to force an event for checking the heat of the reactor :
os.startTimer(0.2)

I think that the "lag" comes from here, but I don't know how to avoid this and keep checking the heat, even if a key is not stroke.

if someone can help me understand why it generate lag and how to avoid that, it will be great.
Lyqyd #2
Posted 24 April 2013 - 04:40 AM
Split into new topic.
remiX #3
Posted 24 April 2013 - 05:37 AM
After how long does it start lagging? I left it for about a minute and it was fine.

Maybe set the delay to 1 second and rather do it this way:
local selector = 1
local heatChecker = os.startTimer(1) -- start a timer for 1 second to check the heat of reactor.

while true do
  menuDraw(selector)
  local e = {os.pullEvent()}
  if e[1] == "key" then
    if e[2] == keys.down then
      selector = selector < #menu_options and selector+1 or 1
    elseif e[2] == keys.up then
      selector = selector > 1 and selector-1 or #menu_options
    elseif e[2] == keys.enter then
      if selector == 1 then
        m.transmit(1, 1, "start")
        term.setTextColor(colors.green)
        term.setCursorPos(1, termY)
        term.write("Le reacteur est demarre.     ")
      elseif selector == 2 then
        m.transmit(1, 1,"stop")
        term.setTextColor(colors.red)
        term.setCursorPos(1, termY)
        term.write("Le reacteur est arrete.      ")
      elseif selector == 3 then
        break
      end
    end
  elseif e[1] == "timer" and e[2] == heatChecker then
    heatChecker = os.startTimer(1)
    if verifheat() then
      error = "Mauvaise carte dans le lecteur !"
      break
    end
  end
end
Change the part from local selector = 1 in your code to the end of the while loop to this
Kuchiha #4
Posted 24 April 2013 - 05:43 AM
Thx for the answer, I've got some lag after about 5-10 minutes (on a server).

Gonna try your fix asap. I was also thinking to do it with coroutines. Does it worth it ?
remiX #5
Posted 24 April 2013 - 06:10 AM
Oh quite a few minutes.. hmm..

I haven't really worked with coroutines so not sure.

Btw I had the os.startTimer(0.2) still in the loop, don't forget to remove it :P/>
Kuchiha #6
Posted 24 April 2013 - 08:12 AM
Tried your version for an hour, no more lags :D/>

Thx remiX :)/>