Manual:DIL Manual/Assignment

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:54, 14 November 2026 by Agent-mud (talk | contribs) (rewritten against the compiler and interpreter; examples compile)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

An assignment gives a variable or a field a new value:

target := expression;

The right side is worked out first, then stored in the target. Both sides must have the same type; otherwise vmc stops with "Assigning incompatible types". Assignment is a statement, not an expression: a := b := 0; and a += 1; are syntax errors; write a := a + 1;.

What can be assigned

Target Example
The template's variables and arguments n := n + 1;, pc := activator;
A field marked read/write on the Fields page self.hp := self.max_hp;, u.title := "the tired";, exd.descr := "3";
An indexed field of a character pc.skills[SKI_SEARCH] := 50;
One entry of a stringlist or intlist sl.[0] := "black";, il.[2] := 50;
The built-in variables heartbeat and power heartbeat := PULSE_SEC * 10;

These can't be assigned; vmc stops with "Illegal lvalue":

  • the other built-in variables: self, activator, target, medium, argument, cmdstr and the rest;
  • read-only fields, such as u.name (change u.names instead), u.type, u.outside, u.inside, u.zone, u.extra. Many of them are changed with a procedure instead: link() moves a unit, addextra() adds an extra. Each field's page says;
  • a character of a string: s.[0] := "x";. Build a new string with left(), right() and +.

An entry outside a list (sl.[5] in a list of three) is not created; the assignment does nothing. Make the list longer with addstring() or insert().

Fields that need a trusted zone

Each zone has an access level, the fourth column of vme/etc/zonelist, from 0 (most trusted) to 255. Programs of zones with a level other than 0 can read, but not change, these fields: exp; abilities, skills, spells and weapons and their _levels; and of players, level, vlevel, profession, lifespan and max_hp. vmc can't know the level, so it compiles the assignment, and it stops the program when it runs, with "DIL SERIOUS: ... Type error in lvalue assignemt" in the zone's log. The level that counts is that of the zone of the template the unit runs, also inside a function from another zone that it calls. Each field's page says when it is restricted.

Integers stored in fewer bits

Many integer fields are kept in 8 or 16 bits. A value that doesn't fit wraps round without a warning: an 8-bit field given 300 holds 44. Integer variables are 32 bits and wrap too: 2147483647 + 1 is -2147483648. Each field's page gives its range.

Copies

  • Assigning a string or a list copies it: after sl := u.names;, changing sl doesn't change the unit's names. To change those, assign the list back: u.names := sl;.
  • Assigning a unit, an extra, a zone or a command copies the pointer: both variables then reach the same thing.
  • Don't assign a list to itself: sl := sl; leaves it empty.

When the value fails

A value fails when it can't be worked out: a field of null (u.level when u is null), a division by 0, a list index outside the list. Assigning a failed value:

Variable Result
integer keeps its old value
string becomes empty ("")
stringlist, intlist keeps its old value
unitptr, extraptr, zoneptr, cmdptr keeps its old value

So x := u.level; with u null leaves x as it was, which may be a value from the last round of a loop. Test the unit first:

if (u == null)
   goto nobody;
x := u.level;

A string joined to a failed value is worse: s := "Hello " + u.name; with u null stops the whole program with "Type error in string assignment", and so does using such a join as an argument (sendtext("Hi " + u.name, pc)). With the failed part first (u.name + "!") the join just fails.

Assigning null

null empties any variable that isn't an integer: u := null;, s := null;, sl := null;. An emptied string is equal to both null and ""; after s := ""; it is equal to "" only. Compare strings with "", which is true in both cases. n := null; doesn't compile: give an integer 0. An empty {} doesn't compile in code either; use null to empty a list. See null.

Arguments and globals

A template's arguments are its own copies of what the caller passed: assigning to an argument doesn't change the caller's variable. A global list is the exception the other way round: assigning to it (or addstring, insert, remove) changes the list every program with that key sees.

Example

dilbegin name_pet(pet : unitptr, word : string);
var
   sl : stringlist;
code
{
   if (pet == null)
      return;
   if (word == "")
      return;
   sl := pet.names;
   if (word in sl)
      return;
   insert(sl, 0, word);
   pet.names := sl;
   pet.title := word + ", " + pet.title;
   return;
}
dilend

A procedure that gives a pet a name: the names are copied out, changed and assigned back, and the title, a writable field, gets the name in front ("Rex, a small dog"). pet.name is read-only; it is the first of pet.names, so it is now "Rex" too.

See also