How to Use Variables in Sessions (Examples)

From TrainzOnline
(Difference between revisions)
Jump to: navigation, search
m (Example 3: Counting Consist Checks)
m (Example 2: Event Counter and Message Selection)
Line 228: Line 228:
 
<tr valign="top">
 
<tr valign="top">
 
<td width=10>[[image:DotPoint.JPG|10px|link=]]</td>
 
<td width=10>[[image:DotPoint.JPG|10px|link=]]</td>
<td>A variable, '''SetSPAD''' (value "0" or "1"), is used to indicate whether a SPAD event is to be counted and responded to or not. This is set to "0" or "1" by track triggers</td>
+
<td>A variable, '''SetSPAD''' (value "0" or "1"), is used to indicate whether a SPAD event is to be counted and responded to or not. This is set to "0" or "1" by track triggers. Use the '''[[Session_Rule_List_with_Parameters#Variable_Modify_Rule|Variable Modify Rule]]''' to create and set this variable</td>
 
</tr>
 
</tr>
 
<tr valign="top">
 
<tr valign="top">
 
<td>[[image:DotPoint.JPG|10px|link=]]</td>
 
<td>[[image:DotPoint.JPG|10px|link=]]</td>
<td>A variable, '''SPADCounter''', is used to count the SPAD events and is initially set at 0</td>
+
<td>A variable, '''SPADCounter''', is used to count the SPAD events and is initially set at 0. Use the '''[[Session_Rule_List_with_Parameters#Variable_Modify_Rule|Variable Modify Rule]]''' to create and set this variable</td>
 
</tr>
 
</tr>
 
</table>
 
</table>
Line 330: Line 330:
 
<tr valign="top">
 
<tr valign="top">
 
<td width=629><span id="stepEx3"></span>
 
<td width=629><span id="stepEx3"></span>
 +
 
='''Example 3: Counting Consist Checks'''=
 
='''Example 3: Counting Consist Checks'''=
 
</td>
 
</td>

Revision as of 12:08, 3 October 2023

The information in this Wiki Page applies to TANE, TRS19, Trainz Plus and TRS22. It gives examples of using variables in Sessions.

Contents

BlueDot10x10.png Variables store integer values in Sessions that can be used to:-
BlueDot10x10.png Test for certain events (e.g. if something has occurred a set number of times)
BlueDot10x10.png Make decisions according to the value held by a variable
BlueDot10x10.png Act as counters or score keepers
BlueDot10x10.png Variables can be used for basic arithmetic by having set values added or subtracted
BlueDot10x10.png Variables can have random values assigned to them


The scenarios shown below give examples of how variables can be used to control the operation of a session.

Example 1: Random Passenger Stop

    Top   Next Down Bottom
Magnifier.png The Scenario:
In the example shown below a random variable is to be used to decide which one of 3 stations a user controlled train will be required to stop at. The user will be informed of the decision, to stop or not, by a message placed on the screen before they reach each station. When the train approaches the first possible stopping place, Sandplace, the session checks the value stored in "stopID" and if it is "1" then a stop will be requested.
RandomLogic.jpg
DotPoint.JPG If a stop is required then a predefined Navigation Point is shown to indicate the stopping place
DotPoint.JPG The same logic is repeated for the other two stations with stopID values of "2" and "3"


Conditions.PNG Preconditions:
DotPoint.JPG The Randomize Rule is used at the start of the session to create a different sequence of random values each time the session is run
VariableRandom.jpg
Setting the random variable stopID
DotPoint.JPG The Navigation Display Rule is used to create three Navigation Point Sets, one for each stopping place
DotPoint.JPG The Navigation Set Hide/Show Rule is used to initially hide all three navigation sets
DotPoint.JPG The Variable Random Rule is used to create a variable, stopID, with an integer random value from 1 to 3. Each number representing one of the stops


RulesWiki.PNG Rules Used:
In addition to those used in the Preconditions above:-
DotPoint.JPG Trigger Check Rule to trigger the decision and message event
DotPoint.JPG Variable Check Rule to check the value held in a session variable
VariableCheck.jpg
Checking the variable stopID for the first stopping place
DotPoint.JPG Navigation Set Hide/Show Rule to show the navigation set identified by the random variable
DotPoint.JPG Message Popup Rule to open a message window on the screen


Screenshot.PNG Session Editor Screenshot:
AppRandomStop1.jpg


Explanation.PNG Description:
Line  1 : Trigger Check The event is triggered when the consist passes over a track trigger. This executes the following rules:-
Line  2 : Variable Check The value stored in the variable stopID is tested "=1". Lines 3 and 4 are executed only if stopID is equal to "1"
Line  3 : Navigation Set Hide/Show Displays a navigation point showing the stopping location
Line  4 : Message Popup Displays a message window showing that a stop has to be made at Sandplace
Line  5 : Variable Check The value stored in the variable stopID is tested "<>1". Line 6 is executed only if stopID is not equal to "1".
Line  6 : Message Popup Displays a message window showing that no stop has to be made at Sandplace


Example 2: Event Counter and Message Selection

    Top Next Up Next Down Bottom
Magnifier.png The Scenario:
Each time a user causes a SPAD (Signal Passed At Danger) at specific locations, the event is counted and an appropriate message is displayed on the screen. The tone of the messages becomes more severe with each occurance.


Conditions.PNG Preconditions:
DotPoint.JPG A variable, SetSPAD (value "0" or "1"), is used to indicate whether a SPAD event is to be counted and responded to or not. This is set to "0" or "1" by track triggers. Use the Variable Modify Rule to create and set this variable
DotPoint.JPG A variable, SPADCounter, is used to count the SPAD events and is initially set at 0. Use the Variable Modify Rule to create and set this variable


RulesWiki.PNG Rules Used:
DotPoint.JPG Signal Passed At Danger Check Rule set to trigger on every SPAD event
DotPoint.JPG Variable Check Rule to check the value held in a session variable
DotPoint.JPG Driver Schedule Rule used to immediately stop the train
DriverSchedule.jpg
Using the Driver Schedule Rule to halt the train
DotPoint.JPG Simultaneous List Rule to force all rules to run simultaneously for speed
DotPoint.JPG Variable Modify Rule to alter the value held in a session variable
DotPoint.JPG Variable Show Rule adds a session variable to the Heads Up Display
DotPoint.JPG Message Popup Rule to open a message window on the screen


Screenshot.PNG Session Editor Screenshot:
VariableSPADWarnings.jpg


Explanation.PNG Description:
Line  1 : Signal Passed At Danger The event is triggered every time a consist passes a signal (or stop sign) set at STOP. This executes the following lines:-
Line  2 : Variable Check The value stored in the variable SetSPAD is tested "=1". If true then the following lines are executed. This executes the following lines:-
Line  3 : Driver Schedule The current schedule (if any) for the user controlled driver is replaced with a Halt Train command to bring the train to an immediate stop
Line  4 : Variable Modify The value stored in the variable SPADCounter is increased by 1
Line  5 : Simultaneous List This forces all the Variable Check Rules to be run simultaneously. This is not strictly necessary but without it all the Variable Check Rules will be tested, one after the other, when only one (or none) of them will be executed. This will (marginally) speed up the processing. This will execute the following lines:-
Line  6 : Variable Check The value stored in the variable SPADCounter is tested "=1". If true then the first SPAD event has occurred and the following lines are executed:-
Line  7 : Variable Show The value stored in the variable SPADCounter is added to the Custom HUD. Each time the value changes it will be updated in the HUD
Line  8 : Message Popup The first warning message is shown on the screen
Line  9 : Variable Check The value stored in the variable SPADCounter is tested "=2". If true then the second SPAD event has occurred and the following line is executed:-
Line  10 : Message Popup The second warning message is shown on the screen
Lines 9 and 10 are then repeated with variations as shown. If more than 5 SPADs occur, then no new messages will be shown (the driver is clearly taking no notice so what is the point?) but the SPADCounter will continue to be updated.


Example 3: Counting Consist Checks

    Top Next Up Next Down Bottom
Magnifier.png The Scenario:
The Consist Check Rule cannot be set to trigger only once but sometimes you want it triggered only once on (for example) the 2nd time that a particular consist is formed. This example uses a variable to count the number of times the rule has been triggered and acts only when a set count has been reached. It could easily be modified to trigger a set number of times.


Conditions.PNG Preconditions:
DotPoint.JPG A variable, ConsistCount, is initially set to "0". Use the Variable Modify Rule to create and set this variable


RulesWiki.PNG Rules Used:
DotPoint.JPG Consist Check Rule set to trigger when a specific consist has been formed
DotPoint.JPG Variable Modify Rule to set and increase the value held in a session variable
DotPoint.JPG Variable Check Rule to check the value held in a session variable
DotPoint.JPG Message Popup Rule to place a message on the screen


Screenshot.PNG Session Editor Screenshot:
VariableConsistCounter.JPG


Explanation.PNG Description:
Line  1 : Consist Check Rule Triggered when the specified consist has been found. This executes the following lines:-
Line  2 : Variable Modify Rule Adds 1 to the variable ConsistCounter
Line  3 : Variable Check Compares the value stored in ConsistCounter to a set value (in this case 2). If it matches then the next line is executed:-
Line  4 : Places a message on the screen


Example 4: Train Status

    Top Next Up Next Down Bottom
Magnifier.png The Scenario:
A consist identified by number 188 passes over the same trigger twice, once when empty and once when loaded. Each time the user needs to be given a different set of instructions


Conditions.PNG Preconditions:
DotPoint.JPG A variable, T188Status is initially set to "1" signifying that the train is empty. This will be modified to "2" after it has been loaded


RulesWiki.PNG Rules Used:
DotPoint.JPG Trigger Check Rule to trigger the event when the consist reaches the trigger
DotPoint.JPG Variable Check Rule to check the value held in a session variable
DotPoint.JPG Message Popup Rule to place a message on the screen


Screenshot.PNG Session Editor Screenshot:
VariableTrainTrigger.JPG


Explanation.PNG Description:
Line  1 : Trigger Check Rule Triggered by the train. This is NOT set to trigger once only. When triggered it will execute the following lines:-
Line  2 : Variable Check Compares the value stored in T188Status to 1, indicating "empty". If it matches then the next lines are executed:-
Line  3 : Message Popup Places a message on the screen
Line  4 : Variable Check Compares the value stored in T188Status to 2, indicating "loaded". If it matches then the next line is executed:-
Line  5 : Message Popup Places a message on the screen


Variable Rules and Commands

    Top Next Up   Bottom
RulesWiki.PNG These are the Session Rules that can access, create and/or modify Session variables
DotPoint.JPG Randomize Rule - creates a different set of random numbers every time a session is run
DotPoint.JPG Variable Check Rule - executes child rules according to the value of a specified variable
DotPoint.JPG Variable Modify Rule - changes the value of a specified variable and creates new variables by setting their initial values
DotPoint.JPG Variable Modify Continuous Rule - continuously changes the value of a specified variable
DotPoint.JPG Variable Random Rule - assigns a random value to a variable
DotPoint.JPG Variable Show Rule - adds or removes a specified variable and a descriptive label to the custom HUD


CommandsWiki.png These are the Driver Commands that can access Session variables. The WikiLink.PNG InputTable Rule must be added to the Session and configured with the names of the variables before these Driver Commands can be used.
DotPoint.JPG Skip if Session Variable - conditionally jumps to the named command label in the Driver Command Bar if a selected session variable meets a set condition
DotPoint.JPG Wait Until S-Variable V3 - AI driver waits for the specified variable condition to occur
      Top Next Up    


Trainz Wiki

TrainzWiki.png

More Tutorials and Guides to Using Trainz


This page was created by Trainz user pware in April 2018 and was last updated as shown below.


Personal tools