Manual:DIL Manual/var

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:26, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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: MyVar and myvar are the same variable.
  • Every function, field and built-in variable of DIL is a reserved word, so name, names, npcs, objs, key, target, level and the like can't be variable names. vmc reports a syntax error with that word as the token. Use pname, 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@zone after 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.

See also