Manual:DIL Manual/var
Jump to navigation
Jump to search
var
Keyword. Starts the variable declarations of a template.
var name : type; name, name, ... : type; name : stringlist global key@zone;
The var section comes after the header and the external section, and before code. It is optional. Each line names one or more variables and their type, and ends with a semicolon (vmc lets the last one go without).
The template's arguments are variables too, declared in the header instead. Every run of a template, and every call of it, gets its own set of variables, which start empty: 0, "", an empty list, or null.
What a variable keeps
| Type | After a wait (pause, wait, a heartbeat) | When the unit is saved and loaded |
|---|---|---|
| integer, string, stringlist, intlist | kept | saved with the unit |
| unitptr | null, unless the unit is secured |
null |
| extraptr | null | null |
| zoneptr, cmdptr | kept | null |
Notes
- Names ignore case:
MyVarandmyvarare the same variable. - Every function, field and built-in variable of DIL is a reserved word, so
name,names,npcs,objs,key,target,leveland the like can't be variable names. vmc reports a syntax error with that word as the token. Usepname,u,kw. - A variable can't have the same name as another variable or an argument ("Duplicate variable identifier").
- A template has at most 255 variables, arguments included.
- Saved values are thrown away, and the program starts over, when the template's variables have changed (names, types or order) since the unit was saved.
global key@zoneafter the type shares one value between every program that names the same key; see global.
Example
dilbegin greeter(greeting : string);
var
pc : unitptr;
seen : stringlist;
count, i : integer;
code
{
:start:
wait(SFB_DONE, command(CMD_AUTO_ENTER) and (activator.type == UNIT_ST_PC)
and (activator.outside == self.outside));
pc := activator;
if (pc.name in seen)
goto start;
addstring(seen, pc.name);
count := count + 1;
exec("say " + greeting + ", " + pc.name + "! You are guest number "
+ itoa(count) + ".", self);
goto start;
}
dilend
An NPC that greets each player who walks in, once, and counts them; seen and count keep their values between waits, pc is set again after each one. Adapted from gm_greet in commands.zon.