Creating and Destroying Actors
by Jon (Updated on 2014-01-26)
- What are Recycled Actors?
- How To: Creating Actors
- How To: Destroying Actors
Note for Stencyl 3.0 and above
Recycled Actors are now the default and only choice in 3.0. Creating and destroying Actors automatically opt for the recycled option. We're keeping this article around for users of older versions of Stencyl and for educational purposes in informing you about what is happening under the hood.
Imagine that you’re playing an action game such as Mega Man. The main character fires bullets to the side. These bullets hit enemies, which die after being hit a few times. These enemies fire bullets back at you.
In any modern game, actors get created and destroyed at a rapid rate. This article talks about how you create actors and destroy them in a way that maximizes your game’s performance.
What are recycled actors?
Stencyl is built around a recycled actor model.
Normal actors follow an expected lifecycle - they’re created, they do stuff and they finally die.
Recycled actors are different. While normal actors are destroyed when you kill them, when you “kill” a recycled actor, it goes into a pool of recycled actors.
The next time you create a “recycled” actor, it will check the pool and see if that actor has been created before. If it has, it will pull the “new” actor from the pool instead of making the new actor from scratch. Thanks to recycling, the penalty for creating and destroying a recycled actor is close to nothing.
How To: Creating Actors
Just these two blocks (Scene > Actors)
Sometimes, you’ll want to refer to the actor that you just created. Use the “last created actor” option under any Actor dropdown to do this.
How To: Destroying Actors
Destroying actors is similar to creating them.
Events: Knowing when actors get created or die
Did you know there are events to control what happens when an actor is created or destroyed?
Select one of these events via the “Add Event” button.
- Recycled actors come from a “pool” of actors. This is good for scenarios that require lots of creating/destroying of actors, such as firing lots of bullets, which many games do.
- You can use events to detect the creation or destruction of actors.
Challenge 1: Create A Particle Effect
Why not try creating a particle effect using recycled actors and along the way, compare its performance to doing this with regular actors?
Many games use particle effects to add visual polish. Although Stencyl will support particle effects at the engine-level in the future, you can roll your own today.
To create particle effects, just spawn (recycled) actors rapidly and “emit” them at a random speed and starting position and have them fade out over time. With some experimentation, you can create a fire and smoke effect with ease.
Challenge 2: Create A Bullet Limiter
Many games have a mechanic that allows you to fire bullets, but only a certain number at a time, otherwise the game would become too easy.
Why not try creating a bullet limiter that allows the player to create no more than 3 bullets at once?