Manual:DIL Manual/global
Jump to navigation
Jump to search
Contents
global
Keyword. Makes a variable shared by every program that names the same key.
var name : stringlist global key@zone; name : intlist global key@zone;
A variable declared with global key@zone is not the program's own: every variable in any template, in any zone, declared with the same key is the same list. A change made by one program is seen at once by all the others. The list lives in the server's memory from the first time a program uses the key until the server stops.
The variable's own name is free; the key is what counts. randomt.zon fills its material tables once at boot (constants@randomt, called from initial_prg@basis) and its other templates read them, declaring only the ones they need.
Notes
- Use it for
stringlistandintlistonly. vmc also accepts other types, but a globalstringis not shared (each program sees its own value), and aninteger,unitptr,extraptr,zoneptrorcmdptrglobal makes the server fail an assertion when the program starts. - One name per line:
a, b : stringlist global key@zone;stops vmc with "var global cannot define multiple names". - The key needs both parts,
name@zone, or vmc says "var global missing name@zone". The zone part is only part of the key: it doesn't have to be a real zone, but by habit it is the zone that owns the data. - The key's case matters:
Guests@exandguests@exare two lists. - All programs must agree on the type. A program that declares a key as an intlist when it already exists as a stringlist (or the other way) logs "DIL ERROR: getDilGlobalDilVar() type error" and gets a broken variable.
- Globals are never saved: they start empty at every boot, and a saved program doesn't store them. Fill them at boot (from
initial_prg@basis) or the first time they are needed (testlength(list) == 0). - Assigning to a global (
list := {...};),addstring,substring,insertandremoveall change the shared list.
Example
dilbegin guestbook();
var
guests : stringlist global guests@ex;
pc : unitptr;
code
{
:start:
wait(SFB_DONE, command(CMD_AUTO_ENTER) and (activator.type == UNIT_ST_PC)
and (activator.outside == self));
pc := activator;
if (not (pc.name in guests))
addstring(guests, pc.name);
goto start;
}
dilend
dilbegin do_guests(arg : string);
var
sl : stringlist global guests@ex;
i : integer;
code
{
sendtext("Guests since the last boot:<br/>", self);
while (i < length(sl))
{
sendtext(sl.[i] + "<br/>", self);
i := i + 1;
}
quit;
}
dilend
guestbook, on a room, notes every player who walks in; do_guests, a command, lists them from the same global list.