Page 1 of 1

Simple lightweight variable change detector

Posted: Wed Feb 28, 2024 5:09 pm
by noideawhoiam3855
I've made a simple, lightweight, variable change detector. Using tables to check if a variable is different from its most recently saved value, should work with true & false, numbers, strings, etc. If there are any errors, let me know.
(Note the image is a separate version which uses print to show the result; this is meant to be used as a function you can either put in a lua file or use in require, and as such returns a true or false on change detection)

Re: Simple lightweight variable change detector

Posted: Wed Feb 28, 2024 11:24 pm
by BrotSagtMist
There is nothing simple nor lightweight about this.
Inserting and removing in a table like that is the very thing you try to avoid with such checks. Because that is really adding cpu usage.
Here is a lighter version that works without.

Code: Select all

do
 local switch
 function detectChange(v)
  if switch==v then 
   return false, v, v
  else 
   local _ =switch
   switch=v  
   return true, _,v
  end
 end
end