Let’s Consider “Clear by Defeating 5 Enemies”: Handling Variables
We need a way to count the number “5”. The goal is for this number to decrease each time an enemy is defeated, and when it reaches 0, a clear sequence is triggered. In game development, we handle this using “variables.”
What is a “Variable”?
A “variable” is like a container for storing values. The Health Points (HP) we discussed earlier are actually variables; when “hit by an enemy attack,” the value in the HP container decreases by one.
There are two main ways to set up variables in ACTION GAME MAKER: one is using the VariableSettings node of an object, and the other is using “Project Variables” in the database. The “Health” we discussed earlier falls under the former category.
Difference Between “VariableSettings Variables (Object Variables)” and “Project Variables”
The difference is that “Object Variables” are dedicated containers belonging to specific objects, while “Project Variables” are shared containers accessible by anyone, anywhere.
The major distinction is that “Object Variables” are attached to objects and disappear when the object is deleted, whereas “Project Variables” remain usable as long as you are within the same project. Data tied to specific objects, such as “Health” or “Attack Power,” should use Object Variables. Data you want to save even when switching save files, like “High Score,” or data you want to retain across stages, such as “Coin Count” or “Remaining Lives,” should use Project Variables.
So, which variable should we use for “Remaining Enemy Count”?
Technically, either would work, but since multiple objects might need to manipulate this value, let’s create it as a “Project Variable” this time.
When an enemy enters the “Disappear” state, decrement the “Remaining Enemy Count” by 1. When the “Remaining Enemy Count” reaches “0,” trigger the clear sequence. This approach seems feasible.
How to Implement the Clear Sequence
It is best to create a dedicated game object to check the remaining enemy count and place it on the UI layer that is always visible. By displaying the current kill count on this object, similar to an HP bar, we can also solve the problem of players not knowing how many more enemies they need to defeat. Let’s proceed with the following steps: create a “Project Variable” named “Remaining Enemy Count,” add an action to the “Disappear” state of the “enemy” object to increment the kill count, and finally, place the clear sequence object.