Conditionals
The Command Prompt is not just commands. It's commands and conditionals. A conditional is something which has to be true in order for your command(s) to execute. In other words, you can ask Platform Builder something before you run the command(s).
Conditionals are advanced controls that you can use with commands. If you haven’t already learned about commands, please see the Command Prompt page first!
if money = 100
health + 1
if money >= 100
max health + 1
if money >= 100
max health + 1
if money >= 100
money -100
if money >= 100
{
max health + 1
money – 100
}
The above example will increase max health by 1 and remove 100 money if money is greater than or equal to 100. If money is less than 100, nothing will happen. (For those of you who have used code in the past, please note that the curly brackets must be placed on their own line.)
You can ask several conditionals using “&” for and or “|” for or. For example:
if money > 99 | health = 1
{
max health + 1
money -100
}
if money > 99 & max health != 3 & double jump = true
{
max health + 1
money – 100
}
The above example checks that money is greater than 99, max health is something other than 3, and if the character can double jump. If all three of these things are true, then the commands within the brackets will apply. If any of those three conditionals are not true, then nothing will happen. (Note that Platform Builder does not support the use of parentheses to group these things together.) Alternatively, you can use “and” and “or” to pair your conditionals.
if money > 99
and max health != 3
and double jump = true
{
max health + 1
money – 100
}
if money > 100 | money = 100
{
max health + 1
money -100
}
else
{
+You don’t have enough money!
}
The above example checks if money is greater than or equal to 100. If this is true, then max health goes up by 1 and money decreases by 100. If it is not true, then a message says “You don’t have enough money!” (The second set of curly brackets is not necessary since there is only a single command after the else conditional, but it won’t hurt anything.) Note that you cannot add other conditionals to “else” using “&” or “|”
if health = [max health]
+You already have full health!
else
{
health = [max health]
+Your health has been restored to [health]!
}
The above example checks if health is equal to the max health. If it is, then a message displays telling you that you are already at full health. If not, then your health is fully restored to the maximum health, and a message displays telling you the new amount of health that you have.
Note that conditionals are a little more space-sensitive than commands, so try to be more careful.
- Almost any command can be used as a conditional. For example:
“swim style = standard” becomes “if swim style = standard”
“background = forest 1” becomes “if background = forest 1”
“snow = false” becomes “if snow = false”
“game var 5 = 20” becomes “if game var 5 = 20”
“area = 3, Start1” becomes “if area = 3”
There are also some special conditionals that do not look like any other command. They are already available on the main command prompt page, but we will list them here again.
Command | Operator(s) | Value(s) | Example | Notes |
---|---|---|---|---|
if item exists, if character has item, if player has item | , ! | (Item ID) | if item exists = 5 | Checks if the character has an item of the given item ID. Note that this does not check for permanent items that have been collected (you could use a game var instead.) The example checks if the player has an item with the ID of 5. |
if player exists | , ! | 1, 2 | if player exists = 2 | Checks if the specified player is currently active. The example checks if player 2 is currently in the area. |
if character exists | , ! | (Character ID) | if character exists != 4 | Checks if the character with the specified ID currently exists as one of the two players. The example checks if character 4 does not exist. |
if chance percent [or] if percent chance | (Number from 1-100) | if percent chance = 25 | Randomly results in “true” according to the percent given. The example is a conditional which will return “true” 25 percent of the time, or 1 in 4 chance. In other words, the command or set of bracketed commands below this conditional have a 25 percent chance to occur. | |
if random (number) | (Any) | (Number) | if random 9 < 3 | The conditional starts with a random whole number between 0 and what you specify. Then is checks that number with the rest of your command. The example generates a random whole number between 0 and 9, and then checks if that number is less than 3. The conditional passes if the random number is 0, 1, or 2, which means that this conditional will have a 3 in 10 (30%) chance of passing. |
if enemy (ID) number, if enemy (ID)count | (Any) | (Number) | if enemy 2 number < 5 | Checks the number of enemies of the specified ID within the view and nearby. The example checks if there are less than 5 enemies with the ID of 2. |
if item (ID) number, if item (ID) count | (Any) | (Number) | if item 6 count = 3 | Checks the number of items of the specified ID within the view and nearby. The example checks if there are 6 items with the ID of 3. |
if projectile (ID) number, if projectile (ID) count | (Any) | (Number) | if projectile 1 number != 0 | Checks the number of projectiles of the specified ID within the view and nearby. The example checks if there are not 0 projectiles with the ID of 1. |
if block (ID) number, if block (ID) count | (Any) | (Number) | if block 3 count = 4 | Checks the number of blocks of the specified ID within the view and nearby. The example checks if there are 4 blocks with the ID of 3. |
if key, if keyboard | , ! | up, down, left, right, jump, attack, pick, use | if key = jump | Checks if a movement or action key is pressed down. The example checks if the key for jumping is being pressed. If you want to run commands only once when the key is pressed down, you can use variables. An example is given here on our forums. |
if key press | , ! | up, down, left, right, jump, attack, pick, use, button a, button b, button c, button d | if key press = jump | Checks if a key has been pressed down on the frame that the command is run. You select any of the standard Platform Builder keys or extra buttons (a-d) if you are using them (see “map” commands). Note that since this conditional is checking for a very specific moment of when a key is being pressed down, you will want to use this in a looping command prompt with a constant looping speed (see “loop” commands). Also, there are a few alternatives to these values. For instance, you could use “grab” or “pickup” instead of “pick” and it will work the same. The example checks if the jump button has been pressed in the current frame of the game. Lastly, “key press” can also be used as a non-conditional to cause the character act as if the key was pressed down. |
if key release | , ! | up, down, left, right, jump, attack, pick, use, button a, button b, button c, button d | if key release = button a | Checks if a key has been released on the frame that the command is run. You select any of the standard Platform Builder keys or extra buttons (a-d) if you are using them (see “map” commands). Note that since this conditional is checking for a very specific moment of when a key is being released, you will want to use this in a looping command prompt with a constant looping speed (see “loop” commands). Also, there are a few alternatives to these values. For instance, you could use “grab” or “pickup” instead of “pick” and it will work the same. The example checks if button a (one of the four extra keys that you can map in your game) has been released in the current frame of the game. |
if mouse, if mouse button | , ! | left, right | if mouse button != right | Check if a mouse button is being pressed or not. The example checks if the mouse right button is not being pressed. |
if standing, walking, running, skidding, full speed, jumping, falling, standing attack, moving attack, jumping attack, hovering, gliding, ducking, defending, defended, pounding, pounded, wall sliding, climbing, hanging, swimming, using pick up gun, using pick up melee | , ! | true, false | if pounded = true | Checks the current action state of the character. Note that “full speed” returns true when the character is running at the maximum running speed. “defending” is when the character is in defensive stance, but has not blocked any attack. “defended” means that the character has blocked an attack while in defensive stance. “pounding” checks if the character is going down into a ground pound. The example checks if the character has “pounded,” that is, when the character has landed a ground pound. |
if ID | (Number) | if id = 5 | Checks the ID number of the item, enemy, etc. which is running the command. This may be useful when using invisible command blocks which are triggered by items, enemies, etc. Here, you can check if the item, enemy, etc., is the proper one before continuing. | |
if testing | true, false | if testing = true | Checks if you are currently testing a level or doing a test run of the game. This can be useful to create your own “debug mode” to help you get around your game more easily as you test. | |
if testing area | true, false | if testing area = true | Checks if you are currently testing a level (full-game test runs are not checked). | |
if test run, testing run, test game, testing game | true, false | if test run = true | Check if you are currently doing a test run of the game (testing an area is not checked.) | |
if distance to player (1, 2, or leave blank) | (Any) | (Number) | if distance to player > 64 | Check the distance to a player. You can specify player 1, player 2, or don’t specify (like in the example) to check for whatever player is running the command. The example checks if the distance to the player is greater than 64, which is the length of two blocks. |
if distance to character (ID) | (Any) | (Number) | if distance to char 2< 50. | Checks the distance to the character of the specified ID. The distance is measured in pixels. The example checks if the object calling the commands is less than 50 pixels away from the character. |
if distance to enemy (ID) | (Any) | (Number) | if distance to enemy 3 > 50. | Checks the distance to the enemy of the specified ID. The distance is measured in pixels. The example checks if the object calling the commands is more than 50 pixels away from the enemy 3. |
if distance to projectile (ID) | (Any) | (Number) | if distance to projectile 2< 10 | Checks the distance to the projectile of the specified ID. The distance is measured in pixels. The example checks if the object calling the commands is less than 10 pixels away from the projectile with an ID of 2. |
if distance to character projectile (ID) | (Any) | (Number) | if distance to character projectile 2< 10 | Checks the distance to the enemy projectile of the specified ID. (It won’t check for that projectile if it was launched from the character) The distance is measured in pixels. The example checks if the object running the commands is less than 10 pixels away from a projectile with an ID of 2 that was launched from an enemy. |
if distance to enemy projectile (ID) | (Any) | (Number) | if distance to enemy projectile 2< 10 | Checks the distance to the character projectile of the specified ID. (It won’t check for that projectile if it was launched from an enemy) The distance is measured in pixels. The example checks if the object running the commands is less than 10 pixels away from a projectile with an ID of 2 that was launched from the character. |
if distance to item (ID) | (Any) | (Number) | if distance to item 9> 40 | Checks the distance to the item of the specified ID. The distance is measured in pixels. The example checks if the object running the commands is more than 40 pixels away from item 9. |
if distance to block (ID) | (Any) | (Number) | if distance to block 6 >= 10 | Checks the distance to the block of the specified ID. The distance is measured in pixels. The example checks if the object running the commands is 10 or more pixels away from block 6. |
if distance to npc (ID) | (Any) | (Number) | if distance to npc 3 <= 30 | Checks the distance to the NPC of the specified ID. The distance is measured in pixels. The example checks if the object running the commands is 30 or less pixels away from NPC 3. |
if distance to sprite (ID) | (Any) | (Number) | if distance to sprite 4 < 25 | Checks the distance to the sprite of the specified ID. The distance is measured in pixels. The example checks if the object running the commands is less than 25 pixels away from a sprite with an ID of 4. |
if character (ID) | , ! | inside view, in view, outside view, out view | if character 2 = in view | Checks if the character with the specified ID is inside or outside the current view. The example checks if the character with an ID of 2 is inside the view. |
if player (1 or 2) | , ! | inside view, in view, outside view, out view | if player 2 = inside view | Checks if the player 1 or payer 2 is inside or outside the current view. The example checks if player 2 is inside the view. |
if enemy (ID) | , ! | inside view, in view, outside view, out view | if enemy 6 = inside view | Checks if the enemy with the specified ID is inside or outside the current view. The example checks if the enemy with an ID of 6 is inside the view. |
if item (ID) | , ! | inside view, in view, outside view, out view | if item 2 != out view | Checks if the item with the specified ID is inside or outside the current view. The example checks if the item with an ID of 2 is not outside the view (in other words, if it’s inside the view) |
if npc (ID) | , ! | inside view, in view, outside view, out view | if npc 10 = in view | Checks if the NPC with the specified ID is inside or outside the current view. The example checks if the NPC with an ID of 10 is inside the view. |
if projectile (ID) | , ! | inside view, in view, outside view, out view | if projectile 6 = in view | Checks if the projectile with the specified ID is inside or outside the current view. The example checks if the projectile with an ID of 6 is inside the view |
if block (ID) | , ! | inside view, in view, outside view, out view | if block 1 = outside view | Checks if the block with the specified ID is inside or outside the current view. Check example checks if the block with an ID of 1 is outside the view. |
if collision character, if touching character | (N/A) | (Character ID) | if touching character 2 | Checks if whatever is calling the command is in contact with the specified character. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching a character with an ID of 2. |
if collision player, if touching player | (N/A) | 1, 2 | if collision player 1 | Checks if whatever is calling the command is in contact with the specified player. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching player 1. |
if collision enemy, if touching enemy | (N/A) | (Enemy ID) | if touching enemy 5 | Checks if whatever is calling the command is in contact with the specified enemy. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching a custom enemy with an ID of 5. |
if collision item, if touching item | (N/A) | (Item ID) | if collision item 7 | Checks if whatever is calling the command is in contact with the specified item. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching a custom item with an ID of 7. |
if collision block, if touching block | (N/A) | (Block ID) | if collision block 1 | Checks if whatever is calling the command is in contact with the specified block. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching a custom block with an ID of 1. |
if collision character projectile, if touching character projectile | (N/A) | (Projectile ID) | if collision character projectile 1 | Checks if whatever is calling the command is in contact with the specified character projectile. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching a character projectile with an ID of 1. |
if collision enemy projectile, if touching enemy projectile | (N/A) | (Projectile ID) | if touching enemy projectile 9 | Checks if whatever is calling the command is in contact with the specified enemy projectile. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching an enemy projectile with an ID of 9. |
if collision sprite, if touching sprite | (N/A) | (Sprite ID) | if touching sprite 6 | Checks if whatever is calling the command is in contact with the specified sprite. So, if this command comes from an enemy command prompt, the example checks if the enemy is currently touching an imported sprite with an ID of 6. |
if game (Number of game file) exists | (N/A) | (N/A) | if game 3 exists | Checks if a game exists with the specified game number. The example check if game 3 exists. |