An introduction to variables: Part 3 – Let Storyline do the maths
So, if you’ve read part one and two of this series about variables in Articulate Storyline, you will know that we can use variables to store text strings. In this blog, we are going to look at how we can use variables to store numbers, count, add, subtract and multiply… Simply put, I’m going to show you how Storyline does the maths for you.
Storing a number that a learner enters is easy – in this way it is very much like a text variable, in that you insert an input of a numeric entry field. When the learner clicks away from the field (i.e. ‘when the control loses focus’ in Storyline speak), the number variable is stored. There are, however, a few differences between number and text variables. Firstly, you cannot put any text into a numeric entry field (where you can put numbers into a text entry field). Secondly, you can complete calculations with number variables, but there are a few things you need to be aware of before performing any calculations.
But, before we jump ahead of ourselves, we need to understand number variables. A few key points to remember are:
- Number variables are containers waiting for input.
- The number must be stored in the container before performing a calculation.
- Once set, the numbers can be used for various calculations.
- The trigger order of number variables impacts their functionality.
So, let’s break it down. When you add a number variable, your trigger will look like this:
As you can see, a number variable is set when the ‘control loses focus’.
If you’re still trying to get your head around the term ‘when the control loses focus’ check out the description in part two of this series. Simply put ‘when the control loses focus’ means when the learner clicks elsewhere on the screen, for example a button. Get it? Great, let’s move on.
Here is the tricky thing about variables; we cannot see what has changed because variables are invisible to the learner unless we refer to them. We discussed referring to a variable in step 4 of blog 2 of the series, but it’s a really useful technique to highlight again. I always put a reference to my variables on the screen and this is really important when it comes to number variable calculations.
Remember, to recall your variable you need to add a reference in a text box or shape, containing the variable between two percentage marks, i.e. %VAR_DemoValue%
It’s also important to name your variables – and the name of your variable matters. And make sure you give them a logical name! We will discuss this more in part 5 of this blog series.
Once you have set your variable you can then use it to perform calculations; it is much easier if you can see what values you are performing calculations with, hence adding a reference of your numeric entries.
For number variables to work, you will need some input variables and a result variable. It helps to imagine your results variable as an empty box. Before you do any calculations, you have nothing in the box. To fill the box, we need some input variables.
In this example, we have . two input variables and one result variable. This means we have two items we want to put in the results box.
Set the input variable values:
Storyline doesn’t know what we want to do with these input variables until we trigger it. We need a couple of triggers to perform the calculation. In this example we are going to use a simple addition calculation, but all calculations will work on a similar principle.
Create a trigger to make our results variable equal input_1
Create a trigger to add input_2 to the value that is stored in the Results variable
These triggers can all be actioned by one interaction, like a button click, so the variable result is now equal to 3.
The same steps can be followed for subtraction, multiplication and division. But what happens if you want to use a number variable for something that does not use numeric entry fields, like counting how many times a learner has clicked a button or performed an action? Storyline can do that too.
Here, instead of adding two variables together, you can add a variable to a value:
Once again, you can do all of the operator functions with this approach. Just remember, that it is mathematically impossible to divide something by zero. If you do this, you are more than likely to cause a problem with your variable calculations!
It’s important to remember with number variables that Storyline does exactly what we tell it to. In this instance, we must be careful to clear the values stored against the variable if we wish to do a different calculation using the same variables, or we may find we get an accumulative result. You can do this by setting up another trigger, like below:
In part five of this blog series we will be discussing treating variables and values as integers and how to avoid issues with variables in general, so keep your eyes peeled for blog five coming soon!
In the next instalment of this blog series, I will be teaching you all about true/false variables, but in the meantime, if you have any questions, please feel free to contact us or tweet us @OmniplexeLearn – we’d love to hear from you!