
Scripting: If/End While/End
# some basic scripting information
Every script generally has some basic elements in common so here are two of them. You will note that in between the If/End and while/end sections, the text is indented. This is to allow easier visual inspection of the structure and makes debugging a lot easier. No, you don't have to do it that way, but most people do.
The "IF" decision structure is basically looking to see if something
is true or false. If it is true then it does the next line after the evaluation.
If/End If/ElseIf/End
# note: // end of line comments are not currently supported by twx
# use the # above a line to comment within a script
setVar $x 1 // sets the variable $x to 1
if ($x = 1) // we want to know if the value in the variable $x = 1
echo "*" $x // echos to the client screen (* is the enter command)
end // we are done with our check
--------------------------------------------------
getRnd $x 1 10 // get a random number in the range of 1 to 10 and put in variable $x
if ($x = 1) // see if $x = 1
echo "*Value of $x is: " $x // displaying $x
elseif ($x = 10) // see if it was 10
echo "*Value of $x was 10" // just saying $x = 10
else // value was neither of the above
echo "*Value was neither 1 or 10"
echo "*$x = " $x
end
--------------------------------------------------
Loops are a common method of doing something a certain number
of times or until something changes value in the loop. The script
help .html file contains an example of reading a file that will be of
interest.
Endless loops are a very bad thing that can easily max your cpu,
and besides, you don't do anything except spin in a circle. You
will have to kill TWX to get out of it. (ctrl-alt-delete and then
select TWX to kill).
While/End
setVar $x 0 // set the variable $x to 0
while ($x < 10) // while $x is less than 10 loop
add $x 1 // add 1 to $x otherwise this is an endless loop
echo "*" $x // echo the value of $x to the client
end // end of while
--------------------------------------------------
setVar $x 0
while ($x < 10)
add $x 1
if ($x = 7)
echo "*$x reached the value of 7"
end
end
--------------------------------------------------
The positioning of your add $x 1 can affect your output. In the
first example of while ($x < 10) it showed values of 1 - 10. The
next will show values of 0 - 9.
setVar $x 0
while ($x < 10)
echo "*" $x
add $x 1
end
--------------------------------------------------
If you wanted values of 0 - 10 to show in the above example, you
could change "while ($x < 10)" to "while ($x <= 10)".
--------------------------------------------------
# putting a while loop to use
# using ANSI_12 to output the echo in red (try the different ANSI_xx values)
# CURRENTSECTOR gets our current sector from TWX
# $i is initialized to 1, because
# sector.warps[CURRENTSECTOR][0] would not be valid.
setVar $warpCnt sector.warpcount[CURRENTSECTOR] // get the warpcount of the current sector from TWX's database
setVar $i 1 // init our var to 1
while ($i <= $warpCnt) // setup while loop value
echo ANSI_12 "*" sector.warps[CURRENTSECTOR][$i] // display adjacent warps contained in TWX's database
add $i 1 // increment counter
end // end of while loop
halt // stop script
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."