Hardrock/Sandbox – User
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 22: | Line 22: | ||
|p5= else_code: [[String]] - the code, which is executed if the condition equals false. See above for the code block syntax. |= | |p5= else_code: [[String]] - the code, which is executed if the condition equals false. See above for the code block syntax. |= | ||
| [[Any Type]] - If the condition is fulfilled, the last given value of the if_code code block is returned (if not followed by a semicolon). Else [[Nothing]] is returned. [[Any Type]] If the condition is fulfilled, the last given value of the if_code code block, else the last given value of the else_code code block is returned (if not followed by a semicolon).'''Example 1:'''<code>if (canFire tank) then {grp1 setCombatMode "BLUE"} else {grp1 setCombatMode "RED"}</code> '''Example 2:'''<code>_text = if (alive player) then {"alive"} else {"not alive"}hint _text</code> outputs the message "alive", if the player is alive, and "not alive" if not.Additional Syntax in Scripts OFP's scripting language provides another language structure with a similar task: '? condition : code'. This structure may be only used in scripts although (see [[Chapter 1. Scripts (*.sqs)]]). It is less strict than 'if ... then'; for 'if ... then' conditions have to be embraced by two brackets, while for '? ... :' they don't.<code>? alive player : hint "yes"</code> This code is completely valid, while you have to add the brackets, if you want to use the 'if ... then' syntax:<code>if (alive player) then {hint "yes"}</code> Â |= | | [[Any Type]] - If the condition is fulfilled, the last given value of the if_code code block is returned (if not followed by a semicolon). Else [[Nothing]] is returned. [[Any Type]] If the condition is fulfilled, the last given value of the if_code code block, else the last given value of the else_code code block is returned (if not followed by a semicolon).'''Example 1:'''<code><nowiki>if (canFire tank) then {grp1 setCombatMode "BLUE"} else {grp1 setCombatMode "RED"}</nowiki></code> '''Example 2:'''<code><nowiki>_text = if (alive player) then {"alive"} else {"not alive"}hint _text</nowiki></code> outputs the message "alive", if the player is alive, and "not alive" if not.Additional Syntax in Scripts OFP's scripting language provides another language structure with a similar task: '? condition : code'. This structure may be only used in scripts although (see [[Chapter 1. Scripts (*.sqs)]]). It is less strict than 'if ... then'; for 'if ... then' conditions have to be embraced by two brackets, while for '? ... :' they don't.<code><nowiki>? alive player : hint "yes"</nowiki></code> This code is completely valid, while you have to add the brackets, if you want to use the 'if ... then' syntax:<code><nowiki>if (alive player) then {hint "yes"}</nowiki></code> Â |= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
|x1= <code>if (!canFire tank) then {grp1 setCombatMode "RED"}</code> Note You have to take care, that combined conditions are embraced by parenthesises. Wrong: if (alive player) && (someAmmo player) then {hint "yes"} Right: if ((alive player) && (someAmmo player)) then {hint "yes"}Syntax with Two Code BlocksYou may define another code block, which is executed if the condition doesn't fulfill. |= | |x1= <code><nowiki>if (!canFire tank) then {grp1 setCombatMode "RED"}</nowiki></code> Note You have to take care, that combined conditions are embraced by parenthesises. Wrong: if (alive player) && (someAmmo player) then {hint "yes"} Right: if ((alive player) && (someAmmo player)) then {hint "yes"}Syntax with Two Code BlocksYou may define another code block, which is executed if the condition doesn't fulfill. |= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
Revision as of 04:47, 1 August 2006
Description
- Description:
- Syntax with One Code BlockThis structure checks for a condition and executes a code block, if the condition equals true. Additionally you may define a code block, which is executed if the condition equals false.
- Groups:
- Uncategorised
Syntax
- Syntax:
- if (condition) then if_code if (condition) then if_code else else_code
- Parameters:
- condition: Boolean - either a variable or any comparison or code that returns either true or false
- if_code: String - the code, which is executed if the condition equals true. The code is usually written in curly braces ('{', '}'), but you may also use quotes (see String Notation). You have to seperate different commands with a semicolon (';'). The rules for code blocks are equal to those for functions (see Chapter 2. Functions (*.sqf)). Variables used in code blocks have to defined previously, otherwise they will be deleted after its execution.
- condition: Boolean - either a variable or any comparison or code that returns either true or false
- if_code: String - the code, which is executed if the condition equals true. See above for the code block syntax.
- else_code: String - the code, which is executed if the condition equals false. See above for the code block syntax.
- Return Value:
- Any Type - If the condition is fulfilled, the last given value of the if_code code block is returned (if not followed by a semicolon). Else Nothing is returned. Any Type If the condition is fulfilled, the last given value of the if_code code block, else the last given value of the else_code code block is returned (if not followed by a semicolon).Example 1:
if (canFire tank) then {grp1 setCombatMode "BLUE"} else {grp1 setCombatMode "RED"}
Example 2:_text = if (alive player) then {"alive"} else {"not alive"}hint _text
outputs the message "alive", if the player is alive, and "not alive" if not.Additional Syntax in Scripts OFP's scripting language provides another language structure with a similar task: '? condition : code'. This structure may be only used in scripts although (see Chapter 1. Scripts (*.sqs)). It is less strict than 'if ... then'; for 'if ... then' conditions have to be embraced by two brackets, while for '? ... :' they don't.? alive player : hint "yes"
This code is completely valid, while you have to add the brackets, if you want to use the 'if ... then' syntax:if (alive player) then {hint "yes"}
Â
Examples
- Example 1:
if (!canFire tank) then {grp1 setCombatMode "RED"}
Note You have to take care, that combined conditions are embraced by parenthesises. Wrong: if (alive player) && (someAmmo player) then {hint "yes"} Right: if ((alive player) && (someAmmo player)) then {hint "yes"}Syntax with Two Code BlocksYou may define another code block, which is executed if the condition doesn't fulfill.
Additional Information
- See also:
- See also needed
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note