As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

115,938

Views

Beginner

Level

13

Comments

Creating and Destroying Actors

by Jon (Updated on 2015-10-20)


Contents

  • Creating Actors
  • Referring to the "Last Created Actor"
  • Destroying Actors
  • Events

Creating Actors

Use the create-actor-of-type block (under Scene > Actors).

Create Actor

  • You can drag an actor-type block into the Choose Actor Type field. This is useful when you want to make this field configurable.

  • Front / Middle / Back will place the actor on those layers, respetively. Middle will choose whatever layer is closest to the middle. If you need a specific layer, change the actor's layer after creation.

  • In terms of execution order, when you create an actor, the new actor's "when created" event is run before the current behavior proceeds further.

Referring to the "Last Created Actor"

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.

Last Created Actor

Destroying Actors

Destroying actors is similar to creating them. Use the kill-actor block (under Scene > Actors).

Kill Actor

Killing an actor is immediate. Don’t attempt to refer to an actor at any point after you’ve killed it. Especially during timed tasks (do-after, do-every), where it's easy for this to happen by accident.

Events: Knowing when actors get created or die

We provide events that let you control what happens when an actor is created or destroyed. Select one of these events via the “Add Event” button.

Events

  • When an actor dies, the die event happens before the actor actually dies, so you can still refer to it during the event.
  • When an actor is created, the actor's "when created" event always runs first. Then, the other created events pertaining to the actor are run.

Challenge 1: Create A Particle Effect

Why not try creating a particle effect using lightweight actors (or images) and along the way, compare its performance to doing this with regular actors?

(View Demo)

To create particle effects, just spawn 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.

Print Article Edit Article How to Edit an Article
Disclaimer: 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!

13 Comments

d0n1
Demo links are broken!
2 7 years, 5 months ago
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 11 years, 10 months ago
wilycodger
Is there a way to flush the pool of recycled actors?
2 11 years, 11 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 11 years, 11 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.
-1 12 years, 1 week 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.
-1 12 years, 2 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.
2 12 years, 2 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?
1 12 years, 2 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 12 years, 3 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 12 years, 5 months ago

Sign In to Comment