LogicMachine Forum
Real use of resident 0 script? - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Real use of resident 0 script? (/showthread.php?tid=6212)



Real use of resident 0 script? - rbourgeon - 11.12.2025

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?


RE: Real use of resident 0 script? - admin - 11.12.2025

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.