Manual:DIL Manual/Data Types
DIL has eight types. Every variable, argument, field and expression has one of them, fixed when vmc compiles the program, and vmc refuses a value of the wrong type ("Assigning incompatible types"). There are no other types: no floating-point numbers, no records, no arrays besides the two list types.
Contents
The eight types
| Type | Holds | Written as | Empty value |
|---|---|---|---|
| integer | A whole number, signed, 32 bits. Also DIL's truth values: 0 is false, anything else true. | 42, 0x2A, -5, constants such as TRUE or PULSE_SEC |
0 |
| string | A piece of text. | "Hello there" |
"" (or null)
|
| stringlist | An ordered list of strings: names, keywords, words. | {"red", "green"} |
an empty list |
| intlist | An ordered list of integers. | {10, -3, 0x10} |
an empty list |
| unitptr | A room, an object, an NPC or a player. | self, findroom("square@mytown") |
null |
| extraptr | One extra description of a unit (also used to store data on units). | "$visits" in u.extra |
null |
| zoneptr | A zone. | findzone("mytown"), zhead() |
null |
| cmdptr | One of the game's commands. | getcmd("look"), chead() |
null |
Variables are declared in var (n, m : integer;) or as a template's arguments, and start with the empty value. A list in braces may hold only constants; build a list from variables with addstring() or insert(). Each type's page tells how to work with it; the Fields page lists what units, extras, zones and commands hold.
Values and copies
- Integers, strings and lists are values. Assigning one variable to another, or passing it to a template, copies it: changing the copy doesn't change the original.
- unitptr, extraptr, zoneptr and cmdptr point to something that exists in the game. A copy points to the same thing, so
u.hp := 10;changes the unit whichever variable it is reached through. nullis the empty value of the pointer types, and assigning it empties strings and lists too. An integer can't be null. See null.- A global stringlist or intlist is shared by every program that declares the same key.
What a variable keeps
A program sleeps between events. What its variables hold when it wakes, and when its unit is saved and loaded again (a player and what the player carries), depends on the type:
| Type | After a wait (wait, pause, 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 (it can't be secured) | null |
| zoneptr, cmdptr | kept | null |
So keep names, numbers and texts in variables of the first kind, and find units and extras again after waiting. Saved values are thrown away when the template's variables have changed since the save. See var.
Truth values
Conditions (if, while, wait, and, or, not) take a value of any type:
- an integer is true when it isn't 0;
- a unitptr, extraptr, zoneptr, cmdptr or string is true when it isn't null. A string that holds
""after an assignment is true, so test text withs != ""; - a stringlist or intlist is true when it has at least one entry;
- a failed value (below) is false.
Failed values
Some expressions give no value at all: a field of null (u.level when u is null), a field the unit doesn't have, a division by 0, a list index outside the list, and most functions given a failed argument. Such a value fails: it is false in every comparison and condition, and assigning it leaves an integer, list or pointer variable as it was and empties a string variable. See Expressions and Assignment.
Converting between types
DIL never converts by itself: "You have " + 5 and if (n == "5") don't compile. The functions that convert:
| From | To | How |
|---|---|---|
| integer | string | itoa(n): "You have " + itoa(n) + " coins."
|
| string | integer | atoi(s): the number at the start of the text, 0 when there is none |
| string | stringlist | getwords(s) (words) or split(s, sep) (pieces between separators) |
| stringlist | string | a loop over the entries, joining them with +; sl.[i] is one entry
|
| string | unitptr | findsymbolic("name@zone"), findroom("name@zone"), or findunit() for a name as players type it |
| unitptr | string | u.name; its symbolic name is u.nameidx + "@" + u.zoneidx (the macro SYMNAME(u) in wmacros.h)
|
| string | zoneptr | findzone(name); a unit's zone is findzone(u.zone)
|
| string | cmdptr | getcmd(word) |
| string | extraptr | "name" in u.extra, or u.extra.["name"]
|
An extra description is the usual place for data on a unit: its text (exd.descr) holds a string, its vals an intlist. A cast such as (integer) exists only for calling a template whose name is in a string (see Functions).
Comparing
== and != work between two values of the same type. On strings they ignore case. On units, extras, zones and commands they tell whether both are the same thing. On two lists they tell whether both are the same list, not whether they hold the same entries: two list variables are never equal, even with the same contents. Comparing a list with null tells whether it is empty. See Operators.