Math with Variables?
Nov 30, 2012
By
OWEN HOLT
Let's assume that I am performing some type of preference quiz. Based on user selections, 2 variables (let’s call them E and I) store adjusted values. So far, easy enough. Now let’s say I want to show as a percentage the strength of one over the other. In excel (or any other program supporting simple math functionality) I would use something like: 100 * (Variable X / (Variable X + Variable Y)).
My question is, can I set a new variable's value using a simple equation like this?
“Set EvIP = 100 * (Variable X / (Variable X + Variable Y)) when the user clicks…”
11 Replies
I can't remember if you can do multi-step calcs natively. What you might be able to do each step one at a time to produce the end result. E.g. start with a division > then a multiplication > then another division = final answer. NOTE: those 3 steps aren't supposed to mimic your exact formula - they're just an example. Sure, this might mean introducing another couple of variables but logically (and I'm not in front of SL right now) I see no reason for it not to work.
You'd also have to line up each trigger in the correct sequence so they fire in the right order.
Hi Owen! You can certainly do that. You'll need triggers for each operation within the equation, though. Attached at the bottom of this post is an example file showing your equation of 100*(E/(E+I)) ... go ahead and preview it in Storyline and see if this mimics what you had in mind.
There might be other ways to do this, but to me (and my non-math-brain) it made the most sense to create a variable for not only E and I, but also the sum of E+I and the final result. Then I used those variables to work my way through the equation. Here's the logic I used in my triggers:
And YES Simon makes a great point about trigger order. This is really important in a calculation such as this one.
If you're down with using JavaScript, you'd be able to call this whenever you need to run the calculation.
For this one:
Set EvIP = 100 * (Variable X / (Variable X + Variable Y))
Make a JavaScript trigger like this:
var player=GetPlayer();
var vX=player.GetVar("VariableX");
var vY=player.GetVar("VariableY");
player.SetVar("EvIP",(vX/(vX+vY))*100);
Now you can just copy this trigger whenever and it'll set EvIP to the calculated value.
Got it... and THANKS!
Next question.... based on the outcome of the math performed using the variables, can I use the results variable to control/change a graphic?
In other words, can graphics be variable based on another variables value? (Still trying to learn.)
You can show and hide graphics and change states as well as showing layers to change visual representations. However, you can't change the location or scale of an object using a trigger. So, you can fake it. But you can't programmatically set transformations.
Hi Owen - here's a quick example. In the .story file attached below, I inserted a character and created a couple of states for him (the states just show the character in a different expression/pose). Then I added some triggers which change the character's state based on the outcome of the calculation. Here's a screenr that shows how: https://jeanette.viewscreencasts.com/aa920f4367005c834623a473f848c643
That is just so perfect... Thanks Jeanette!
...and the good news is, I received approval finally and purchased my license yesterday!
So now I am official.
Hey that is great news Owen! Can't wait to see & hear about the cool stuff you build with it!
This discussion is closed. You can start a new discussion or contact Articulate Support.