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.
Normal vs. Recycled Actors
Stencyl supports two types of actors:
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” actors, 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 is this useful?
Recycled actors are best when they’re created and killed rapidly, such as particles or bullets.
Exercise: If recycled actors sound so great, why don’t we make them the default? That’s a good question! Can you think of a downside to them?
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, in the respect that there are separate blocks for destroying normal actors and recycled actors.
Remember this golden rule when killing an actor: kill an actor in the same way you made it.
Which is to say...
If you created a normal actor, kill it.
If you created a recycled actor, recycle it.
Note: Killing or recycling an actor is immediate. Don’t attempt to refer to an actor at any point after you’ve killed it, including during timed tasks. For example, don’t do this.
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.
Kill actors the same way you made them.
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?