Manual:DIL Manual/dilcall()

From DikuMUD Wiki
Jump to navigation Jump to search

dilcall()

Function (integer). Calls a DIL function whose name is only known when the program runs.

dilcall(tmpl : string)(u : unitptr, i : integer, s : string) : integer

Calls the template named by the string tmpl ("name@zone") with the three arguments in the second pair of brackets, and gives back what it returns. It works like an ordinary call of a function from another zone, except that the name can be built at run time: taken from a list, read from an extra, or made with +. quests.zon uses it to run the check function named in a quest's data, randomt.zon to run an enchantment picked at random.

Parameters

Name Type Meaning
tmpl string The template's name with its zone, "name@zone".
u unitptr First argument passed on.
i integer Second argument passed on.
s string Third argument passed on.

The called template must be a function with exactly these arguments, in this order, that returns an integer:

dilbegin integer name(u : unitptr, i : integer, s : string);

Its arguments can have any names; arguments it doesn't need are still declared.

Returns

The integer the called template returns. -1 when no template of that name exists; the zone log then gets "DIL ..., Unable to find template ...". When u fails (a field of a null unit, for example) the call fails, and the variable assigned keeps its old value; a variable holding null is passed on as null.

Notes

  • The call runs in the same program, like any function call: self in the called template is still the unit running the program, not u.
  • A template whose arguments are of other types stops the program, with "... where parameter N has incorrect type. Stopping program." in the zone log. Other argument counts and procedures (templates without a return type) are not checked and leave the program's stack wrong: don't use them.
  • It must be assigned, n := dilcall(f)(u, 0, "");, or used in an expression. It can't stand alone as a statement.
  • No external declaration is needed for the called template.

Example

dilbegin integer cheer1(u : unitptr, i : integer, s : string);
code
{
   act("$1n cheers " + s + "!", A_SOMEONE, u, null, null, TO_ROOM);
   return (TRUE);
}
dilend

dilbegin integer cheer2(u : unitptr, i : integer, s : string);
code
{
   act("$1n claps " + s + ".", A_SOMEONE, u, null, null, TO_ROOM);
   return (TRUE);
}
dilend

dilbegin crowd();
var
   fn : string;
   n : integer;
code
{
   heartbeat := PULSE_SEC * 30;
   :loop:
   wait(SFB_TICK, TRUE);
   fn := "cheer" + itoa(rnd(1, 2)) + "@ex";
   n := dilcall(fn)(self, 0, "loudly");
   goto loop;
}
dilend

An NPC in a crowd that picks one of two reactions at random by name. The example zone is called ex.

See also