Jump to content




[Solved] menu becoming laggy after a while


  • You cannot reply to this topic
5 replies to this topic

#1 Kuchiha

  • Members
  • 3 posts

Posted 23 April 2013 - 07: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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 April 2013 - 03:40 AM

Split into new topic.

#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 24 April 2013 - 04: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

#4 Kuchiha

  • Members
  • 3 posts

Posted 24 April 2013 - 04: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 ?

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 24 April 2013 - 05: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

#6 Kuchiha

  • Members
  • 3 posts

Posted 24 April 2013 - 07:12 AM

Tried your version for an hour, no more lags :D

Thx remiX :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users