Simple lightweight variable change detector

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
noideawhoiam3855
Prole
Posts: 1
Joined: Wed Feb 28, 2024 4:59 pm

Simple lightweight variable change detector

Post 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)
Attachments
main.lua
(464 Bytes) Downloaded 167 times
changeDetect.png
changeDetect.png (103.37 KiB) Viewed 3640 times
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Simple lightweight variable change detector

Post 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
obey
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests