How to Use Variables in Sessions (Examples)
(→Trainz Wiki) |
m (→Preconditions) |
||
Line 127: | Line 127: | ||
==Preconditions== | ==Preconditions== | ||
− | *A variable, ''' | + | *A variable, '''ConsistCount''' is initially set to "0". |
==Rules Used== | ==Rules Used== |
Revision as of 08:55, 1 July 2018
Contents |
The screenshots shown below give examples of how variables can be used to control the operation of a session.
Example 1: Random Passenger Stop
The Scenario
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.
Preconditions
- 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.
- The Navigation Display Rule is used to create a Navigation Point Set.
- 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.
Setting the random variable stopID |
Program Logic
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.
If a stop is required then a predefined Navigation Point is shown to indicate the stopping place.
The same logic is repeated for the other two stations with stopID values of "2" and "3".
Rules Used
- Trigger Check Rule to trigger the decision and message event.
- Variable Check Rule to check the value held in a session variable.
Checking the variable stopID |
- Navigation Set Hide/Show Rule to display a predefined navigation point.
- Message Popup Rule to open a message window on the screen.
Session Rules Screenshot
Explanation
- Line 1: Trigger Check The event is triggered when the consist passes over a track trigger.
- 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.
- 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"
Example 2: Event Counter and Message Selection
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.
Preconditions
- 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.
- A variable, SPADCounter, is used to count the SPAD events and is initially set at 0.
Rules Used
- Signal Passed At Danger Check Rule set to trigger on every SPAD event.
- Variable Check Rule to check the value held in a session variable.
- Driver Schedule Rule used to immediately stop the train.
Using the Driver Schedule Rule to halt the train |
- Simultaneous List Rule to force all rules to run simultaneously for speed.
- Variable Modify Rule to alter the value held in a session variable.
- Variable Show Rule adds a session variable to the Heads Up Display.
- Message Popup Rule to open a message window on the screen.
Session Rules Screenshot
Explanation
- Line 1: Signal Passed At Danger The event is triggered every time a consist passes a signal (or stop sign) set at STOP.
- Line 2: Variable Check The value stored in the variable SetSPAD is tested "=1". If true then the following lines are executed.
- 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.
- 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.
- 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 2: Variable Check The value stored in the variable SetSPAD is tested "=1". If true then the following lines are executed.
Lines 9 and 10 are then repeated with variations as shown. If more than 5 SPADs occur, then no further 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
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 1st, 2nd, 3rd, 4th, etc, 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.
Preconditions
- A variable, ConsistCount is initially set to "0".
Rules Used
- Consist Check Rule set to trigger when a specific consist has been formed.
- Variable Modify Rule to set and increase the value held in a session variable.
- Variable Check Rule to check the value held in a session variable.
- Message Popup Rule to place a message on the screen.
Session Rules Screenshot
Explanation
- Line 1: Consist Check Rule Triggered when the specified consist has been found.
- 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 lines are executed.
- Line 4: Message Popup Places a message on the screen.
Trainz Wiki
- How to Guides
- Session Rules List (Alphabetical) with Parameters
- Session Rules List (Categories) With Parameters
Route Creation Tutorials:
Session Creation Tutorials:
- Adding Message Popups
- Adding Message Popups - Applications
- Adding Navigation Points
- Adding Navigation Points - Applications
- Adding Track Triggers
- Configuring the Driver Setup Rule
- Controlling Junctions in Sessions
- Controlling Signals in Sessions
- Creating a HTML Asset (Session Introduction Page and In Game Messages)
- List of Parent Rules
- Parent and Child Rules