Don't confuse Random Variables with Random Values. Random Variables are variables that change at regular intervals (that is, every so many turns). Random Values, on the other hand, are the random numbers assigned to any kind of variable. Random Variables may use Random Values, but they are two totally different concepts.
Random Variables are generally used to generate random descriptions or cause random events to occur. This is accomplished by changing the value of the variable at a regular interval (usually, every turn). There are three ways to change this value: (1) increasing/decreasing the number BY a specific value; (2) increasing/decreasing the number BY a random number in a range between X and Y; or (3) changing the number TO a random number in a range between X and Y. Each of these three methods is discussed in other sections of this Tutorial. The purpose of Random Variables is best accomplished using the third method.
You know how to change the value of the variable TO a random number from other sections, but how do you cause this randomization to happen regularly? And then, what is the purpose of it?
To randomize a variable at regular intervals you need to create a Task-Event combination, a team that works together to change your variable every so many turns. For our example, we will update the variable every turn. To do this, let's create a generic Task-Event combo that we can use to update ANY variable which should be changed every turn.
First, create a Task entitled "~Randomize Variables Every Turn" (I have used a tilde "~" at the beginning to minimize the possibility of it being invoked accidentally by the player). You don't need a description. There are no restrictions. Make sure it is set to be Repeatable. Later, we will change our variables in the Actions section, but go ahead and close the new task for now.
Next, create an Event entitled "Randomize Variables Every Turn". Set the event to start "Immediately" and last between "1" and "1" turns. This event should also be checked to "Restart when finished". Lastly, we want the event to execute the Task we just created above. This new Event will now run every turn and execute the Task called "~Randomize Variables Every Turn".
We have to setup this Event-Task combo because only Events can be set to run every turn, and only Tasks can change the values of variables. Finally, we want to change our variables. What if we changed our WallColor variable (discussed previously) to a random number between "0" and "9"? We would then have an ALR File changing the numbers to colors (red, green, orange, etc.) Now, every turn that the player looked at the wall, it would randomly change to a different color.
Don't forget, you can put as many variables as you need to in this newly created Event-Task combo when you want their values to randomly change every turn. If you want a different interval, then you would need to create a new Event-Task combination.
The following example uses variables and ALR File entries to change what the player sees when he looks outside, depending on whether it is day or night. We need an outside room, three variables for appearance, sound and temperature, and an Event-Task combo. Create the outside room and for the description, enter:
!Appearance-%OutsideAppearance% !Sounds-%OutsideSounds% !Temp-%OutsideTemp%
Now create the variables "OutsideAppearance", "OutsideSounds" and "OutsideTemp". The initial value for each should be "0". Next edit your ALR File to include the following entries (see the ALR File Tutorial for more information on working with ALR files):
!Appearance-0|The day is bright and sunny with scattered clouds in the blue sky overhead.
!Appearance-1|It is dark outside, but the moon's rays peeking through the cloudy night sky give you a quick glimpse of your surroundings.
!Sounds-0|You can hear the sounds of a bubbling brook nearby and leaves rustling in the wind.
!Sounds-1|Sounds of crickets and other nocturnal creatures fill the air.
!Temp-0|The temperature outside is warm and comfortable.
!Temp-1|You are covered in goose bumps as the chill of the night air sends a quick shiver up your back.
Remember that text may or may not wrap in your ALR file as it does above. It depends on the current wrap settings of your text editor. Finally, we need to create an Event-Task combo. Create a Task entitled "~Change Time of Day" and set it to be repeatable. Add three Actions to change the values of our three variables to "1". If day changed to night only once, we would be finished with this task. However, day turns to night, then back to day, so we actually need two tasks: one which executes when it is daytime and one to execute when it is nighttime. Since our current task is changing the variables to "1", meaning night, then we also need to add a Restriction to this task requiring at least one of the variables to equal "0" (this ensures that this task only runs when it is daytime).
Then, we create a second task that mirrors the first task, except that the Restriction now requires one of the variables to equal "1" and the Actions change all three variables to "0". The second task's name should be identical to the first so that the Event we create will execute one of these two tasks based on which one satisfies the Restrictions.
NOTE: You may have noticed that we only had to use one variable in the Restrictions. This is because all three variables will be equal at all times. So there is only a need to setup the Restriction using one of these variables. If the variables were not equal for any reason, then you would need to setup Restrictions for ALL of them.
We're almost done! Last, create an Event entitled "Change Time of Day". Set the Event to start "Immediately" and have it last between "30" and "30" turns. Also set it to "Restart when finished". To complete the Event, have it execute the Task "~Change Time of Day" when finished.
Save and run your adventure. When the player looks during the first 30 turns, the value of the variables is initially set to "0", representing daytime. Therefore, the description would be:
The day is bright and sunny with scattered clouds in the blue sky overhead. You can hear the sounds of a bubbling brook nearby and leaves rustling in the wind. The temperature outside is warm and comfortable.
When the player looks 30 turns later, he would get a different description as follows because the value of all three variables would then be "1":
It is dark outside, but the moon's rays peeking through the cloudy night sky give you a quick glimpse of your surroundings. Sounds of crickets and other nocturnal creatures fill the air. You are covered in goose bumps as the chill of the night air sends a quick shiver up your back.
Variables are a great tool for allowing the player to use some type of currency for purchasing or trade. First, you need to create a variable representing the currency amount. For this example, we will use a pouch of gold coins. Our variable will be "NumCoins", with an initial value of "100". We also need an object to reference which we will give the name "coins" and an object prefix named "a small pouch of". In the description of the object, type "It is a small leather pouch, with %NumCoins% gold coins inside." With this description, the player can find out how many coins he/she has at any given time by typing "look coins".
Second, we need an object to purchase, so let's create an object "sword" with an object prefix named "an ancient". We can't purchase our object unless we have a task "buy sword". Set the task to be repeatable and having the description, "You purchase the ancient sword. It makes a wonderful addition to your collection." The task also needs restrictions to prevent the player from purchasing the sword unless (1) the player is holding the coins, and (2) the player has enough coins to buy it. Therefore, create two restrictions, one stating "a small pouch of coins must be held by player" and a second one stating "NumCoins must not be less than 50". Last, create an Action moving the sword to be held by the player.
Finally, you will want to create other items in your adventure to purchase or other ways to lose and gain gold coins. You will also need characters in your game to interact with when making your purchases and trades.
Another way in which variables can be used is to adjust the skill level of a player. For example, if the player is a gunfighter in a western, the skill level he has may determine whether or not he wins a draw at the end of the game. Or it may determine how "accurate" the player's shots are.
To accomplish this, you would need a variable such as "ShotAccuracy" to represent the player's skill level at firing. You would of course also need the appropriate objects and tasks for the gun and shooting the gun, as well as targets to shoot at. You would also need a task to increase the skill level every time the player "practiced". For instance, every five times the player shoots at cans on a fence, the ShotAccuracy variable would be increased by one, to a maximum of 3.
How then do you determine the outcome based on the ShotAccuracy value. Let's assume that the player has a draw with an outlaw at the end of the adventure. You might have a simple task "shoot outlaw", but you would need to duplicate the task for however many outcomes you want in the draw. For example, we will have three outcomes. Therefore, the first "shoot outlaw" task should have a restriction for the ShotAccuracy to be "3". The description in this task will read, "As the smoke clears, the outlaw lays dead. You stand proud as people begin emerging from the stores rejoicing."
The next two tasks will be exact duplicates except the restrictions for ShotAccuracy will be 2 and 1. The descriptions for the ShotAccuracy of value "2" would be something like you and the outlaw both getting shot. The description for the "1" ShotAccuracy would end up in you being shot and killed.
System Variables may be used in descriptions and in expressions set up on the Actions tab of the "Edit a Task" dialog box. However, they cannot be used in expressions on the Restrictions tab. The following is a workaround, allowing you to use the value of System Variables in a Task Restriction.
Let's say we want to use the player's current score (%score%) in a Task Restriction. First you need to create an Event-Task combination which runs every turn (see the section Random Variables for more information). You can use the same one you may currently be using every turn to randomize certain variables. Next, create a variable for the player's score (other than %score%, because that is the System Variable), such as "PlayerScore". Go to the Actions tab of the Task used in your Event-Task combo and create the entry "Change PlayerScore to expression %score%".
With this completed, your variable, PlayerScore, will be set to the value of the System Variable, %score%, every turn. You now have a variable you can use in a Task Restriction with the same value as the associated System Variable. For an additional example of this concept, please see the section, Checking the Player's Version of Runner.
It may be necessary to alert your player if he/she needs to update their version of ADRIFT Runner before playing your adventure. Otherwise, there may be features you used which are not available to the player, or which are entirely misunderstood, rendering your adventure pretty much useless.
To do this, you need to have a way of comparing the value of the System Variable, %version%, with the version of ADRIFT Generator that you used to create your TAF file. First, you need an understanding of the workaround to use System Variables in Task Restrictions. If you don't know what I'm talking about, then please see the previous section, System Variables in Task Restrictions for more information.
Create two variables, RunnerVer and GameVer. Set the initial values of both variables to the current version of ADRIFT Generator you are using. This value should be similar in format to the System Variable, %version%, which is in the form "3900016" (meaning Version 3.90, Release 16). If you aren't sure of the exact value or its format, put the System Variable, %version%, in the description of your adventure's first room and run the adventure. When you "look" at the room, the version number will be displayed for you in the format you need to use. For this example, we will assume that %version% is equal to "3900016". Therefore, RunnerVer and GameVer should both be set to the initial value of "3900016".
In your Event-Task combo which runs every turn, create an Action to set the RunnerVer equal to the expression %version%. This will update your variable RunnerVer with the current version of ADRIFT Runner used by the player. Now create an Event-Task combination that runs only once at game start. The task should check to see that RunnerVer is equal to, or greater than, the variable GameVer. If not, you may want to display a message alerting the player of his/her need to update their version of ADRIFT Runner.
You are using an older version of ADRIFT Runner. This adventure may not work properly with this version. Please update your version of Runner with the latest release at www.adrift.org.uk.