backIndex

Xelagot action script

Using Packer variables and creating new variables

X1 2.9999979, Av99Bot/SrvcXlgBot 2.979 onwards

Advanced stuff, not for beginners!

Packer variables, available as from X1 2.9999979 and Av99/SrvcXlgBot 2.979, can be used to represent other variables, which must be packed into them. They work a bit like pointers to variables in C and Pascal, but are not quite the same. They contain the 'name' of the variable, as a string, and use that variable in operations in which they are involved. For example:

GetVarName ^a %b
Add ^a %c %d
is equivalent to writing
Add %b %c %d

NOTE: you may not assign a value to a Packer variable using the equal sign. This is not valid:

GetVarName ^a %b
^a = 3
You should do this so:
GetVarName ^a %b
Add ^a %b 0
There may be a few more exceptions of which I am not yet aware, please let me know by email if you encounter them: xelag@3dee.nl.

The usual way of creating variables is when the script is initialised, before it is run. The scripting engine will look for lines starting with the word var and add the names of variabled declared in those lines to its internal list of variables.

Occasionally, you will want to define more variables while running the script. Up to x1 2.9999979, this was not possible. As from this version you can use the following statements to achieve this:

GetVarName ^b variable

or
GetVarName $a variable
you can pack the name of any variable in a Packer variable or a string variable using this statement, for example:
GetVarName ^b %c
is like writing ^b = "%c", which means, it turns the name of a variable into a string and packs it into the Packer variable.
^b = $a

or
^b = "/s_List20"
If string variable $a contains the name of a variable, that name will be transfered to Packer variable ^b. You may also use literal strings for this.
NOTE: the variable need not exist. If it has not yet been created, to be able to use this packer variable, you must first create it, see NewVar
$a = ^b
transfers the name of a variable (whether it exists or not) that is packed into the Packer variable ^b to string variable $a.
^b = "/s_List20"
NewVar ^b

or
$a = "/s_List20"
^b = $a
NewVar ^b

or
$a = "/s_List20"
NewVar $a
NewVar: creating new variables at runtime

define the name of the new variable, for example /s_List20, and pack it into a Packer variable, in this example in variable ^b. Then use the NewVar statement to create /s_List20.
OR
Pack it into a string variable, and create the variable using NewVar on the Packer variable.
^b = "/s_List20"
IfisVar ^b statement1
Else statement2

or
$a = "/s_List20"
^b = $a
IfisVar ^b statement1
Else statement2

or
$a = "/s_List20"
IfisVar $a statement1
Else statement2
IfisVar: you can test if a certain variable name is define (exists) as variable in your script, using the IfisVar statement. First pack the name of the variable in a Packer variable or a string variable, then test it with IfisVar, as in the example.
^b = "/s_List20"
KillVar ^b

or
$a = "/s_List20"
^b = $a
KillVar ^b
or
$a = "/s_List20"
KillVar $a
KillVar: you can destroy a variable created with NewVar using KillVar. As With NewVar and IfisVar, you may use a Packer variable or a string variable to pack the name of another variable.

A question remains: how does your script know and remember the new variable names, since they were not declared beforehand when you wrote the script? Look at the reason to create new variables: you need to be able to create new variables because unexpected circumstances occur. For example, you make a script for a bot that must serve various booths, but you do not know how many when you write the script. You plan to add the name and location of the booths in a file, and load it at runtime. In a second file you can put the names of new booth variables.

An example...

File "Booths.txt" contains the names of booths:

boothA
boothB
boothC
File BoothVars.txt has the variable names:
boothA=/s_boothA
boothB=/s_bootbB
boothC=/s_boothC
Load these files:
Var /s_Booths, /s_BoothVars, %Booths
SlistLoad /s_Booths "Booths.txt"
SListNoBlanks /s_Booths
SListLoad /s_BoothVars "BoothVars.txt"
SlistCount /s_Booths %Booths
%i = 1
Label AssignIn
  IfInt %i > %Booths Goto AssignOut
  SListGet /s_Booths %i $a
  SListGetValue /s_BoothVars $a $b
  NewVar $b
  Inc %i
  Goto AssignIn
Label AssignOut
In this example, you assign one new string list for each booth, without even knowing the names of the booths beforehand, or the names of the string lists... which are now kept in string list /s_BoothVars and can be recovered in $b at any time. The code in blue puts the name of the string list in $b, the code in red makes the new string list variable. So by simply knowing the number of the booth (in this case represented by %i) you can manipulate the string list corresponding to that booth. That is, if you need a string list to go with the booth :)

In string lists, you can store the coordinates of the booth (as a name value pair), the size of the booth, etc. This information can be stored in files: BoothA.txt, BoothB.txt, BoothC.txt. For example, BoothA.txt can have the following information:

Position=5.370n 6.0w 0a 180
Radius=10
Tenant=ImaBots
Modify the previous bit of code:
Var /s_Booths, /s_BoothVars, %Booths
SlistLoad /s_Booths "Booths.txt"
SListNoBlanks /s_Booths
SListLoad /s_BoothVars "BoothVars.txt"
SlistCount /s_Booths %Booths
%i = 1
Label AssignIn
  IfInt %i > %Booths Goto AssignOut
  SListGet /s_Booths %i $a
  SListGetValue /s_BoothVars $a $b
  NewVar $b
  ^b = $b
  Concat $f $a ".txt"
  SlistLoad ^b $f
  Inc %i
  Goto AssignIn
Label AssignOut
The code in blue makes a filename $f with the name of the booth, and loads it in the booth's string list (represented by ^b, you can not manipulate a list while it's name is assigned only to a string variable, you must use a Packer variable for that: ^b = $b).

backIndex