Difference between revisions of "Manual:DIL Manual/command()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfcomm"></span>
+
= command() =
−
integer command ( cmd : string or integer )
+
'''Function''' (integer). Tells whether the event that woke the program is a given command.
−
    cmd : A string of the full typed command, e.g. "push" or "say".
 
−
          Alternatively you can specify one of the macros defined in
 
−
          values.h and/or vme.h, e.g. CMD_SAY
 
−
  return: Whether the command specified by the activator is the one of
 
−
          the argument.
 
  
−
  It is noteworthy, that unlike simple compares like this;
+
  command(name : string)
 +
command(no : integer)
  
−
if ("spook" in cmdstr)
+
Right after a <code>wait()</code>, <code>command()</code> tells what woke the program up. With a string it is TRUE when the character typed that command, the way the game itself understands commands: <code>command("say")</code> is true for "say hello" and for any abbreviation the game takes as <code>say</code>. With an integer it compares the event's command number with one of the <code>CMD_AUTO_*</code> constants, for events that aren't typed: a heartbeat tick, a combat round, a death, and so on.
−
or
 
−
if (cmdstr == "spook")
 
−
where the first will activate even if the cmdstr is "nospook", and the
 
−
second only if cmdstr equals the word "spook" to the letter, the
 
−
following construct will activate as long as the contents
 
−
of cmdstr doesn't conflict with cmd;
 
  
−
if (command("spook"))
+
It is mostly used in the condition of <code>wait()</code>: <code>wait(SFB_CMD, command("pull"))</code>.
−
ie, it will receive the same kind of treatment as a 'regular' command.
 
−
That means that you provide ease of use to the user (allowing shorthand
 
−
notation), while securing that you're doing what the user wants.
 
−
<STRONG>CAVEAT</STRONG> Builder:
 
−
If you use a string argument as cmd, be sure that
 
−
there are no white-space in it, ie, that the argument does only
 
−
consist of letters.
 
  
−
Example:
+
== Parameters ==
−
    command("spook");
+
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| name || string || One command word, in lower case, such as <code>"say"</code> or <code>"pull"</code>.
 +
|-
 +
| no || integer || A <code>CMD_AUTO_*</code> constant from <code>vme/include/vme.h</code>.
 +
|}
  
−
    is a valid string, while this construct;
+
== Returns ==
 +
Integer: 1 if the event is that command, otherwise 0. It fails if the argument fails.
  
−
    command("spook him");
+
== Notes ==
 +
* '''A command the game knows''' (one in <code>commands.def</code>): the string is looked up like a typed word, and the command it stands for is compared with the command the character typed. So it doesn't matter how the character abbreviated it, and <code>command("say")</code> is safer than <code>cmdstr == "say"</code> or <code>"say" in cmdstr</code> (which is also true for "essay").
 +
* '''A word the game doesn't know''' (<code>"pull"</code>, <code>"ring"</code>): it matches when the character typed that word or the start of it ("pul"), as long as what was typed isn't itself taken as a real command (a short start such as "pu" usually is).
 +
* The lookup of a known command is case sensitive: write the name in lower case. <code>command("Say")</code> never matches.
 +
* Give one word. <code>cmdstr</code> holds the command word and <code>argument</code> the rest of the line.
 +
