Get/Set Attributes for Other Behaviors
by Jon (Updated on 2015-10-18)
- Getting Attribute Values
- Setting Attribute Values
- Example: Revisiting the Level Selector
Attributes are often thought to be like "local variables" or variables that are linked to a behavior. What if you want to know the value of attributes from other behaviors? Or what if you want to change those other values?
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?
All of the blocks for getting (and setting) attribute values are located under Behavior > Attributes.
How do you fill in the blanks?
This process is facilitated by Stencyl, but before we get to that, 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.
Now that you know this, rest assured that you don't need to type in the attribute and behavior names directly.
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.
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.
Note: Take care to set the values to the appropriate type. That is to say, don't try dumping a text value into a Boolean attribute. This will cause errors at compile-time or run-time.
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).
Note: The +1 is to account for the fact that computers count, starting from 0, but it would look strange to a regular player to see "Level 0"
- You can get and set the values of attributes contained in other behaviors.
- Just use the dropdowns to fill in the blanks. Don't type in names directly.
Directions unclear. I got a hot dog stuck in my ceiling fan.
In the example above with the Boy's HP, how would I place that block in Stencyl? What Flow piece do I use to return that info?
"(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.)"
This is not explained anywhere. I tried to build my own level selection screen and it just doesn't work because I'm missing this "hidden behaviour".
It's not explained anywhere in Chapter 2, Google is not helping either (appearently no one else has this problem) and the dropdown menu doesn't work either.
Could anyone help me with this?
(Just to make sure: I'm trying to use the "for [Actor], set [text] to [anything] for [behaviour]" block.)
i have an ennemie that is like a goomba but i want to make it so that if i touch the sides of him i die but i also want to make it so that i can kill it by jumping on him anyone can help?
This seems like it's very out of date. The getter block in Stencyl 2.2 looks nothing like this example. For instance, the first dropdown doesn't let me select a specific actor. I tried creating an actor attribute, but these seem to be scoped locally to the behaviors of the actor I created it for, and I can't use them to reference other actors.
I'm trying to draw the current speed of the player's character on the screen. I've got the player's character and the actor for drawing speed, anchored to the screen.
When I'm trying to use "for self, get smth from behavior smth" to get the current speed from the attribute, used in player's actor, I've got nothing, but "null" on the screen. Can't understand what's wrong.
I often use the loops under Scene > Actor such as "for each actor of type" when I need one actor to get or set an attribute for another actor. There's probably another section that talks about this but I think it should be mentioned here as well.