A to Z Scripting: Conditionals: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
== Conditionals ==
=== Basics ===
Scripts can check whether conditions have been met before carrying out the associated set of instructions. This is very useful for making dynamic scripts that react to or wait for events in the map.
Scripts can check whether conditions have been met before carrying out the associated set of instructions. This is very useful for making dynamic scripts that react to or wait for events in the map.


Line 21: Line 19:
You can either:
You can either:
* compare values with each other (i.e. "is an entity's health less than or equal to x?")  
* compare values with each other (i.e. "is an entity's health less than or equal to x?")  
* or you can evaluate whether a single value is true or false. You can evaluate any type of variable, not just booleans: any non-0 or non-default value counts as "true". So i.e. for an entity variable, $null_entity is considered false, for a string "" is considered false, for a float 0 is false, for a vector '0 0 0' is false.
* or you can check if a single value is true or false. For non-booleans, any non-default value counts as "true", so for an entity $null_entity is considered false, for a string "" is considered false, for a float 0 is false and for a vector '0 0 0' is false. Note that a negative float also counts as true.


Various examples:
Various examples:
  if (attempts_left) //check whether the player has a non-0 number of attempts left (note: also negative values count as true)
  if (attempts_left) //if the player has attempts left... (negative numbers count too, so not ideal in this case)
  if (attempts < max_attempts) //another way of checking whether the player has attempts left
  if (attempts < max_attempts) //another way of checking whether the player has attempts left
  if ($lord_marlow.getFloatKey("can_unlock")) //check whether this AI can unlock doors
  if ($lord_marlow.getFloatKey("can_unlock")) //check whether this AI can unlock doors
   
   
  //If you want to check whether something is false, you need an exclamation mark within the brackets.
  //If you want to check whether something is false, you need to begin with an exclamation mark within the brackets.
  if ( !$lord_marlow.canSee($player1) ) //if the AI CAN'T see the player, do the following...  
  if ( !$lord_marlow.canSee($player1) ) //if the AI CAN'T see the player, do the following...  


Line 62: Line 60:
  && AND
  && AND
  || OR
  || OR
The OR sign works inclusively: the check is successful if any of the values are true.




Line 79: Line 79:


"Else" will always get carried out, while "elseif" checks whether its conditions are met.
"Else" will always get carried out, while "elseif" checks whether its conditions are met.
{| class="wikitable"
|-
| if
|-
| elseif (alternative: else if)
|-
| else
|}




Line 86: Line 95:
  if ( lever_position == "inactive" ) return; //if lever is inactive, stop processing this script
  if ( lever_position == "inactive" ) return; //if lever is inactive, stop processing this script
   
   
  if ( lever_position == 1 ) sys.trigger($door1); //check for lever position 1
  if ( lever_position == 1 ) sys.trigger($door1); //check for lever position 1
  elseif ( lever_position == 2 ) sys.trigger($door2); //otherwise check for lever position 2
  elseif ( lever_position == 2 ) sys.trigger($door2); //otherwise check for lever position 2
  else sys.trigger($door3); //only remaining possibility is the lever is in position 3
  else sys.trigger($door3); //only remaining possibility is the lever is in position 3

Revision as of 17:20, 23 December 2020

Scripts can check whether conditions have been met before carrying out the associated set of instructions. This is very useful for making dynamic scripts that react to or wait for events in the map.


If there's only a single line of instructions to carry out, then the instructions can be on the same line as the conditional itself. No extra brackets needed:

if($player1.getHealth == 100) sys.println("player1 has full health");


If you have a set of instructions, use curly brackets. Note that the line with the conditional needs no semicolon:

if($player1.getHealth != 100)
{
	sys.println("player1 doesn't have full health");
	$player1.setHealth(100);
	sys.println("player1 has now been restored to full health");
}


You can either:

  • compare values with each other (i.e. "is an entity's health less than or equal to x?")
  • or you can check if a single value is true or false. For non-booleans, any non-default value counts as "true", so for an entity $null_entity is considered false, for a string "" is considered false, for a float 0 is false and for a vector '0 0 0' is false. Note that a negative float also counts as true.

Various examples:

if (attempts_left)				//if the player has attempts left... (negative numbers count too, so not ideal in this case)
if (attempts < max_attempts)			//another way of checking whether the player has attempts left
if ($lord_marlow.getFloatKey("can_unlock"))	//check whether this AI can unlock doors

//If you want to check whether something is false, you need to begin with an exclamation mark within the brackets.
if ( !$lord_marlow.canSee($player1) )		//if the AI CAN'T see the player, do the following... 


Available symbols

==, != equal, not equal
<, > less than, greater than
<=, >= less or equal to, greater or equal to


An easy mistake is to use only a single equals sign in a conditional:

= means "set to this value"

== means "equal to this value"

Example:

attempts = 3;		//translation: set "attempts" to 3
if (attempts == 3)	//translation: check if "attempts" is equal to 3


Multiple conditionals

Sometimes you want to check multiple conditionals, i.e. if A and B are true but C is false. Important symbols here are:

&& 	AND
|| 	OR

The OR sign works inclusively: the check is successful if any of the values are true.


Example: secret conversation under specific circumstances

if ( ( $lord_marlow.getLocation() == $mansion_bedchamber || $mansion_secretchamber ) && ( !$lord_marlow.canSee($player1) ) )
{
	$lord_marlow.bark("snd_secretconversation");	//say a secret pass phrase if he believes no one's nearby
}

In other words: if (Lord Marlow is either in his bed chamber or his secret chamber) and (he can't see the player), make him say a secret line.

Getting the right number of brackets in the right place can be tricky. It can be helpful to write each conditional on its own line first, then combine them one by one into a single line.


Chains of conditionals

Other than "if", there are also "else" and "elseif". These only get considered if the preceding "if" has failed.

"Else" will always get carried out, while "elseif" checks whether its conditions are met.

if
elseif (alternative: else if)
else


Example: lever with 3 positions and an inactive state

void lever()			//intended as one of several scripts controlling a lever. Checks which position the lever is in and performs an appropriate action
{
	if ( lever_position == "inactive" ) return;		//if lever is inactive, stop processing this script

	if 	( lever_position == 1 ) sys.trigger($door1);	//check for lever position 1
	elseif	( lever_position == 2 )	sys.trigger($door2);	//otherwise check for lever position 2
	else				sys.trigger($door3);	//only remaining possibility is the lever is in position 3
}


Next / previous article