A to Z Scripting: Conditionals

From The DarkMod Wiki
Jump to navigationJump to search



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");
	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 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.

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 < 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 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"


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

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.

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