Home Blog Gaming

GDScript If Statements

Godot Tutorials GDScript If Statements

What is an If Statement?

Computer code is often dependent on conditions. For example, If you shoot the enemy three times he will die. Code that simulates the enemy's death will only execute if you shoot him three times.

If statments are a common method to program conditions and they are based on propositional logic. In this lesson, you will learn to program conditions into your game code.

An if statement is a conditional statement based on implication. The value of a variable will determine the code that executes.


You are making a battle card game. Each player has a set of skills. Each skill is equal to an integer 1-20 (based on D&D dice).

If a character's spirituality skill minus brutality skill is greater than 10 that character will have the power to heal. To calculate the number of health points a character can heal each round:

(spirituality - brutality) / 5

Write code to determine a character's healing points.

Mouse Hover over the blue text in the description to highlight the code.

extends Node

# Declare Variables
var spirituality: int = 18
var brutality: int = 5
var baseScore = spirituality - brutality
var healing: int = 0
func _ready():
  # If the condition is satisfied, execute this code
  if baseScore > 10:
    healing += (baseScore / 5)
  # print healing points
  print (healing)

➼ spirituality equals 18 (an integer)
➼ brutaility quals 5 (an integer)
➼ baseScore equals spiritality minus brutality - This number determins if a character has healing
➼ healing is initalized at 0 - If not other healing code executes, the character has no healing
➼ if the baseScore is greater than 10 - This is the condition that determines if more healing code executes
➼ Add (baseScore / 5) to healing - This only happens if the condition is satisfied: baseScore > 10
➼ Output healing points


** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
** Debug Process Stopped **

Elif - Multiple Cases

In the example above there are two cases: baseScore is greater than 10 or baseScore is not greater than 10. Sometimes there are more than 2 cases. That scenario calls for elif statements.

Let's look at an example. You are writing code to determine if a number is positive, negative, or zero.

# Declare num
var num = 3 * 2

func _ready():
  # Conditional Code
  if num < 0:
    print ("negative")
  elif num == 0:
    print ("zero")
  elif num > 0:
    print ("positive")
  else: print ("Error")

➼ if num < 0 - The code will output "negative"
➼ if num == 0 - The code will output "zero"
➼ if num > 0 - The code will output "positive"
➼ if none of the specified conditions are met - The code will output "Error". Even if you do not think there is a possibility outside of the conditions you have named, you should still have an else statement. You never know what a user will break. A good programmer will make every effort to prevent an application from breaking.


** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
** Debug Process Stopped **

Compound Conditions

Sometimes more than one condition has to be satisfied for a block of code to execute. We code this with logical operators.

Billy is ordering t-shirts. Plain youth shirts cost $10. Graphical youth shirts cost $14. Plain adult shirts cost $15. Graphical adult shirts cost $20. Write the code to output the price of a shirt.

# Declare Variables
var adult: bool = true
var graphics: bool = false

func _ready():
  # Conditional Code
  if adult && graphics:
    print ("$20")
  elif adult && !graphics:
    print ("$15")
  elif !adult && graphics:
    print ("$14")
  elif !adult && !graphics:
    print ("$10")
  else: print ("error")

➼ If adult and graphics are true - Output "$20"
➼ If adult is true and graphics is false - Output "$15"
➼ If adult is false and graphics is true - Output "$14"
➼ If adult and graphics are false - Output "$10"
➼ If none of the named conditions are satisfied - Output "error"


** Debug Process Started **
OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
** Debug Process Stopped **


Write GDScript code to perform the following:
  1. A character's defensive score equal to strength plus intelligence plus diplomacy. If the character has at least 20 defensive points he will have a ten point shield bonus. Otherwise shield points equal defensive points. Calculate a character's shield points.

  2. A third grade class is being tested on mathematics. If the student scores 90-100% she passes with an A, 80-89% she washes with a B, 70-79% she passes a C, and any score lower than 70% means she failes and has to retake the test. Write code to determine a student's grade and whether she must retake the test.

  3. Write code to calculate the area of rectangle. Also let the user know if the rectangle is also a square.

Want Commander Candy to Grade your Homework?


Linux (Ubuntu)+ NodeJS + Postgres
LAMP - Linux, Apache 2, MySQL, PHP
Sexy Linux T-shirt
Twitch: daisychaincosplay Live Coding!!!
Learn PHP on Linux with Commander Candy
Women in Tech - Milly Berst
Coding Commanders YouTube
Coding Commanders Twitter
Learn HTML on Linux with Commander Candy
Learn ReactJS VR on Linux with Commander Candy
Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
murks - Linux Game Dev
Linux Gaming King Hatnix on Twitch
How to Install Dragon Age Orgins on Linux by Hatnix
SteamWorld Quest: Hand of Gilgamech
Previous Lesson | Next Lesson