Event Trigger when Variable Value Changes (Triggering Events Based on Changes to Variable Values)

rp-7

#1

(or… listening to variables)

Hello Everyone,

Is there a way to have changes in variables trigger events, or something that can be reacted to with subsequent behavior (code-based logic)? In my scenario, the user has just entered a particular context within my storyboard (a page has loaded consisting of various widgets, etc.). As part of this loading, various context variables are set with initial values local to this new context. As the user operates within this context, various actions will cause changes to the values of these variables. Upon changes to such variables, I would like certain actions to automatically be triggered. And I’d like these respective actions to be simply assigned to the variable change events that logically should trigger them (In other words, I’d prefer not to have to perform these actions within the Case that is also making the change to the corresponding variable… I’d like the action to basically be a Case assigned to a ‘VariableChange’ event…). Thanks in advance…


#2

There is no OnVariableChanged Event yet, we’ll look into adding this. For now you could use something similar to functions, Function/Functions in Interactions (Triggering ‘Custom Events’ on Widgets, Trigger, Triggers, Actions Macro/Action Macros).

You would still have to add the Move by 0,0 of the Dynamic Panel to each action that set the Variable value, but at least you could keep all of the logic in a single place.


#3

Is it possible to trigger an event when an variable value changes?


#4

You can fake it.

What you need to do is add a checkbox - anywhere, it doesn’t matter as you’re going to hide it - and set whatever action affects your variable to also toggle that checkbox. Then, add to the checkbox an OnCheckedChange - so whenever your variable changes, your checkbox toggles and your action is performed. Do a quick preview to ensure it’s working - remember by clicking the ‘X=’ link in the sitemap you can monitor in real time your variable values - and then once it’s working properly, hide your checkbox.


#5

Another workaround would be to store the value in a hidden text field rather than a Global Variable, and then use the text field’s OnTextChange event to trigger whatever you need.


#6

Thanks Stu, works like a charm …


#7

Hey, the link seems to be broken.


#8

Here’s an improved solution that allows you to attach your functionality to the ‘listening’ elements, rather than where variable value is changed.

This is much more elegant because it means your action doesn’t have to manually target all of the elements that need to change, and does not need to be updated every time you add new ‘listening’ elements, because the elements themselves listen for the change.

See the attached example (changing the colour scheme of a design) or follow the below steps.

Variable-based changes.rp (73.6 KB)

  1. Create your dynamic elements as masters
  2. In each master add a Page Interaction for ‘Page Click or Tap’ or ‘Page Loaded’ depending on how you want the change to be detected. Use ‘Page Click or Tap’ if your variable is changed by clicking a button or some other control; use ‘Page Loaded’ if it happens when the page is first drawn.
  3. Add logic to those events to check the value of the variable and perform an action on the widgets in your master for each eventuality, e.g. change the state of a DP