Helpful Script Examples
Posted: Fri Jan 11, 2013 1:28 pm
The IF statement is likely the most commonly used script element. It can be used in many different ways and combined with other script elements with ease. The following are many examples displaying various methods of using it. These script pieces all work and can be copied into your world to see their output and how they work.
If anybody has any other suggestions of script pieces to make examples for, or would like ot see how something is used, feel free to post a request for it.
IF ELSE statement
Perhaps the most common use of the IF statement. Due to containing the ESLE statement the script will perform whatever IF action applies and then stop. If no IF statements apply, it performs the ELSE statement.
IF statement - single output
The ELSE statement is somewhat optional. This script is the same as the above, but without the ELSE. Excluding the ELSE statement means you can add additional stuff after the IF statements.
IF statement - multiple output
Also similar to the above two, by excluding the ELSE statement this script shows how multiple IFs can be used and applied without the need to nest them or use ELSE statements. It also allows additional stuff to be added at the end of the script.
Nested IF ELSE statement- single output
This is an example of an IF ELSE statement with nested IFs that have no ELSE. The script parser will go down whichever first level of the script applies, then perform whatever 2nd level IF statement applies, and continue to the end of the first level statement and stop.
If anybody has any other suggestions of script pieces to make examples for, or would like ot see how something is used, feel free to post a request for it.
IF ELSE statement
Perhaps the most common use of the IF statement. Due to containing the ESLE statement the script will perform whatever IF action applies and then stop. If no IF statements apply, it performs the ELSE statement.
Code: Select all
Event ("&command","test")
{
$TempVar = sysRand(6)
If ( $TempVar = 1 )
{
*say TempVar equals one
}
Else If ( $TempVar = 2 )
{
*say TempVar equals two.
}
Else If ( $TempVar = 3 )
{
*say TempVar equals three.
}
Else If ( $TempVar = 4 )
{
*say TempVar equals four.
}
Else If ( $TempVar = 5 )
{
*say TempVar equals five.
}
Else
{
*say TempVar equals six.
}
}
IF statement - single output
The ELSE statement is somewhat optional. This script is the same as the above, but without the ELSE. Excluding the ELSE statement means you can add additional stuff after the IF statements.
Code: Select all
Event ("&command","test")
{
$TempVar = sysRand(5)
If ( $TempVar = 1 )
{
*say one
}
If ( $TempVar = 2 )
{
*say two.
}
If ( $TempVar = 3 )
{
*say three.
}
If ( $TempVar = 4 )
{
*say four.
}
If ( $TempVar = 5 )
{
*say five.
}
*say complete
}
IF statement - multiple output
Also similar to the above two, by excluding the ELSE statement this script shows how multiple IFs can be used and applied without the need to nest them or use ELSE statements. It also allows additional stuff to be added at the end of the script.
Code: Select all
Event ("&command","test")
{
$TempVar = sysRand(5)
If ( $TempVar > 0 )
{
*say one
}
If ( $TempVar > 1 )
{
*say two.
}
If ( $TempVar > 2 )
{
*say three.
}
If ( $TempVar > 3 )
{
*say four.
}
If ( $TempVar > 4 )
{
*say five.
}
*say Count complete
}
This is an example of an IF ELSE statement with nested IFs that have no ELSE. The script parser will go down whichever first level of the script applies, then perform whatever 2nd level IF statement applies, and continue to the end of the first level statement and stop.
Code: Select all
Event ("&command","test")
{
$TempVar = sysRand(10)
If ( $TempVar > 5 )
{
If ( $TempVar = 6 )
{
*say six.
}
If ( $TempVar = 7 )
{
*say seven.
}
If ( $TempVar = 8 )
{
*say eight.
}
If ( $TempVar = 9 )
{
*say nine.
}
If ( $TempVar = 10 )
{
*say ten.
}
*say Script complete
}
Else
{
If ( $TempVar = 1 )
{
*say one
}
If ( $TempVar = 2 )
{
*say two.
}
If ( $TempVar = 3 )
{
*say three.
}
If ( $TempVar = 4 )
{
*say four.
}
If ( $TempVar = 5 )
{
*say five.
}
*say Script complete
}
}