1: Getting Started
2: Building Logic
5: Game Mechanics
6: Advanced Topics
7: Testing & Tuning
8: The Last 10%
M1: Mobile - Intro
M2: Mobile - Basics
M3: Mobile - Services
M4: Mobile - Publishing
B: How-To Guides
3.0 Drafts (In Progress)
Optimizing Actor Performance
One of the most important ways to ensure fast, smooth game performance is by making sure you're using actors in your game efficiently. The following tips can help you do this.
Recycle Actors When Possible
(Note: In Stencyl 3.0 and above, recycling is the only option available and is implicitly done when you create and kill an Actor.)
The game engine Stencyl uses has two types of actors, regular and one the engine can recycle. When the engine creates a regular actor, it introduces a new instance of a type of actor to a scene. The actor stays in the scene until removed. Regular actors are created using the Create Actor block.
Actors the engine can recycle work differently. With a recycled actor, Stencyl creates an actor and then adds it to a pool which exists outside the scene (i.e. a recycled actor in the pool exists in memory but is not visible in the scene in any way). An actor the engine can recycle is created with this block:
When the engine recycles an actor, using this block...
...the actor returns to the pool outside the scene. Once the actor is restored to its original state, it ends up in line to go back to the scene. It's returned when needed. Here's an illustration:
Avoid Creating Large Numbers of Actors Every Frame
If you place a create actor block into the Always event block, like this...
...Stencyl will attempt to create an actor every single frame, which at 60 frames per second, would mean 60 actors per second. This will obviously slow your game to a crawl.
Instead, put blocks that create actors into the When Created, When This Collides, or When This Hears blocks, or constrain actor creation using a conditional (if statement).
In the examples above, we chose to create a recycled actor for bullets and explosions, the types of actors we would want to exist on screen temporarily and potentially have a lot of. F
or the menu button, we'll only have one, and it will be permanent, so we don't need to create a version we can recycle - we can use the regular Create Actor block.
If this all sounds trivial and completely obvious, you might be surprised to see just how often we see this happen in the field. ;)
Remove Actors When Possible
There are many times when you should remove an actor from your game. Examples include bullets, special effects, temporary power ups, enemies, and more.
Any actor the player's actor doesn't need to run into more than once is an actor you should remove. An actor you want to remove (when certain conditions in your game are met) will need a Behavior that provides the logic for removal.
There are two ways to remove an actor, using the kill [actor] block...
...or the recycle [actor] block. Make sure you don't attempt to recycle a regular actor and vice versa.
A few good ways to remove actors are after a set time period, using the Do After block...
The logic in this Behavior is designed to prevent errors from occurring where the engine attempts to recycle an actor that's already been removed.
Other options include when an actor collides with another or when it leaves the screen.
In this case, the boolean attribute called Actor On Screen? is there to ensure the actor isn't removed before it has a chance to appear on screen. You would use logic elsewhere to set that boolean attribute to true once that actor appears on screen. Then, when the actor leaves the screen again, the logic shown above will remove it from the scene.
One other tip for removing temporary actors is to use logic in a Behavior that removes an actor under a range of conditions, e.g. leaving the screen and after a short period of time.
Now it's your turn. Share your Tips.
What have you found, in your experiences, boosts performance of your games when you're in a bind? Use the comments area below to share your stories.
Last Updated: 2013-01-29 by Jon
18631 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!