1 – Attributes in GameSalad

In this tutorial, we’ll take a look at Attributes in GameSalad.  Attributes are one of the most powerful aspects of GameSalad.  They are essentially value-holders; they’re designed to store numerical or text values to be used for different situations.  They can easily be used for anything from the angle at which a cannonball is launched to displaying the name of a character in a game to recording a rapidly-changing score.  Understanding how to use Attributes properly is crucial to unlocking the full power of GameSalad.

Attributes are essentially variables.  Any time you need to record a number (or an angle, or a piece of text), creating a new attribute is probably the best way to do it.  If you’re struggling with this concept, focus on the numeric aspect of attributes.  For instance, say you’re building a racing game.  Creating an attribute to record the speed of your racer gives you the flexibility to easily change that speed in a way that can be referenced in a variety of situations, such as accelerating, braking, and collisions.  At the same time, another attribute might be used to record the amount of time it takes to go around the track, or your final score.

GameSalad comes with a large number of existing attributes in every game project, storing information about actors, scenes, and the game itself. These preset list of attributes control certain base activities, such as scene size, actor motion, and game time. There are also options for creating custom attributes for any information you may wish to store as the developer. We’ll touch on existing attributes briefly, and then discuss how to use custom attributes for anything in your game.

Attributes exist for your game, your scenes, and your actors. Game attributes can be accessed by all actor prototypes and instances so that they can be seen in any scene. Scene attributes can only be accessed in a particular scene but all actor instances in that scene can individually be edited to view, use, and modify those scene attributes. Actor attributes can only be accessed by an actor instance, but can be initially defined and used in an actor prototype so that all instance of that prototype can have predefined behaviors.

There are six different types of attributes:

  • Boolean: these are true/false values. One example use for this could the status of a button or door, where “true” would be open / on, and “false” would be closed / off.  All Actors start with at least one boolean attribute by default under “physics” in their attribute list to toggle whether or not the actor is movable. Boolean attributes can be changed to true or false with the “change attribute” behavior.
  • Text: letter and number values. These are primarily used in conjunction with the “display text” behaviors to display scene titles, actor names, and much more.
  • Integer: whole number values, such as 15, 0, 42, and 5801.  Integer is one of the more versatile attributes.  A few of the many possible uses are storing the game’s score, specifying the number of units to spawn, or keeping track of how many reward objects are left in a scene.
  • Real: allows for decimal values, such as 3.582, 0.882, and -2.5. Obviously integers can work here as well.  Real attributes allow developers to include fractional events, such as time ticking down (or up), pouring a volume of liquid, or hit point / damage records where fractions of a point can be dealt.
  • Angles: allows for values from 0 to 359, representing the degrees of a circle. You can use decimal values here as well within this range.  Angle attributes are primarily used with objects that rotate or have some sort of angular movement, including launchers, such as the cannon in our “cannon physics” template.
  • Index: positive whole numbers, such as 0, 1, 58, 2804.  Index attributes can fulfill many of the same roles as Integers, but have the interesting feature of being unable to store a negative number.  For instance, an index attribute with a value of 4 that was told to subtract 6 would then have a value of 0, not -2.

Existing attributes in your game can be viewed in the Scene editor (in the Inspector, chose either the “Game” or “Scene” tab, and then the “attributes” tab below) and in the Actor editor (the left-hand side contains a list).  These existing attributes record data about your game, scene, and actors, such as their size, location, color, and more.  However, custom attributes contain data that is unique to your game – how fast your race car is moving, or how many blocks remain in a puzzle.  Simply click the “+” button at the bottom of any list of attributes, and rename as desired.

Because you’ll be accessing the value held by this attribute from actor behaviors, rename it something that makes sense to you and you’ll be able to remember later – such as “race car speed”, or “blocks remaining”.  At times, you’ll want to record the location of an actor – you can do this through attributes as well, and these attributes might be named something like “PlayerX” and “PlayerY”, to record the X and Y coordinates of the player.

To reference an attribute once you’ve created it, use the expression editor (“e”) or attribute browser (“…”) in any behavior.