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

Need help with autorunning script efficiently every x seconds

Started by loom123, 29 August 2013 - 10:59 AM
loom123 #1
Posted 29 August 2013 - 12:59 PM
Title: Need help with autorunning script efficiently every x seconds

Im using this script on a server, so I need some efficiency. All solutions I (LUA noob much) tried tend to produce lag or even worse disasters…

Set up:
I want the CCmonitor to show current ammount of spoilsbags (the un-player-inventorized ones) in my AE network.
I have a computer connected to a monitor and a ME Bridge.
Script so far:

--credit to voidreality on FTB forums for the ME Bridge basics
m = peripheral.wrap("bottom")
mon = peripheral.wrap("right")
  mon.clear()
  a = 6297 --spoils bag ID
  x, y = mon.getSize()
  
local function cwrite (text, y)
  cx = math.ceil((x/2)-(string.len(text)/2))
  mon.setCursorPos(cx, y)
  mon.write(text)
end
 
myList = m.listItems()
boo = false
  cwrite("Spoils", y/2 - 1)
  cwrite("Bags:", y/2)   
  for k, v in pairs (myList) do
    if k == a then
	  boo = true
	  cwrite(tostring(v), y/2+2)
    end
  end
  if boo == false then cwrite("0", y/2+2) end

The code shows the spoils bag ammount just fine, but I need it to run every x seconds, which is where I did not succeed.
Any help on how to achieve this or even how to make my current code better is greatly appreciated.
GopherAtl #2
Posted 29 August 2013 - 02:37 PM
you'll want to use os.startTimer, here's a simple example of repeating something at an interval using it…


--loop...
while true do
  --start a 5-second timer
  os.startTimer(5)
  --wait for it
  os.pullEvent("timer")

  --now do whatever you want to repeat
  print("tick")

end
loom123 #3
Posted 29 August 2013 - 03:02 PM
Thank you very much, got it working perfectly :)/>
Engineer #4
Posted 30 August 2013 - 07:01 PM
you'll want to use os.startTimer, here's a simple example of repeating something at an interval using it…


--loop...
while true do
  --start a 5-second timer
  os.startTimer(5)
  --wait for it
  os.pullEvent("timer")

  --now do whatever you want to repeat
  print("tick")

end
Why not just use sleep?
Imred Gemu #5
Posted 30 August 2013 - 08:23 PM
you'll want to use os.startTimer, here's a simple example of repeating something at an interval using it…


--loop...
while true do
  --start a 5-second timer
  os.startTimer(5)
  --wait for it
  os.pullEvent("timer")

  --now do whatever you want to repeat
  print("tick")

end
Why not just use sleep?
That would be simpler to type, but it doesn't really matter, sleep does the same thing internally, and GopherAtl's suggestion isn't hard to type anyway.
GopherAtl #6
Posted 30 August 2013 - 11:05 PM
I never suggest sleep. This way just feels more extensible to me, and introduces os.pullEvent, which is something every cc programmer ought to learn to use as early as possible, and hides sleep, which causes people all kinds of bugs because of it eats messages.
Engineer #7
Posted 31 August 2013 - 08:19 AM
I never suggest sleep. This way just feels more extensible to me, and introduces os.pullEvent, which is something every cc programmer ought to learn to use as early as possible, and hides sleep, which causes people all kinds of bugs because of it eats messages.
Sure, I do agree with you. But the sleep function does exactly do the same, and this is like rewriting a function which already exists. And that for me is so-called inefficient.
GopherAtl #8
Posted 31 August 2013 - 08:45 AM
this is a bit of a tangent, but I've noticed there's an obsession around here with an idea of efficiency that, to me, doesn't seem terribly grounded in reality.

First off, it's one extra line of code. If that's taking you a significant amount of extra time or effort, learn to type faster.

Second, it may be less efficient measured in keystrokes or lines of code - both of which are arbitrary measures that are at best only loosely coupled to the actual time spent working on a program.

Thirdly, efficiency is fine and dandy, but it is not always wise to make efficiency your mission #1. It is one consideration among many.

Lastly, and most importantly, this is the AaP section. You should be posting code in response to people's questions with the primary goal of to introducing and teaching fundamental concepts. You should not be posting code to show off how clever you are. Optimized code - whether optimized for performance, for "efficiency", or byte count, for line count, or for any other arbitrary epeen metrics, is harder to read and understand for someone not already familiar with lua, and so it has no place here, unless the question was "how could I optimize/improve this code."

</rant_tangent>

Rant finished, there's nothing at all wrong with pointing out that he could also use sleep in this case. It's a perfectly valid approach. But the idea that it is somehow more efficient, and in some absolute way better than just doing the timer and pullEvent yourself, is ridiculous.