48,469

Views

Beginner

Level

7

Comments

Attributes

by Ceric (Updated on 2012-05-13)


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.
 

Contents

  • What are Attributes?
  • Creating Attributes
  • Using Attributes in Behaviors
  • Hidden vs. Visible
  • Attributes in Dropdowns

 

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.

stencyl-design-mode-attributes-behavior-configuration

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 the Working 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.

stencyl-design-mode-attribute-creation

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

  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

To deal with both of these issues, let’s create a speed Attribute and use it instead of the value -20.

With the Fire Laser Behavior open, select the Attributes tab beneath the Palette, and then click the Add Attribute button.

stencyl-design-mode-add-attribute

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.

stencyl-design-mode-create-attribute-dialog

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).

stencyl-design-mode-attribute-pane

 

Using Attributes in Behaviors

Once an Attribute has been created, Stencyl adds blocks in Design Mode that can we can use to retrieve or modify the Attribute's value.

stencyl-design-mode-attribute-getter-setter-buttons

Let’s make use of the speed Attribute we created.

1) Flip back over to the Palette, and click on the Attributes category. Notice that the Speed Attribute appears, listed beneath its type (i.e., Number).

stencyl-design-mode-attribute-palette-category

2) Drag and drop the Speed block into the set y-speed to [-20] for [Last Created Actor] block.

stencyl-design-mode-drag-attribute-block-example

3) Navigate over to the ship Actor Type which we’ve already attached this Behavior to.

Note: If the ship Actor Type was already open, we’ll have to tell Stencyl to reload the Behavior’s Attributes, so it can pick up the new one we just added. Press Ctrl-R (Cmd-R on Mac) to do this.

4) After selecting the Fire Laser Behavior, the Speed Attribute appears over to the right, having the default value we entered earlier.

stencyl-design-mode-behavior-attribute-configuration-example

 

Hidden vs. Visible

Notice how the laser beam’s speed can now be set without having to go into Design Mode and modify the Behavior. We call this Attribute a visible Attribute, meaning the Attribute is exposed to the Actor Type Editor. 

Attributes that we don’t want to be modifiable from the Actor Type Editor are called hidden Attributes.

We create visible Attributes by not checking the Hidden? checkbox when creating the Attribute.

stencyl-design-mode-hidden-attribute

The larger benefit here comes if we want to attach this same Behavior to another Actor Type – say, a powered-up ship. Not only do we not have to create the logic all over again, but using the same Behavior, we can even choose a different (say, faster) laser beam speed for the new ship.

We don’t always want Attributes to be modifiable, though, such as when we’re keeping track of a player’s score. Hidden Attributes are created by checking the Hidden? checkbox while we’re creating an Attribute.

 

Attributes in Dropdowns

As we've seen in the Working with Behaviors article, some block fields are dropdowns, which let you select from a number of pre-determined options. Attributes can be used here as well.

stencyl-design-mode-block-dropdown

Let's use this technique when creating a second Attribute for our Fire Laser Behavior.

1) Flip back to Design Mode, and following similar steps as before, create a second Attribute called Fire Button, with this one having the type Control. Instead of “hardcoding” the control inside the event wrapper block as before, we’ll make it another value that users can customize from within the Actor Type Editor.

stencyl-design-mode-control-attribute-block-example

2) Click the Control dropdown inside the event wrapper block, and then click Choose Attribute. Select the new Attribute, and click OK.

stencyl-design-mode-select-attribute-dropdown

Tip: Alternatively, we could have just dragged the Attribute block from the Palette directly inside the dropdown box.

stencyl-design-mode-drag-attribute-dropdown

3) Now we can customize this Attribute from the Actor Type Editor, just as we did before. (Remember to reload the document with Ctrl-R or Cmd-R if the new Attribute doesn’t appear.)

stencyl-behavior-customize-attribute-example

 

Summary

  • 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.
  • Two of the defining features of Behavior Attributes are that they are changeable and that they can be made customizable.
  • We sometimes have to let Stencyl know that we’ve added a new Attribute to an existing Behavior. From the Behaviors tab of the Actor Type Editor, press Ctrl-R (Cmd-R on Mac) to do this.
  • Visible Attributes are exposed to the Actor Type Editor.
  • Attributes that we don’t want to be modifiable from the Actor Type Editor are called hidden Attributes.
  • Attributes can be selected from dropdowns, or Attribute blocks can be dragged into dropdowns directly from the Palette.

 

Challenge: Swappable Weapons

Generally, it's a good idea to avoid "hardcoding" values in behaviors you intend to reuse across several Actor Types.

Our Fire Laser Behavior still has a "hard-coded" value by specifying exactly what kind of laser it wants to shoot. What if we want to shoot a different laser?

stencyl-design-mode-attribute-challenge

Create a visible Attribute to replace the Actor Type of the laser beam, and then select an Actor Type for it on the ship's Behaviors page.

Then, create a new Actor Type for a powered-up ship. Attach this Behavior, giving it larger and faster-moving lasers!

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!

7 Comments

lize
????????
2 5 months, 2 weeks ago
Strata
So if you want an attribute to be accessed by multiple actors you need to create a game attribute? So for example. Im trying to make an attribute that counts the number of bullets in existence in a scene.

Since the command to create them is inside the Spaceships Actors events, I have put a plus1 increment counter to attribute 'Bullet Count'

But the events that destroy the bullets are in the Bullet Actors events. So I want these events to decrease the attribute 'Bullet Count'. But the attribute 'Bullet Count' does not transfer between actors.

Im not really sure what I'm doing, but in essence I only want a bullet to be produced if there are no bullets alive at the point of pressing the fire button.

1 1 year, 8 months ago
rainvillain
Very good tutorial! Though you forgot to mention that the Speed attribute should be "number" type. It is fairly obvious (and you can tell it is highlighted in the image), though it might help some people out to explicitly say what type it needs to be :)
0 1 year, 9 months ago
Stevetheipad
Adding in the word "variable" in the what section might be helpful...many new Stencylers know what variables but not attributes are.
0 2 years, 1 month ago
snortch
Very cool toot. The way Stencyl uses attributes and enables reusability is really very nice.

Thank you for making this.

0 2 years, 2 months ago
softwareskull
The next section covers the DUde. Hoping you found it by now.
0 2 years, 3 months ago
ThatSmartDUde
How do i make it that i can access the atribute in a different actor? Is it even possible?
2 2 years, 3 months ago

Sign In to Comment