# 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

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

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

Hi Ross, even more interested now.  I am at schounsham@gmail.com  i'll be offline for a few hours though.  Cheers Christine

Hi Ross,

Storyline has built-in Random feature.
I think you can implement it using this feature.

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();
player.SetVar("randnum",randomnumber);

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.  :)

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();
player.SetVar("randnum1",randomnumber1);

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

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.

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.  :(

Hi Ross,

I understand what you want but for now.. Storyline 3/360 doesn't have feature to control a State from a variable value directly.

Thanks Daniel, Not sure what I can do to keep track of where the different counters are on the screen for 2 players.

Need to go back to the drawing board on this one.

Thanks to  Chino Navarro, Customer Support Engineer at Articulate Support, he has given me a solution to try.    Using Background layers means less scripting to help keep track of there the counters are.  Watch this space.  :)

This post was removed by the author

This post was removed by the author

Hi Ross,

Here is my mini-game prototype for 2 players.
Questions haven't added yet for this version.

Hi Daniel  That is very impressive.   Well done you.  :)  Thanks for sharing. :)

Thanks, Ross for the shoutout to Chino too. I'll be happy to pass that along!

You are welcome Ross. I can make that cleaner structure if I have enough time. Making a game like this is not easy in one sitting. :) Hope that this method will help you. Cheers.

Hi Daniel, I am adding in questions so we shall see.  You are amazing as me not being a programmer are astonished at what you could do.

The logic is really cool, so thanks for the help with this.  :)

Hi Ross,

Let me know if you have questions on how it is structured.

HI Daniel, Here is where I got to with the game with Questions added.

They always can do with more work but I am happy with how it is going now. :)

Thanks for assisting with this.  :)

Great Job Ross. That's how we teach a man how to fish here and feed him for a lifetime. :)
Keep spinning, keep layering, keep triggering and Publishing!

Cheers!

Thanks Daniel,  Keep teaching us to us great time saving scripting.

I think your brain is wired a little different to mine.  :)