This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Real use of resident 0 script?
#1
A key feature of the resident scripts with 0 second pause is that their global variables are memorized from one execution to the next one. Knowing this, if we want to have a script that always does the same thing over and over, we have two options:

Option 1 - Each execution of the script terminates and then passes the values of the global variables to the next execution

Code:
if MY_SCRIPT == nil then
  -- initialization
  MY_SCRIPT = {}
  MY_SCRIPT.foo1 = value1
  MY_SCRIPT.foo2 = value2
  MY_SCRIPT.loop = function()
    -- do some stuff
  end
end

MY_SCRIPT.loop()
-- one loop execution runs, then the script ends,
-- and because it's a resident 0, it will restart itself
-- immediately, with the variable MY_SCRIPT still in memory,
-- so the loop function will run again



Option 2 - There is only one execution of the script, it runs forever

Code:
-- initialization
MY_SCRIPT = {}
MY_SCRIPT.foo1 = value1
MY_SCRIPT.foo2 = value2
MY_SCRIPT.loop = function()
  -- do some stuff
end

-- we loop forever, so it's always the same instance of the script running
while true do
  MY_SCRIPT.loop()
end


Can you explain to me which of the two options is the most efficient and the most CPU-friendly, and tell me why?
Reply
#2
The second option is a tiny bit better but it does not really matter.

Previously "while true" loops were prohibited in resident scripts, because scripts were reloaded on the fly after saving changes. Now scripts are fully restarted so infinite loops are allowed.
Reply


Forum Jump: