You're gonna have to create a variable to keep track of the actor's lives first. If you don't know how to do this, here's a quick guide:
First you open the script editor. This can be done when adding an event to an actor.
Then, you click the button that says "Variables".
Then, click "Add".
In the window that appears, write a name for your variable. I recommend giving it a name that describes it's function in two words or less. Variable names can't contain any spaces.
Now, if you only want to add a life counter for the player actor, just click Add.
But if you want one for more actors, there is a button/drop down menu that says "Global". Click it, and in the menu that opens, click Actor and then click Add.
That's how you make a variable.
With that out of the way, it's time to create the life counter.
In this guide, the variable I told you to create will be called
lives and will be a global variable. If you want to know how to do this with an actor variable, just let me know.
First of all, you need a text actor that will serve as the life counter.
Just give it this text:
- Code: Select all
Lives left: 0
Then, in a draw actor -> script editor event for that actor, write:
- Code: Select all
spring(text, "Lives left: ℅i", lives);
This single line of code is what makes your life counter show how many lives you have left, but we'll have to add a few more things to make it work, so don't close the script editor yet.
Instead, add the following below that line:
(I'll be explaining everything line by line.)
- Code: Select all
if(lives == 0)
This line just checks if the
lives variable is 0.
- Code: Select all
{
DestroyActor("Player");
}
The first and third lines do nothing on their own, but are needed for the if statement to work.
The second line is what tells the game to destroy the actor. This only happens if
lives hits 0. Replace the part that says
Player with the name of the actor you want to destroy.
We're done with this event, so click the Add button at the bottom of the script editor window and click "Immediate action".
Now we need to add a Create Actor -> Script editor event to the life counter actor, so go ahead and do that. In the script editor, write:
- Code: Select all
lives = 3;
This just sets the life counter to 3 at the start of the game. Add the script the same way I described above.
Now, in a collision -> script editor event with anything that'll cause the actor to lose a life, write this:
- Code: Select all
lives--;
And add it as described above.
And there you have it, a fully functional life counter. If you have any further questions, let me know.