If

From EventScripts Community Encyclopedia
(Redirected from Do)


Overview

Syntax: if (<value1> <operator> <value2>) then <command>

Note: Command parameters are described inside the < and > characters. Optional parameters are contained within [ and ] characters.

Variation of this command that doesn't expand variables: es_xif

Description

Will execute command if the expression/condition in parentheses is is true.

Parameters

  • value1 - A string or numeric value to compare against value2
  • operator - if supports the following conditional operators:
    • equalto (==/=)
    • notequalto (!=)
    • greaterthan (>)
    • lessthan (<)
    • notlessthan (>=)
    • notgreaterthan (<=)
    • in - executes the conditional predicate if the first part of the condition is a substring of the second part. (first added in v1.1)
    • notin - executes the conditional predicate if the first part of the condition is not a substring of the second part. (first added in v1.2)
  • value2 - A string or numeric value to compare against value1
  • command - Command to execute if the condition is true. Can be a single command or use es_doblock to do several commands. Can also use several commands if you use a do block. (See Examples)


Examples

//Example 1:
// Works in all script types
   es_xsetinfo var 1
   if (server_var(var) > 0) then es_xmsg >0 true
   es_xmath var - 1
   if (server_var(var) > 0) then es_xmsg This should never be displayed!
 
//Example 2:
// Only works in script addons (recommended)
   if (server_var(var) < 5) do
   {
      es_xmsg <5 true
      es_xmsg multiple commands are possible with 'do'
   }
 
// Example 3:
//  An example of the "in" operator
   if ("hello" in "hello world") do
   {
      es_msg There was a "hello" found in "hello world"
   }


Notes

if (<value> <operator> <value2>) do 
{
<command1>
[...]
[commandN]
}
  • Never use if/then/do in the same line. You can use if/then or if/do, but never combine then and do!
  • Every { or } has to be on a new line.

See also

blog comments powered by Disqus