Switch to Book Layout1: Getting Started2: Building Logic
3: Actors
4: Scenes
5: Game Mechanics6: Advanced Topics7: Testing & Tuning8: The Last 10%M1: Mobile - IntroM2: Mobile - BasicsM3: Mobile - ServicesM4: Mobile - PublishingA: Troubleshooting
B: How-To Guides
C: ReferenceD: Resources3.0 Drafts (In Progress)
|
||
|
Level: Beginner
Optimizing Actor PerformanceOne 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.
Contents
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 FrameIf 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.
Solution 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 PossibleThere 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
15456 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!
7 Comments
Dincicode
Does switching animations from 100 milliseconds to 200 milliseconds for actors improve performance, and what about looping animations does that make a difference if the actors only have 1 frame?? 1 2 months, 2 days ago
Silux
I found a cause of lag also having many layers of trasparent backgrounds/foregrounds, to create an actor that collides upon another, bigger than screen and always simulating actors, creating more than 12 recycled actors at once(included point at and set velocity), to create trasparent actors. 0 1 year, 2 months ago
bigdaddio
GC is not all that great, no matter what anyone says. I have also created XBLIG and the GC will often take more than a frame to complete causing a frame to drop. Do this often enough and you get a stuttering mess that since we do not have very deep debuggers would be difficult to find. 0 1 year, 8 months ago
SpaghettiToastBook
One of the pictures has a create actor block in the "when drawing" wrapper. 1 1 year, 8 months ago
Jon
The recycle functionality is not completely bug free. That's why it isn't the default yet. Creating new actors on the fly is expensive in Box2D. We found substantial, very substantial performance improvements using recycled vs. not. 1 1 year, 10 months ago
Luxon5
Yes I also wonder about the difference between recycled and non-recycled actors. If recycled actors improve performance, why not use them all the time? Why would you ever have to avoid it and use a regular actor instead? 0 1 year, 10 months ago
herby
Why all that complication with the recycled actors? In my opinion it severely complicates design and can lead to bugs. Is garbage collecting not good enough to do its work (it does it anyway, for all the rest)? -1 1 year, 11 months ago |






















