Beginner's guide to Construct 3

1940

Index

Contributors

Stats

1,312,012 visits, 3,809,872 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 20 Sep, 2017. Last updated 24 Jun, 2024

Using instance variables

Instance variables allow each goblin to store its own health value. A variable is 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 goblin. This works somewhat similarly to adding a behavior. Click the goblin in the Project Bar. Alternatively, you can switch back to the layout using the tabs at the top and select a goblin object. This will show the goblin's properties in the Properties Bar. Click Instance variables to open the Instance Variables dialog.

Editing instance variables

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

Adding the Health instance variable

Type Health for the name, leave Type as Number, and for Initial value enter 5 (as shown). This starts every goblin 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 goblin 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 goblin with a different amount of health.

Changing the events

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

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

Replacing an 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 GoblinSubtract 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:

Finished event with instance variable

Now when we hit goblins with a spell they lose 1 health and a flash appears, but we haven't made an event block to destroy goblins when their health reaches zero. Add another event:

Condition: GoblinCompare instance variableHealth, Less or equal, 0

Action: GoblinSpawn another objectSparkFlash

Action: GoblinDestroy

Event to destroy goblins

Why "less or equal 0" rather than "equals 0"? Suppose we added another more powerful spell which subtracted 2 from health. As you hit a goblin, its health would go 5, 3, 1, -1, -3... notice at no point was its health directly equal to zero, so it'd never be destroyed! 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 goblins five times to cast them back to the dimension from which they came!

Next Tutorial In Course

How to Learn Construct 3? Next Steps for Beginners 51:05

Get started with Construct's built in examples and game templates

Disabled Comments have been disabled by the owner.