Difference between revisions of "Manual:DIL Manual/Assignment"
(XML import) |
(rewritten against the compiler and interpreter; examples compile) |
||
| Line 1: | Line 1: | ||
| − | + | 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 [[Manual:DIL Manual/Data Types|type]]; otherwise vmc stops with "Assigning incompatible types". Assignment is a statement, not an expression: <code>a := b := 0;</code> and <code>a += 1;</code> are syntax errors; write <code>a := a + 1;</code>. | |
| − | + | == What can be assigned == | |
| + | {| class="wikitable" | ||
| + | ! Target !! Example | ||
| + | |- | ||
| + | | The template's variables and arguments || <code>n := n + 1;</code>, <code>pc := activator;</code> | ||
| + | |- | ||
| + | | A field marked read/write on the [[Manual:DIL Manual/Fields|Fields]] page || <code>self.hp := self.max_hp;</code>, <code>u.title := "the tired";</code>, <code>exd.descr := "3";</code> | ||
| + | |- | ||
| + | | An indexed field of a character || <code>pc.skills[SKI_SEARCH] := 50;</code> | ||
| + | |- | ||
| + | | One entry of a stringlist or intlist || <code>sl.[0] := "black";</code>, <code>il.[2] := 50;</code> | ||
| + | |- | ||
| + | | The built-in variables [[Manual:DIL Manual/heartbeat|heartbeat]] and [[Manual:DIL Manual/power|power]] || <code>heartbeat := PULSE_SEC * 10;</code> | ||
| + | |} | ||
| − | + | These can't be assigned; vmc stops with "Illegal lvalue": | |
| − | + | * the other built-in variables: <code>self</code>, <code>activator</code>, <code>target</code>, <code>medium</code>, <code>argument</code>, <code>cmdstr</code> and the rest; | |
| − | + | * read-only fields, such as <code>u.name</code> (change <code>u.names</code> instead), <code>u.type</code>, <code>u.outside</code>, <code>u.inside</code>, <code>u.zone</code>, <code>u.extra</code>. Many of them are changed with a procedure instead: [[Manual:DIL Manual/link()|link()]] moves a unit, [[Manual:DIL Manual/addextra()|addextra()]] adds an extra. Each field's page says; | |
| − | + | * a character of a string: <code>s.[0] := "x";</code>. Build a new string with [[Manual:DIL Manual/left()|left()]], [[Manual:DIL Manual/right()|right()]] and <code>+</code>. | |
| − | |||
| − | |||
| − | < | ||
| − | + | An entry outside a list (<code>sl.[5]</code> in a list of three) is not created; the assignment does nothing. Make the list longer with [[Manual:DIL Manual/addstring()|addstring()]] or [[Manual:DIL Manual/insert()|insert()]]. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Fields that need a trusted zone == | |
| + | Each zone has an access level, the fourth column of <code>vme/etc/zonelist</code>, from 0 (most trusted) to 255. Programs of zones with a level other than 0 can read, but not change, these fields: <code>exp</code>; <code>abilities</code>, <code>skills</code>, <code>spells</code> and <code>weapons</code> and their <code>_levels</code>; and of players, <code>level</code>, <code>vlevel</code>, <code>profession</code>, <code>lifespan</code> and <code>max_hp</code>. 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: <code>2147483647 + 1</code> is <code>-2147483648</code>. Each field's page gives its range. | ||
| − | + | == Copies == | |
| − | + | * Assigning a string or a list copies it: after <code>sl := u.names;</code>, changing <code>sl</code> doesn't change the unit's names. To change those, assign the list back: <code>u.names := sl;</code>. | |
| − | + | * 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: <code>sl := sl;</code> leaves it empty. | |
| − | + | ||
| − | dilend</ | + | == When the value fails == |
| + | A value fails when it can't be worked out: a field of null (<code>u.level</code> when <code>u</code> is null), a division by 0, a list index outside the list. Assigning a failed value: | ||
| + | {| class="wikitable" | ||
| + | ! Variable !! Result | ||
| + | |- | ||
| + | | integer || keeps its old value | ||
| + | |- | ||
| + | | string || becomes empty (<code>""</code>) | ||
| + | |- | ||
| + | | stringlist, intlist || keeps its old value | ||
| + | |- | ||
| + | | unitptr, extraptr, zoneptr, cmdptr || keeps its old value | ||
| + | |} | ||
| + | So <code>x := u.level;</code> with <code>u</code> null leaves <code>x</code> 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: <code>s := "Hello " + u.name;</code> with <code>u</code> null stops the whole program with "Type error in string assignment", and so does using such a join as an argument (<code>sendtext("Hi " + u.name, pc)</code>). With the failed part first (<code>u.name + "!"</code>) the join just fails. | ||
| + | |||
| + | == Assigning null == | ||
| + | <code>null</code> empties any variable that isn't an integer: <code>u := null;</code>, <code>s := null;</code>, <code>sl := null;</code>. An emptied string is equal to both <code>null</code> and <code>""</code>; after <code>s := "";</code> it is equal to <code>""</code> only. Compare strings with <code>""</code>, which is true in both cases. <code>n := null;</code> doesn't compile: give an integer 0. An empty <code>{}</code> doesn't compile in code either; use <code>null</code> to empty a list. See [[Manual:DIL Manual/null|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 [[Manual:DIL Manual/global|global]] list is the exception the other way round: assigning to it (or <code>addstring</code>, <code>insert</code>, <code>remove</code>) changes the list every program with that key sees. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | 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"). <code>pet.name</code> is read-only; it is the first of <code>pet.names</code>, so it is now "Rex" too. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/Data Types|Data Types]] | ||
| + | * [[Manual:DIL Manual/Expressions|Expressions]] | ||
| + | * [[Manual:DIL Manual/Fields|Fields]] | ||
| + | * [[Manual:DIL Manual/null|null]] | ||
| + | * [[Manual:DIL Manual/var|var]] | ||
Latest revision as of 23:54, 14 November 2026
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;.
Contents
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,cmdstrand the rest; - read-only fields, such as
u.name(changeu.namesinstead),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;, changingsldoesn'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.