# Variables in games

Community Member Posts: 142 ♪ Opening Act ♪
Hi Everyone,

I'm designing a game where users roll two dices and then pick the question to answer. Rolling the dices activates all questions' buttons.  Is there a way to activate only the button that associates with the total numbers on the dice tiles? For example, if the total number of dice tiles shows 10, then question 10 only becomes active.

I know this needs a variable, but I was not able to reach to it thus far.

Thank you,

• Community Member Posts: 1,217 ♦ Idol ♦
How did you program the dice ... without variables to decide which value each shows?
• Community Member Posts: 142 ♪ Opening Act ♪
Thank you Tim.  Here's what I did:

1- Saved images for each tile of each dice.

2- Grouped the 6 images and called them Dice 1; same with the other 6 images and called them Dice 2

3- Created 2 variables and called them Dice 1 and Dice 2

4- On the Roll button I added actions to Modify the Variable of Dice 1 to set to Random (0,6), and then Change Content of the dice image to the side image if the dice variable is equal to 1, 2, etc.  For instance: Change Content, Target: Dice, Resource: Dice-side 1 If Dice_1 (the variable) is Equal to 1; and so on. (please see attachments).

So basically, I didn't use calculations to roll the dice. I just enabled and disabled the questions buttons with the click on the Rolling button, and used the Move to action to move the dice while rolling.

Now, I want to do some math and enable only the question when the sum of the two dices' sides is equal to the number of question.  For example, the two dices' total is 6, then question 6 becomes active.

How do I do that?

Thank you
• Community Member Posts: 1,217 ♦ Idol ♦
So, you do know how variables work... the math part is simpler than what you've got so far. Just create a third variable, e.g. "Both_Dice", and two actions after those that randomly set the two dice:

On: Click
Action: Modify variable
Target: Both_Dice
Type: Set equal to
Value: VAR(Dice1)

On: Click
Action: Modify variable
Target: Both_Dice
Value: VAR(Dice2)

... make sure it matches the correct names, yours won't have blanks have they?

"Both_Dice" now contains the sum of the two value.

Then add actions similar to those changing the images:

Action: Set state
Target: Button 1
State: Enable

If "Both_Dice" equal to "1"

You'll need 1 for each button / for each value "Both_Dice" might have.
• Community Member Posts: 142 ♪ Opening Act ♪
That was easy!!!

Thanks a lot Tim, that solved the problem and assigned the dices to the correct question numbers.

Thank you so very much,

• Community Member Posts: 142 ♪ Opening Act ♪
Tim or others,

One more question on the same topic: Dice Rolling.  How do I limit the results of the rolled dice to the numbers of unanswered questions?  Meaning: if question 9 is answered, I don't want the dices total to land on 9.  There are 10 questions in this game, and if the users answer 5 questions, they still have to roll over and over until the total lands randomly on an unanswered question.

Thank you.
• Community Member Posts: 1,102 ✭ Legend ✭
That's tricky with the Actions system. It would be easy in code, but Actions have no way to handle a list of numbers.

You can use the question variables (see https://www.trivantis.com/blog/lectora-basics-question-variables/) and a series of Conditions on the goto action to reject going to an already-answered question, but I don't off the top of my head see an obvious way to trigger the action again. (It would be trivial to pop up a message saying, "Please roll again." It would also be clunky and frustrating for the user.)

What is missing is the ability to have an Action trigger itself again if it gets an invalid result.

EDIT: if you are comfortable with even very basic JavaScript (which is all I personally know) you could have the button trigger a script rather easily--that's a supported Action type. That script could set a variable, and could read the Question variables and make sure the result is limited to un-answered questions.
• Community Member Posts: 142 ♪ Opening Act ♪
Thank you Carl.  I know it is rather very difficult with variables and actions than if it is built in Java Script.  Unfortunately, I'm clueless in Java Scripts.  All what I know is to copy and paste them but cannot create them.

Many thanks again for your attempt though:)
• Community Member Posts: 1,102 ✭ Legend ✭
It's times like this I miss Asymetrix ActionScript.
• Community Member Posts: 142 ♪ Opening Act ♪
True! Thank you Carl:)
• Community Member Posts: 1,217 ♦ Idol ♦
Here's a sample how it can be done with Lectora alone. It does require quite a lot of actions...

• Community Member Posts: 142 ♪ Opening Act ♪
This is nice Tim, thank you for sharing it.  Let me challenge myself now and see if I can apply it.  It's exactly what I'm looking for!  I wish I can do it.
• Community Member Posts: 1,217 ♦ Idol ♦
The system uses 5 variables:

die1value, die2value, totalvalue: These are the same as before.

repeat: Either "yes" or "no". It's used during "Check Results" to decide whether new random values are required for die1value and die2value, because the totalvalue has been rolled before.

questions: It contains characters, a - k, to represent each possible "totalvalue", 2 - 12, i.e. a=2, b=3 etc

The group "Enable ..." is what happens when a valid number is rolled. Currently there are two actions for each "totalvalue".

If totalvalue = 2, Button 2 is enabled  and "a" (representing "2") is subtracted from "questions".

When another 2 is rolled, "Check Results" will find that "totalvalue = 2" but "questions" Does not contain "a". "repeat" stays "yes" and the last action the group will run "Randomize Dice" again to receive new values.

You can use any action you want instead of "Enable Button", e.g. "Show Question 1" or "Go to page 15".

You can move the "Subtract a" action to another location, e.g. to the page with the question, to the check button and give it different conditions, e.g. "If Question 1 is correct".
• Community Member Posts: 142 ♪ Opening Act ♪
Thanks a lot Tim, this is a very helpful explanation.  I wish I had had these variables even before I design the game.  My challenge is that my game has already its own variables that do other functions besides the dice rolling.  I'm trying to merge yours with mine, while maintaining those functions running.  With the many actions, it's quite difficult to manage all of them.

I'll keep you posted...

Many thanks again,

• Community Member Posts: 142 ♪ Opening Act ♪
@Tim,

This maybe too late, but I'm indebted to you with a big thank you. A year after you gave me this advice, I was able to apply it during my annual update of the training. Your zip file was so helpful and the trick is super convenient. It saved the time of random rolling of dice.
Thank you so much,
• Product Director, Lectora, eLearning Brothers West MichiganRockstar Manager Posts: 69 eLearning ROCKSTAR Stage Manager
@timk and @tmichael9734 you guys are both Lectora Rockstars! Thank you for being a part of our community!
----------------------------------------------
Chris Willis, Product Director, Lectora
◖⚆ᴥ⚆◗ "Where there's a Willis, There's a Way!"