Construct 3 · Lesson 2
Variables &
Conditionals
By: Mr Farhan
Integers
Let's start with something you already know: numbers.
- Here is a number line, counting from 0 to 5. Press Next →
- Numbers can go below 0 too! These are negative numbers. They have a minus sign.
- Whole numbers, positive or negative, are all called integers.
Types of data
Games remember lots of different things. Each kind of data is called a type.
"Alex"
Words and letters
42
Whole numbers
True False
Yes or no
- There are three types you need to know. Press Next →
- A string is words or letters, like a name. It always goes in "quotes".
- An integer is a whole number, like your score or your lives.
- A boolean can only be True or False, like a light switch that is on or off.
What is a variable?
A variable is a name that remembers a value.
score =
life =hit_power =
- Games have to remember things, like your score. Press Next →
- On the left is the name: score.
- On the right is the value it remembers: 0.
- score lives outside every object. That makes it a global variable: the whole game shares one score.
- Variables can also live inside an object. The player has its own life.
- …and its own hit_power: how much damage its attacks do.
- These belong to the player only. Construct calls them instance variables.
Variables can change
The value inside a variable can change while the game is playing.
PlayerOn collision with Coin
▶SystemAdd 1 to score
PlayerOn collision with Enemy
▶SystemSubtract 1 from lives
- This game has two variables. Hold D and walk over the coins… the score stays at 0!
- Now touching a coin adds 1 to score. Collect some and watch the number go up!
- A slime! Bumping into it subtracts 1 from lives. Watch that number go down.
- The values keep changing as you play. They vary, and that is why we say variable!
Booleans
A boolean is a variable with only two values: true or false.
- A boolean is like a light switch. It is either on (true) or off (false). Press Next →
- Each row is a variable. Its name is a yes-or-no question: isNight means “Is it night?”
- Is it night? Yes! isNight is now true, so the sun goes down. 🌙
- Has the player got a key? hasKey is true, so yes!
- Is the player walking? isWalking is true, so the Walk animation plays.
- Set isNight back to false and it is day again. There is nothing in between!
- Your turn! Click a variable to change it. In Construct, the action Set boolean does this.
Conditions
A condition is a question. The answer is always true or false.
- We can ask questions about our variables. Press Next to ask the first one →
- score > 10? asks: is score more than 10? It is only 3, so the answer is false.
- Now score is 12. Ask the same question again… this time it is true!
- lives = 0? asks: is lives equal to 0? lives is 3, not 0, so the answer is false.
- Lose every life and it turns true. That is how a game knows it is game over!
- There are more: < less than, ≥ at least, ≤ at most and ≠ not equal.
- Your turn! Drag the sliders and watch the answers flip between true and false.
If
An if checks a condition. When it is true, the action runs.
PlayerOn collision with Coin
▶SystemAdd 1 to score
PlayerOn collision with Enemy
▶SystemSubtract 1 from lives
IfSystemscore = 5?
▶TextSet text to "You win!"
IfSystemlives < 1?
▶PlayerSet animation to "Dead"
- The coin game again! Coins add to score and the slime takes away lives. Press Next →
- An if: score = 5? Hold D and collect 5 coins… you win!
- Another if: lives < 1? Hold A and bump into the slime until lives drops below 1… game over!
- The game checks every if all the time. As soon as one is true, its action runs.
Pick your player
Point at an animation to see its frames. Click it to download it!
Pick your enemy
Point at an animation to see its frames. Click it to download it!