Forum Discussion
Is there a way to auto run some triggers in a loop until the results are met?
- 3 months ago
JavaScript is more elegant and efficient, but for a few variables, you can do it with a handful of triggers on a layer.
Demo: https://360.articulate.com/review/content/041f67c6-c44a-47e8-a301-632caa78772b/review
This just shows a layer that assigns 4 random numbers to 4 variables. If any are the same, it reruns the the layer until they are all unique. This is very brute-force, but very simple. The triggers are fast enough that even if it takes many attempts, it is still practically instant.
This could be modified with a few more triggers to be more elegant, recalculating each random number only if it matches one of the previous variables, but in the end it is essentially the same thing. Simple gets the job done.
As Nedim mentioned, if you try this with many variables, it could begin to slow down, or require too many triggers. In that case, JavaScript is the way to go.
Actually, you can simulate your own “loop” in Storyline to keep generating random values for A, B, C, and D until they are all unique by using a motion path animation as a timer:
- Insert a small shape (call it interval) and create a short motion path animation for it, lasting between 0.1 and 1 second (eg., 0.5)
- Start moving interval along its motion path when the learner clicks a button.
- Set a trigger to move interval again along the same path whenever the animation completes.
- This creates a continuous loop effect, running every 0.5 second depending on the animation length.
- On each animation completion, add separate triggers to assign random numbers between 1 and 4 to A, B, C, and D.
- Also on animation completion, check if A, B, C, and D are all unique. If they are, stop (hide) the interval to end the loop.
To rerun the process, create a trigger that resets the state of interval to Normal when the learner clicks a button, which restarts the animation and loop.
This solution only requires about six triggers in total, far fewer than I originally expected, and it doesn’t rely on any JavaScript or additional layers.
Edit:
This motion path trick is conceptually similar to running this JavaScript code, though not identical. JavaScript offers more precise and consistent timing compared to motion path animations in Storyline.
const interval = setInterval(() => {
const A = getVar("A");
const B = getVar("B");
const C = getVar("C");
const D = getVar("D");
const values = [A, B, C, D];
const unique = new Set(values);
if (unique.size === 4) {
clearInterval(interval);
}
}, 500);
Related Content
- 7 months ago
- 4 months ago
- 10 years ago