39,068

Views

Beginner

Level

12

Comments

Creating and Destroying Actors

by Jon (Updated on 2014-01-26)


Contents

  • Introduction
  • What are Recycled Actors?
  • How To: Creating Actors
  • How To: Destroying Actors
  • Events

 

Note for Stencyl 3.0 and above

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.

 

What are recycled actors?

Stencyl is built around a recycled actor model.

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” actor, 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 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.

Note: Killing 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, which many games do.
  • 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?

(View Demo)

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?

(View Demo)

Hint: Use the ‘when an actor of [TYPE] is created/killed’ event!
Disclaimer: All articles are geared towards Stencyl 3.0 and above. Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). 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
0 1 year, 2 months ago
wilycodger
Is there a way to flush the pool of recycled actors?
0 1 year, 3 months 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.
1 1 year, 3 months 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.
0 1 year, 4 months 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.
0 1 year, 4 months 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.
2 1 year, 6 months 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?
0 1 year, 7 months 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?

0 1 year, 8 months 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.
1 1 year, 10 months ago
srivello
Very weak metaphor.
1 1 year, 10 months ago

Sign In to Comment