Using Variables in games

Oct 31, 2018

Hi community, I have created a game using a dice and moving a counter a number of places depending on the result of the dice shake.

This works well for 1 player and 1 counter but I am having problems when I want to have a second player.

The counters have 41 states to represent the starting position and a different state for each of the moves, 1 - 40. 

I would like to use a variable so I can keep track of each counter's state and place the counter depending on the variable that changes with each dice roll.

The problem is, how to I change the State to a Variable instead of a number? 

19 Replies
Christine Hounsham

Hi Ross, Sounds like an interesting project you're working on. 

It would be good to know how your dice is working.  i.e. is that based on states or a variable?

It sounds like there are two possible points:

1. how to use a variable to track which players turn it is, and

2. using variables to add on each players subsequent dice roll to their existing result. 

Hope I'm on the right track, but please excuse if this is not what you're after. 

Re point 1. I would probably use a T/F variable to keep track of player turns.  i.e. create a variable PlayerTurnV, where True = player 1's turn and false = player 2's turn.  

If there are no 'have another turn' options, this makes it easy simply toggle between P1 and P2.  Simply add two triggers for when 'dice rolls':

- adjust variable PlayerTurnV to false when dice rolls, on condition PlayerTurnV = true, and

- adjust variable PlayerTurnV to true when dice rolls, on condition PlayerTurnV = false

You can now use the states of this variable to control which players score gets amended on each roll, regardless of whether you're using states or variables to keep track of total. 

Re 2. - it would be helpful to understand how your dice works for this. 

Cheers Christine 

Ross Dewstow

Hi Christine,  Thanks for the reply.  I will explain.

I am using JavaScript to select a random number from 1 to 6 and then depending on the outcome, I display the State of the Dice, 1 --> 6 and I also display a layer for where the player is up to and move the counter on the number of places shown by the dice.  I have 40 states for the counter so it can move on to the next spot.

This works really well for a single player but is complicated by a second player as I have to keep track of where the counters are and I would rather use a variable to track this so then I can just change the state of a counter to that variable but I cannot seem to do this as I have to select a state not use a variable to select the state.

I have attached the Zip File so you can see it working for 1 person.

The image in the back can be changed and the counters can be moved anywhere bit the States of the Counter would then be changed to make it work properly.

Hope you get the idea.  Can send the Storyline 3 file if you want a look. Let me know your email.



Ross Dewstow

Hi Daniel, That is a feature in Storyline 360 but not in Storyline 3 unless it is in the updated version that I have not got yet.

I used JavaScript to generate the random number, so that is not the issue here.

var randomnumber = Math.floor((Math.random()*6)+1)
var player = GetPlayer();

This returns a random number from 1 - 6 every time, so no issue.

Everything works well for the single player, but adding a second one and it goes out the window.

You see, the counter has 40 states, numbered 1 ----> 40 and to change the state I use the inbuilt script to do this.  The issue being, I want to change the state of a counter to a variable that I assign but I can't do this.  Change state to VarCounter1 as it needs a number....

Help.  :)

Daniel Servan

Hmnnn... I think you need to update your Storyline 3 so you will have that Random feature.
Anyway, for the second player, you will need to add a new variable like.

var randomnumber1 = Math.floor((Math.random()*6)+1)
var player = GetPlayer();

var randomnumber2 = Math.floor((Math.random()*6)+1)
var player = GetPlayer();

You can do it like this as shown below.
Please note that you can't change the state using a variable directly.
For example:
vRandom = 2;
if you have a states from 1-40. You can't say it like this.
Change the state of Shape 1 to  vRandom. Instead, Change the state of Shape 1 to 1 if vRandom is equal to 1.



Ross Dewstow

Hi Daniel, Thanks for that but ...

The issue being I need to use this script for 40 slides X 6 Layers x 2 Players so many copies and changes to make this work.

I sort of figured this out this morning where a Variable would mean 1 script copied many times.  SIGH !!!

I will see if I can get it going and post the interactive for people to try it out.  :)

The biggest hassle is that I have to test where Player 1 is when Player 2 is active as they could be in 1 of 40 places each move. You can see why a variable would make this easier.  :(

This discussion is closed. You can start a new discussion or contact Articulate Support.