Difference between revisions of "Manual:DIL Manual/findunit()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = findunit() = | |
| − | + | '''Function''' (unitptr). Finds a unit by name, the way commands find what a player names. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | findunit(u : unitptr, s : string, flags : integer, list : unitptr) : unitptr | |
| − | + | findunit(u : unitptr, s : string, flags : integer, list : unitptr, type : integer) : unitptr | |
| − | + | Looks for a unit called <code>s</code> in the places <code>flags</code> names, as seen by <code>u</code>: what it wears, carries, what is around it, and so on. It reads names the way the game's commands do, so it is the right tool for the words a player typed: "sword", "the 2.sword", "long sword". To find one particular NPC or object of a zone, use [[Manual:DIL Manual/findsymbolic()|findsymbolic()]] instead; a name can belong to anyone, players included. | |
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! Name !! Type !! Meaning | |
| + | |- | ||
| + | | u || unitptr || The unit that looks. The places searched are relative to it, and when it is a character it only finds what it can see. | ||
| + | |- | ||
| + | | s || string || The name to look for. Leading filler words (a, an, at, from, in, into, of, on, the, to, with) are skipped, and <code>2.sword</code> means the second sword. | ||
| + | |- | ||
| + | | flags || integer || Where to look, <code>FIND_UNIT_*</code> from <code>vme/include/vme.h</code>, joined with <code>|</code>: <code>FIND_UNIT_EQUIP</code> (what <code>u</code> wears), <code>FIND_UNIT_INVEN</code> (what it carries), <code>FIND_UNIT_SURRO</code> (its surroundings: the room it is in and the units there), <code>FIND_UNIT_ZONE</code> (anywhere in the zone <code>u</code> is in), <code>FIND_UNIT_WORLD</code> (anywhere). <code>FIND_UNIT_IN_ME</code> is equip and inventory, <code>FIND_UNIT_HERE</code> adds the surroundings, <code>FIND_UNIT_GLOBAL</code> is all of them. 0 searches only <code>list</code>. | ||
| + | |- | ||
| + | | list || unitptr || null, or a unit to search together with the units after it in the same place. To search what a container holds, pass its <code>inside</code>. | ||
| + | |- | ||
| + | | type || integer || Optional: the kinds of unit wanted, <code>UNIT_ST_PC</code>, <code>UNIT_ST_NPC</code>, <code>UNIT_ST_OBJ</code>, <code>UNIT_ST_ROOM</code>, joined with <code>|</code>. Without it, any kind. | ||
| + | |} | ||
| − | + | == Returns == | |
| + | The unit found, or null. When <code>u</code> is null or <code>s</code> is empty, the call fails and the variable assigned keeps its old value: clear it first if you test it for null afterwards. | ||
| − | + | == Notes == | |
| + | * '''It changes <code>s</code>.''' When <code>s</code> is a string variable (or <code>argument</code>), the words it matched, and the filler words before them, are taken off its start. Copy the string first if you need it again. | ||
| + | * When names of different lengths fit the start of <code>s</code>, the longest wins: for "long sword", a unit named "long sword" beats one named "long". Among equally long matches the order is equipment, inventory, surroundings, zone, world, list. | ||
| + | * <code>"self"</code> with <code>FIND_UNIT_SURRO</code> finds <code>u</code> itself. | ||
| + | * When nothing matches the whole word and <code>u</code> is a player, a word of up to three letters also matches the start of a name: "bra" finds Brawla. | ||
| + | * When <code>u</code> is a character it must be able to see the unit: not blind, light in the room, the unit not invisible (unless <code>u</code> detects invisible), not buried, and not wizard-invisible above <code>u</code>'s level. Gods from <code>CREATOR_LEVEL</code> up see in the dark and see invisible units. | ||
| + | * When <code>u</code> is not a character (an object or a room running the program), <code>flags</code> are ignored and only <code>list</code> is searched. | ||
| + | * <code>FIND_UNIT_PAY</code> and <code>FIND_UNIT_NOPAY</code> are ignored here. | ||
| + | * <code>FIND_UNIT_ZONE</code> and <code>FIND_UNIT_WORLD</code> go through every unit in the game; to find a player by exact name, walk the player list from <code>pc_head()</code> instead. | ||
| − | findunit( | + | == Example == |
| + | <pre> | ||
| + | dilbegin do_poke(arg : string); | ||
| + | var | ||
| + | u : unitptr; | ||
| + | code | ||
| + | { | ||
| + | u := null; | ||
| + | if (arg != "") | ||
| + | u := findunit(self, arg, FIND_UNIT_SURRO, null, UNIT_ST_PC | UNIT_ST_NPC); | ||
| + | if (u == null) | ||
| + | { | ||
| + | sendtext("Poke whom?<br/>", self); | ||
| + | quit; | ||
| + | } | ||
| + | act("You poke $3n.", A_ALWAYS, self, null, u, TO_CHAR); | ||
| + | act("$1n pokes you.", A_SOMEONE, self, null, u, TO_VICT); | ||
| + | act("$1n pokes $3n.", A_SOMEONE, self, null, u, TO_NOTVICT); | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | A player command that finds a character in the room by the name typed. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/findsymbolic()|findsymbolic()]] | |
| − | + | * [[Manual:DIL Manual/findrndunit()|findrndunit()]] | |
| − | + | * [[Manual:DIL Manual/findroom()|findroom()]] | |
| − | + | * [[Manual:DIL Manual/getword()|getword()]] | |
| − | + | * [[Manual:DIL Manual/pc head()|pc head()]] | |
| − | + | * [[Manual:DIL Manual/visible()|visible()]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:18, 14 November 2026
findunit()
Function (unitptr). Finds a unit by name, the way commands find what a player names.
findunit(u : unitptr, s : string, flags : integer, list : unitptr) : unitptr findunit(u : unitptr, s : string, flags : integer, list : unitptr, type : integer) : unitptr
Looks for a unit called s in the places flags names, as seen by u: what it wears, carries, what is around it, and so on. It reads names the way the game's commands do, so it is the right tool for the words a player typed: "sword", "the 2.sword", "long sword". To find one particular NPC or object of a zone, use findsymbolic() instead; a name can belong to anyone, players included.
Parameters
| Name | Type | Meaning |
|---|---|---|
| u | unitptr | The unit that looks. The places searched are relative to it, and when it is a character it only finds what it can see. |
| s | string | The name to look for. Leading filler words (a, an, at, from, in, into, of, on, the, to, with) are skipped, and 2.sword means the second sword.
|
| flags | integer | : FIND_UNIT_EQUIP (what u wears), FIND_UNIT_INVEN (what it carries), FIND_UNIT_SURRO (its surroundings: the room it is in and the units there), FIND_UNIT_ZONE (anywhere in the zone u is in), FIND_UNIT_WORLD (anywhere). FIND_UNIT_IN_ME is equip and inventory, FIND_UNIT_HERE adds the surroundings, FIND_UNIT_GLOBAL is all of them. 0 searches only list.
|
| list | unitptr | null, or a unit to search together with the units after it in the same place. To search what a container holds, pass its inside.
|
| type | integer | . Without it, any kind. |
Returns
The unit found, or null. When u is null or s is empty, the call fails and the variable assigned keeps its old value: clear it first if you test it for null afterwards.
Notes
- It changes
s. Whensis a string variable (orargument), the words it matched, and the filler words before them, are taken off its start. Copy the string first if you need it again. - When names of different lengths fit the start of
s, the longest wins: for "long sword", a unit named "long sword" beats one named "long". Among equally long matches the order is equipment, inventory, surroundings, zone, world, list. "self"withFIND_UNIT_SURROfindsuitself.- When nothing matches the whole word and
uis a player, a word of up to three letters also matches the start of a name: "bra" finds Brawla. - When
uis a character it must be able to see the unit: not blind, light in the room, the unit not invisible (unlessudetects invisible), not buried, and not wizard-invisible aboveu's level. Gods fromCREATOR_LEVELup see in the dark and see invisible units. - When
uis not a character (an object or a room running the program),flagsare ignored and onlylistis searched. FIND_UNIT_PAYandFIND_UNIT_NOPAYare ignored here.FIND_UNIT_ZONEandFIND_UNIT_WORLDgo through every unit in the game; to find a player by exact name, walk the player list frompc_head()instead.
Example
dilbegin do_poke(arg : string);
var
u : unitptr;
code
{
u := null;
if (arg != "")
u := findunit(self, arg, FIND_UNIT_SURRO, null, UNIT_ST_PC | UNIT_ST_NPC);
if (u == null)
{
sendtext("Poke whom?<br/>", self);
quit;
}
act("You poke $3n.", A_ALWAYS, self, null, u, TO_CHAR);
act("$1n pokes you.", A_SOMEONE, self, null, u, TO_VICT);
act("$1n pokes $3n.", A_SOMEONE, self, null, u, TO_NOTVICT);
quit;
}
dilend
A player command that finds a character in the room by the name typed.