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. Click the monster in the project bar or object bar. Alternatively, you can switch back to the layout and select a monster object. This will show the monster's properties in the properties bar. Click Add/edit by Edit variables.
The Instance Variables dialog appears. It looks similar to the Behaviors dialog we saw earlier, but instead allows you to add and change instance variables for the object. Click the green Add button to add a new one.
In the dialog that pops up, 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 click the Add / Edit link.)
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.
The same dialog appears as if we were inserting a new action, but this time it'll replace the action we clicked instead. Choose Monster -> Subtract from (in the Instance variables category) -> Instance variable "health", and enter 1 for Value. Click Done. The action should now appear 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: Monster -> Compare instance variable -> Health, Less or equal, 0
Action: Monster -> Spawn another object -> Explosion, layer 1
Action: Monster -> Destroy
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!
Let's have a score so the player knows how well they've done. We'll need another variable for this. You might think "lets put the score as one of the player's instance variables!". That's not a bad first idea, but remember the value is stored "in" the object. If there are no instances, there are no variables either! So if we destroy the player, we can no longer tell what their score was, because it was destroyed with the player.
Instead, we can use a global variable. Like an instance variable, a global variable (or just "global") can store text or a number. Each variable can store a single number or a single piece of text. Global variables are also available to the entire game across all layouts - convenient if we were to add other levels.
Right-click the space at the bottom of the event sheet, and select Add global variable.
Enter Score as the name. The other field defaults are OK, it'll make it a number starting at 0.
Now the global variable appears as a line in the event sheet. It's in this event sheet, but it can be accessed from any event sheet in any layout.
Note: there are also local variables which can only be accessed by a smaller "scope" of events, but we don't need to worry about that right now.
Let's give the player a point for killing a monster. In our "Monster: health less or equal 0" event (when a monster dies), click Add action, and select System -> Add to (under Global & local variables) -> Score, value 1. Now the event should look like this:
Now the player has a score, which increases by 1 for every monster they kill - but they can't see their score! Let's show it to them with a text object.