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.
Introduction
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
Recycled 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.
Summary
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?
Hint: Use the ‘when an actor of [TYPE] is created/killed’ event!
Last Updated: 2013-01-29 by Jon
22025 have read this article
Disclaimer: The Stencyl Team does not actively monitor comments on articles. If you're seeking help for your game,
please ask a question on the forums. Thanks!
12 Comments
katemamba My game consists of 2 types of deaths of main charcter -1) through dying in pits and 2) direct kill by an enemy . I create actor at start of screen , then recycle him whenever he jumps into pits and dies by enemy. my code and behaviours also support this...but during testing I am Able to recycle the actor (actor dies and new actor is created at start of scene)only when the cause is due to pits, not both by enemy and pits..solutions/fixes please 04 months, 3 weeks ago
wilycodger Is there a way to flush the pool of recycled actors? 15 months, 1 week ago
wilycodger I don't really perceive a frame rate difference between the recycled actor mode and the normal actor mode in the particle demo. I recreated the demo on my machine and also see no substantial difference in performance in my version. I am generating 12 particles per click and fading them out over 4 seconds; in both the recycle and normal modes the frame rate drops off quickly once you go over ~160 particles. 05 months, 1 week ago
cloa513 Acsmitte- The Germans lost because they had Hitler as their boss so there was no good strategy- if they had gone straight for the oil supplies in Urals mainly then the Russian war machine would have ground to a halt. Its like a vast war game that you explore without any point- your forces no matter how good assuming they are mortal if spread thinly will always lose. Its like playing Hercules in standard battlefield rather than guerilla like Rambo. 06 months, 2 weeks ago
andyclark Acsmitte's comment got negative votes. I gave it a positive vote. It showed interest in history AND it sounds like he had a War Game on his mind. 06 months, 3 weeks ago
Xionis I believe it works thusly. Creating an actor consumes a larger portion of resources than just having one, same for destroying. So if you are constantly creating/destroying, thats a lot of resources. But, if you create once (with a recycled actor) and then just reuse the same actor over and over, it uses far fewer resources constantly existing than constantly being created/destroyed. But, if you had a fairly complex actor, with many constant events/behaviors that uses a lot of resources while existing, recycling it would mean those resources keep being used even when the actor isn't. That's what I think the difference is. 18 months, 3 weeks ago
t4u i have no idea what is the difference between recylced and created. i mean article needs an example which says in this case creating actor bad thing, recycling actor good thing. is it only about game performance? 09 months, 4 days ago
vmetalbr Not quite sure if this is the con of recycled actors: they are never destroyed so after creation there will always be in the resource pool occupying space, unless they are cleaned up sometime. Having too many will reduce avaliable resources to other tasks...
So, am I right? 010 months, 1 day ago
luddet Recycling things is usually good. However it requires you to have those created, even when they are not used, which allocates memory. So don't recycle more things than necessary. 111 months, 4 weeks ago