Optimizing Actor Performance
by captaincomic (Updated on 2014-09-03)
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.
- Avoid Excessive Actor Creation
- Remove Actors when they're no longer needed
Avoid Creating Large Numbers of Actors Every Step
If you place a create actor block into the Always event block, like this...
...Stencyl will attempt to create an actor every single step, which would mean 100 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.