Get/Set Attributes for Other Behaviors
by Jon (Updated on 2014-01-26)
- Getting Attribute Values
- Setting Attribute Values
- Example: Revisiting the Level Selector
Attributes are properties, but beyond that, they store the "state" of a behavior (or set of Events). Sometimes, it's necessary to know the values of attributes from other behaviors, and sometimes, you even want to change their values too.
When would this be useful?
- An enemy might want to know a player's remaining health and pursue them more vigorously if they're low on health.
- You're creating a bunch of actors all at once, but you want to customize their values on the fly after creating them. For example, you could be making a bunch of enemies with random starting health points.
Getting Attribute Values from Other Behaviors
Getting an value of an attribute requires at least 2 pieces of information, sometimes 3.
- Which Behavior contains the attribute you want?
- Which Attribute do you want, for that behavior?
- (If applicable) From which actor do you want to pull the behavior + attribute from?
How do you fill in the blanks?
Thanks to Stencyl 2 and beyond, this process is semi-automated, but let's step back a bit and understand what each of the blanks mean, so you aren't lost when you browse your logic later on.
- The first blank lets you pick the target Actor. (For Scenes, there's no blank at all since it applies to the current Scene)
- The second blank takes the internal name of the desired attribute.
The final blank takes the name of the behavior that contains the desired attribute.
Just click on the dropdown arrow for the blank, expand out Attribute Names and pick out the desired Attribute.
Doing this will automatically fill out the field with the Attribute's internal name AND its Behavior's full name. No typing required!
(Aside: The strange name: "ActorEvents_1" stems from the fact that the "Events" page for an Actor or a Scene is represented as a hidden behavior behind the scenes, as you may recall from Chapter 2's lesson on Events.)
Setting Attribute Values for Other Behaviors
Setting the value of an attribute is similar to getting it. The only difference is that you have to provide the value you wish to set the attribute to.
Example: Revisiting the Level Selector
Back in Chapter 4, we made a Level Selector for the Changing Scenes lesson.
In this example, we used an Attribute setter block to assign each level button a number corresponding to the scene it would lead to (and what number it would visually display on the button itself).
- You can get and set the values of attributes contained in other behaviors.
- In Stencyl 2 and later, just use the dropdowns to fill in the blanks. Don't type in names directly.