15,544

Views

Intermediate

Level

0

Comments

Custom Events

by Jon (Updated on 2014-01-26)


Contents

  • What are Custom Events?
  • Defining Custom Events
  • Triggering Custom Events
  • One Recipient vs. All
  • Making the Custom Event Configurable
  • Custom Events vs. Custom Blocks

 

What are Custom Events?

Custom Events are special kinds of events that happen only when you tell them to happen.

Why would you want to do this?

  • Reuse Logic. If you're finding that you're copying and pasting the same code, you could stick it into a Custom Event and trigger that Custom Event instead.
  • Allow other behaviors to cause stuff to happen
     
Aside: In programming terms, Custom Events are equivalent to the concept of "messaging" or "indirect invocation" - they aren't quite the same as function calls since you aren't always specifying a receiver. There can be as few as 0 receivers or infinitely many.

 

Defining Custom Events

1) Inside the Behavior Designer, go to Add Event > Advanced > Custom Event

2) Type something into the blank. This is the text that will cause this Custom Event to trigger.

That's it! Now fill out what you want to happen inside the event.

 

Triggering Custom Events

Custom Events are triggered by any one of the blocks under Behavior > Triggers. The act of telling a Custom Event to happen is called triggering.

To trigger an event, put in the text you stuck into the blank when defining the event.

That's all there is to triggering Custom Events.

You might be curious about what the difference is between the "all behaviors" vs. "(one) behavior" variants though...

 

One Recipient vs. All

As you have may noticed, triggering Custom Events can be done directly (one behavior) or indirectly (all behaviors). This to say:

  • You can specify exactly which behavior you're targeting.


     
  • You specify no target, so it attempts to trigger the event for all behaviors for a particular Actor or Scene.


     
Note: If a behavior doesn't have a Custom Event and you try to trigger it, nothing happens. It's ignored.


When would specifying no target ("all behaviors") be useful?

  • A bunch of behaviors happen to receive the same event, and you want to target them all at once.
  • You don't feel like typing in the target behavior, so you target them all. Besides a minor performance penalty, there's nothing wrong with this.

 

Making the Custom Event Configurable

In some rare instances, you may find a need to make a Custom Event configurable. Which is say that you want to stick a Text Attribute into the blank.

You can do this, and as you'd expect, the text for Custom Event becomes configurable.

Warning: However, it is not advised that you change the value of the Text Attribute at runtime during the game. The Custom Event may break if you do this.

 

Custom Events vs. Custom Blocks

Custom Events and Custom Blocks overlap somewhat in use case. Custom Events differ from Custom Blocks in one key area: they don't accept "parameters." If you need to pass some data in, you have to use a Custom Block instead.

We've got an entire article devoted to Custom Blocks.

 

Summary

  • Custom Events are events that you trigger yourself.
  • You define the text that triggers the event.
  • Generally speaking, it's easier and simpler to just target all behaviors rather than specifying a specific recipient.
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!

0 Comments

Be the first to make a comment!

Sign In to Comment