* The integer form: <code>CMD_AUTO_TICK</code> (heartbeat), <code>CMD_AUTO_COMBAT</code> (combat round), <code>CMD_AUTO_DAMAGE</code>, <code>CMD_AUTO_DEATH</code>, <code>CMD_AUTO_MSG</code> (a message from <code>sendto()</code>, <code>sendtoall()</code> or <code>sendtoalldil()</code>), <code>CMD_AUTO_ENTER</code>, <code>CMD_AUTO_EXTRACT</code>, <code>CMD_AUTO_PLAY</code>, <code>CMD_AUTO_LEAVE</code>, <code>CMD_AUTO_SAVE</code>, <code>CMD_AUTO_EDIT</code> and <code>CMD_AUTO_UNKNOWN</code> (a word that isn't a command), all in <code>vme.h</code>. Every command from <code>commands.def</code> has the number 0 (<code>CMD_AUTO_NONE</code>). There are no constants such as <code>CMD_SAY</code>.
 +
* Like <code>activator</code> and <code>argument</code>, it describes the latest event only. After a <code>pause</code> or another <code>wait</code> it describes that one.
 +
* <code>command</code> is a reserved word, so it can't be a variable name.
  
−
    is NOT valid. The reason of that is that cmdstr only contains the
+
== Example ==
−
    FIRST word of the input from the player, and thus the latter
+
<pre>
−
    construct could never evaluate to true. This should be evident
+
dilbegin bell_ringer();
−
    taking the above information into account, as "spook him" could
+
code
−
    never equal "spook" (which would indeed be the text in cmdstr
+
{
−
    if the player entered the string "spook him".)
+
  heartbeat := PULSE_SEC * 30;
−
<span id="bfcomm"></span>
+
:start:
−
integer command ( cmd : string or integer )
+
  wait(SFB_CMD | SFB_TICK, command(CMD_AUTO_TICK) or command("ring"));
−
    cmd : A string of the full typed command, e.g. "push" or "say".
+
  if (command(CMD_AUTO_TICK))
−
          Alternatively you can specify one of the macros defined in
+
  {
−
          values.h and/or vme.h, e.g. CMD_SAY
+
      act("The bell sways in the wind.", A_ALWAYS, self, null, null, TO_ROOM);
−
  return: Whether the command specified by the activator is the one of
+
      goto start;
−
          the argument.
+
  }
 +
  if (activator.type != UNIT_ST_PC)
 +
      goto start;
 +
  block;
 +
  act("$1n rings the bell. DONG!", A_ALWAYS, activator, null, null, TO_ROOM);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
A bell that sways every 30 seconds and that players can ring; <code>block</code> stops the game from answering "ring is not a known command".
  
−
It is noteworthy, that unlike simple compares like this;
+
== See also ==
−
 
+
* [[Manual:DIL Manual/wait()|wait()]]
−
if ("spook" in cmdstr)
+
* [[Manual:DIL Manual/cmdstr|cmdstr]]
−
or
+
* [[Manual:DIL Manual/argument|argument]]
−
if (cmdstr == "spook")
+
* [[Manual:DIL Manual/activator|activator]]
−
where the first will activate even if the cmdstr is "nospook", and the
+
* [[Manual:DIL Manual/block|block]]
−
second only if cmdstr equals the word "spook" to the letter, the
+
* [[Manual:DIL Manual/chead()|chead()]]
−
following construct will activate as long as the contents
 
−
of cmdstr doesn't conflict with cmd;
 
−
 
 
−
if (command("spook"))
 
−
ie, it will receive the same kind of treatment as a 'regular' command.
 
−
That means that you provide ease of use to the user (allowing shorthand
 
−
notation), while securing that you're doing what the user wants.
 
−
<STRONG>CAVEAT</STRONG> Builder:
 
−
If you use a string argument as cmd, be sure that
 
−
there are no white-space in it, ie, that the argument does only
 
−
consist of letters.
 
−
 
 
−
Example:
 
−
    command("spook");
 
−
 
 
−
    is a valid string, while this construct;
 
−
 
 
−
    command("spook him");
 
−
 
 
−
    is NOT valid. The reason of that is that cmdstr only contains the
 
−
    FIRST word of the input from the player, and thus the latter
 
−
    construct could never evaluate to true. This should be evident
 
−
    taking the above information into account, as "spook him" could
 
−
    never equal "spook" (which would indeed be the text in cmdstr
 
−
    if the player entered the string "spook him".)
 

Latest revision as of 23:15, 14 November 2026

command()

Function (integer). Tells whether the event that woke the program is a given command.

command(name : string)
command(no : integer)

Right after a wait(), command() tells what woke the program up. With a string it is TRUE when the character typed that command, the way the game itself understands commands: command("say") is true for "say hello" and for any abbreviation the game takes as say. With an integer it compares the event's command number with one of the CMD_AUTO_* constants, for events that aren't typed: a heartbeat tick, a combat round, a death, and so on.

It is mostly used in the condition of wait(): wait(SFB_CMD, command("pull")).

Parameters

Name Type Meaning
name string One command word, in lower case, such as "say" or "pull".
no integer A CMD_AUTO_* constant from vme/include/vme.h.

Returns

Integer: 1 if the event is that command, otherwise 0. It fails if the argument fails.

Notes

  • A command the game knows (one in commands.def): the string is looked up like a typed word, and the command it stands for is compared with the command the character typed. So it doesn't matter how the character abbreviated it, and command("say") is safer than cmdstr == "say" or "say" in cmdstr (which is also true for "essay").
  • A word the game doesn't know ("pull", "ring"): it matches when the character typed that word or the start of it ("pul"), as long as what was typed isn't itself taken as a real command (a short start such as "pu" usually is).
  • The lookup of a known command is case sensitive: write the name in lower case. command("Say") never matches.
  • Give one word. cmdstr holds the command word and argument the rest of the line.
  • The integer form: CMD_AUTO_TICK (heartbeat), CMD_AUTO_COMBAT (combat round), CMD_AUTO_DAMAGE, CMD_AUTO_DEATH, CMD_AUTO_MSG (a message from sendto(), sendtoall() or sendtoalldil()), CMD_AUTO_ENTER, CMD_AUTO_EXTRACT, CMD_AUTO_PLAY, CMD_AUTO_LEAVE, CMD_AUTO_SAVE, CMD_AUTO_EDIT and CMD_AUTO_UNKNOWN (a word that isn't a command), all in vme.h. Every command from commands.def has the number 0 (CMD_AUTO_NONE). There are no constants such as CMD_SAY.
  • Like activator and argument, it describes the latest event only. After a pause or another wait it describes that one.
  • command is a reserved word, so it can't be a variable name.

Example

dilbegin bell_ringer();
code
{
   heartbeat := PULSE_SEC * 30;
:start:
   wait(SFB_CMD | SFB_TICK, command(CMD_AUTO_TICK) or command("ring"));
   if (command(CMD_AUTO_TICK))
   {
      act("The bell sways in the wind.", A_ALWAYS, self, null, null, TO_ROOM);
      goto start;
   }
   if (activator.type != UNIT_ST_PC)
      goto start;
   block;
   act("$1n rings the bell. DONG!", A_ALWAYS, activator, null, null, TO_ROOM);
   goto start;
}
dilend

A bell that sways every 30 seconds and that players can ring; block stops the game from answering "ring is not a known command".

See also