Attributes

Most games are very dynamic; characters move around, players press buttons, and properties like health and score change. Many of these are things that we, as developers, want to keep track of. For cases like these, we’ll use values called Attributes.

What are Attributes?
Attributes are changeable values that are used within Stencyl's Behaviors. They enable Behaviors to keep track of dynamic parts of a game and make it possible for developers to customize Behaviors without editing them directly.

Every Attribute has both a value and an associated type. For example:

An Attribute called Health might have a value of 5 and the type Number. An Attribute called Shirt Color might have a value of Blue and the type Color. An Attribute called Hero’s Name might have a value of Link and the type Text. In total, there are 16 different Attribute types, which are described here. Two of the defining features of Behavior Attributes are that they are changeable and that they can be made customizable.

Changeable Behavior Attributes don't have to keep the original values you set for them. (In fact, in many cases you don't want them to.)

When you first create an Attribute, you'll assign it a default value. Depending on what happens during the game, however, you will want the value to change.

Consider the Health Attribute mentioned above. Although the Attribute might start out with a value of 5, if the character takes damage, your Behavior will change this value to something lower.

Customizable Behavior designers can choose to expose Attributes, which allows them (or other game developers) to customize these values in the Actor Editor. This is one way of making Behaviors reusable.

For example, consider a Behavior called Jump that has been designed with customization in mind. It has one Attribute called Height, which determines how high the Actor Type can jump.

After adding this Jump Behavior to an Actor Type, developers can customize the Behavior to their liking simply by modifying the value for Height in the Actor Editor; there's no reason to modify the Behavior itself.

Here are two Actors Types with the same Jump Behavior attached.

Henry the pumpkin has his Height attribute for the Jump Behavior set to 2. Sticky the stick figure has his value set to 1. Notice that Henry jumps twice as high as Sticky.

stencyl-design-mode-attribute-value

Creating Attributes
Attributes are created as part of an Actor Type or Scene Behavior, using the Add Attribute button in Design Mode.



 Let’s return to the vertical shoot ‘em up game we were working on in theWorking with Behaviors article. We had added an event the creates a laser beam when the player presses a button and makes that laser move upward.



 You’ll notice that we “hard-coded” the speed of the laser to be  20. There are two problems with this.

 To deal with both of these issues, let’s create a  speed  Attribute and use it instead of the value  -20.
 * 1) Any time we wanted to change this value to something different, we’d have to edit the Behavior itself.
 * 2) This value has to be the same for any Actor Type to which this Behavior is attached

 With the  Fire Laser  Behavior open, select the  Attributes  tab beneath the Palette, and then click the  Add Attribute  button. In the dialog that pops up, you can choose a name, description, and type for the Attribute, as well as whether or not to expose the Attribute in the Actor Type Editor (via the Hidden checkbox). We'll add an Attribute called  Speed  and make it customizable in the Actor Type Editor by leaving the checkbox unchecked. Click  OK  to create the Attribute.

 Notice that the new Attribute appears in the Attribute listing. With the Speed  Attribute selected, scroll down inside the  General  pane, and set a default value of  -20  (the same value we’re currently using).