Tags

Add to Favourites

6 Favourites
Construct 3
Construct 3

Buy Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Buy Now Construct 3 users don't see these ads

Tutorial Stats

? 4,644 sessions
? 76 sessions per day
? 10,437 page views
? 172 page views per day

Completion

76%
25%
17%
14%
12%
9%
9%
9%
12%

Report Tutorial

Report this tutorial for spam or for being inappropriate.

Using instance variables

Instance variables allow each monster to store its own health value. A variable is simply a value that can change (or vary), and they are stored separately for each instance, hence the name instance variable.

Lets add a health instance variable to our monster. This works somewhat similarly to adding a behavior. Click the monster in the Project Bar. Alternatively, you can switch back to the layout using the tabs at the top and select a monster object. This will show the monster's properties in the Properties Bar. Click Instance variables to open the Instance Variables dialog.

You can add as many instance variables to an object as you like, but we only need one for the monster. Click Add new instance variable. The following dialog appears for adding an instance variable.

Type Health for the name, leave Type as Number, and for Initial value enter 5 (as shown). This starts every monster on 5 health. When they get hit we'll subtract 1 from the health, and then when health is zero we'll destroy the object.

Once you're done click OK. Notice the variable now appears in the instance variables dialog and also in the properties for the monster as well. You can quickly change initial values in the properties bar, but to add or remove variables you'll need to open the instance variables dialog. Also note every object in the layout can have unique instance variable values set as well, so you could for example start every monster with a different amount of health.

Changing the events

Switch back to the event sheet. Right now, we're destroying monsters as soon as the bullet hits them. Let's change that to subtract 1 from its health.

Find the event that reads Bullet: On collision with Monster. Notice we've got a "destroy monster" action. Let's replace that with "subtract 1 from health". Right click the "destroy monster" action and click Replace action.

The same dialog appears as if we were inserting a new action, but this time it'll replace the action we clicked instead. Choose MonsterSubtract from (in the Instance variables category), choose the instance variable "health", and enter 1 for the value. Click Done. The event should now look like this:

Now when we shoot monsters they lose 1 health and the bullet explodes, but we haven't made an event to kill monsters when their health reaches zero. Add another event:

Condition: MonsterCompare instance variableHealth, Less or equal, 0

Action: MonsterSpawn another objectExplosion

Action: MonsterDestroy

Why "less or equal 0" rather than "equals 0"? Suppose we added another more powerful weapon which subtracted 2 from health. As you shot a monster, its health would go 5, 3, 1, -1, -3... notice at no point was its health directly equal to zero, so it'd never die! Therefore, it's good practice to use "less or equal" to test if something's health has run out.

Run the game. You now have to hit monsters five times to kill them!

Share this Tutorial

Make games in your browser

You may use this tutorial for any purpose (even commercial) if you properly attribute it. Click here for more information.

Beta Mode: You may find bugs/missing content. Read more & Report Bugs