Playground Attributes

Checked with version: 2017.4

-

Difficulty: Beginner

Attributes are a category of scripts that usually don’t do much on their own: they just define qualities that object have, and then some other script is going to act based on those Attributes. They have a role similar to Tags, but being scripts they can come with extra data.

BulletAttribute

description

Requires: Collider2D of any shape

description

The Bullet script has no functionality of its own, but it holds a reference to which Player has shot the projectile. This number (playerID in the code) can be 0 (player 1) or 1 (player 2).

The ID is automatically set by the ObjectShooter script when the projectile is launched.

Note: If for any reason you want to set the playerID property in the Inspector, open the BulletAttribute script and remove the attribute [HideInInspector] from the property. This could be useful in case the projectiles are created by a script that is not ObjectShooter.

CollectableAttribute

description

Requires: Collider2D of any shape

description

CollectableAttribute awards a point to any player who touches the object. As such, it requires a Collider2D and potentially that this is marked as a Trigger to avoid collisions.

Its only property, Points Worth, allows to assign a different value to each object.

Note: To see the total score on screen, a UI prefab needs to be present in the Scene. Refer to the Getting Started manual for more information on the UI prefab.

DestroyForPointsAttribute

description

Requires: nothing

description

DestroyForPointsAttribute is good for targets and enemies in shooting games. It destroys the GameObject on collision with another object, only if this has the BulletAttribute script. Also, it awards points to the Player who originally shot the bullet.

See BulletAttribute and ObjectShooter scripts for more information on how IDs are assigned to bullets.

Note: To see the total score on screen, a UI prefab needs to be present in the Scene. Refer to the Getting Started manual for more information on the UI prefab.

HealthSystemAttribute

description

Requires: nothing

description

The HealthSystemAttribute can be added to characters, enemies or objects. It allows them to take damage, and potentially be removed from the game if this goes to 0 - which in the case of the Player it might mean Game Over.

The script works in conjunction with the ModifyHealthAttribute (see below), which is necessary even on bullets. Similarly, an object with ModifyHealthAttribute doesn’t affect an object that doesn’t have HealthSystemAttribute.

HealthSystemAttribute doesn’t enforce the presence of a Collider2D, but it’s recommended to have one if you are relying on collisions to subtract health from your player.

Note: To see the player’s health on screen, a UI prefab needs to be present in the Scene. Refer to the Getting Started manual for more information on the UI prefab.

ModifyHealthAttribute

description

Requires: a Collider2D of any shape

description

This attribute makes any physical object capable of subtracting or adding health to any object that has the HealthSystemAttribute (see above). It is useful for bullets, hazard zones, etc. but also for things that can heal, like medipacks, food, and more.

Destroy When Activated is a property that, when checked, removes this object the first time it produces its effects. Use it for bullets, consumables, and anything that has to act only once.

The Health Change property indicates the change in health that this object produces. If it’s negative, it’s damaging. If positive, it is healing. The last line in the Inspector can turn red or blue depending on which "mode" you are on.

ResourceAttribute

description

Requires: a Collider2D of any shape, and a SpriteRenderer

description

ResourceAttributes, together with ConsumeResourceAction, opens the ability to include resources and an inventory to games made with Playground. Touching an object that has this script picks it up, and adds it to an inventory displayed by the UI.

The script requires a SpriteRenderer since the Sprite is used to display an icon in the lower-left corner, alongside a number representing the amount of that resource the player has:

description

Picking up more resources of the same type adds up to the number. A ConsumeResourceAction script can then request and consume them. See its description for more information.

With resources, you can create a crafting system (collect resources, then use them to "pay" and get an item in return) or simply a door/key system, where each door needs the right key to open.

You define which type this resource belongs to with the Resource of Type property (see below), and optionally you can specify an Amount (useful for coins and money).

Defining resource types

Resource types can be defined by clicking on the Add/Remove types button. This focuses the editor on a ScriptableObject called "InventoryResources". On this object, a list of strings defines all the available types of resources in the game.

description

Several have already been added for your convenience, but you can add and remove more to create more custom gameplay. Once you add a resource type, be sure to go back to the object that has the ResourceAttribute script and assign it.

Note: Resource types are shared between all scenes, so if you remove the base types (Coin, Star, etc.) some Example scenes like Roguelike will stop working. Moving the location of the "InventoryResources" object will also break the system.