On Variable Change
September 22, 2016 12:00 AM
Is there a (hopefully simply) way to trigger an action when the value of a variable changes?
Discussion (7)
1. Create two vars: YourVar (the one you are monitoring) and PreviousVar (we will need it later).
2. Create action group with two actions:
- Set PreviousVar to YourVar value
- Whateve action you want fired on variable change
3. Add an "on timer" action, set it to 0.1 seconds, set it to run ActionGroup IF PreviousVar does not equal YourVar.
So now you have a setup that checks for variable change 10 times per second and fires an action of your choice if it does change.
I don't know whether it's possible to directly react to the change of a variable. You can of course react to the new value or do something if the variable is not equal to its initial value (any more).
What kind of variable is it? Radio buttons for example, support the trigger "On: Select / Change", i.e. an action can be triggered by selecting a different button.
Tim
Thanks, that would do it though I'll need to test what the constant polling does to the playback.
Though programmatically it will work, using a setInterval (timer) to continuously poll the variable will eat up resources. What exactly are you trying to accomplish? Why do you need to constantly monitor that variable? See if you can redesign your process so that as Tim posted, "You can react to the new value or do something if the variable is not equal to its initial value (any more)."
Point taken and I agree that on most devices, especially with an interval of 100ms, it shouldn't be a problem. However, people may access the lesson on mobile devices not equipped with a 6th generation Intel i7's running at 3Ghz. Additionally, who knows what unoptimized code Lectora will spit out and if it can lead to issues. Even the i7 seeks to move into low power states. Depending on how labor intensive the code is, it could prevent that from happening (although at an interval of 100ms it's doubtful). My point was that instead of perhaps making a specific issue a general one, it would most likely be better to examine why you're looking to do that in the first place and see if you can figure out a different solution. How often have you found the need to put a poll on a variable?
SetInterval (timers) definitely have their place, especially when working with audio and video cue points, etc.. I just think constantly polling a variable when you can set things up to react to a change is not a good use in general.
BTW, memories. My first home computer was a TRS-80. I never had the Spectrum although I did pick up a Timex Sinclair before moving onto Commodore's and beyond. Those were some fun times.
Unless you are using 1980's ZX Spectrum, a 100ms setInterval will not consume any resources.
Yep, agreed, instead of polling a variable for a change, it is better to attach all actions to whatever is making the variable to change in the first place. Like button click, user input, device rotation etc.
Discussions have been disabled for this post