Basic Attributes

While you can use any attribute solution you want, I’ve included a ‘Basic Attributes’ solution for you.

The Basic Attributes component is actually more advanced than a simple list of floats. It allows you to assign attributes of different types as well as ‘tag’ objects.

Simply select the value type and press the ‘+’ button to add the attribute. You can change the name as needed.

The following types are allowed:

  • Float
  • Integer
  • Boolean
  • Tag
  • String
  • Vector2
  • Vector3
  • Quaternion
  • Transform
  • GameObject

 

Tags

Tags are similar to Unity’s tag field for objects except you can assign multiple tags to a single item.

Tags have no actual value. It’s simply a matter if the named tag exists or not.

 

Code

To access the attributes through code, you can use exposed functions like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;
using com.ootii.Actors.Attributes;

public class SampleComponent : MonoBehaviour
{
    public void SomeFunction()
    {
        BasicAttributes lAttributes = gameObject.GetComponent<BasicAttributes>();

        lAttributes.AddAttribute("Health", typeof(float));

        float lValue = lAttributes.GetAttributeValue<float>("Health");

        lAttributes.SetAttributeValue<float>("Health", 100f);

        lAttributes.AttributeExists("Tag1");

        lAttributes.AttributesExist("Tag1,Tag2", true);

        lAttributes.RemoveAttribute("Health");
    }
}

Events

Basic Attributes also support events. In this way, you write code that is called when attribute values change. There’s three ways to do this:

OnAttributeValueChangedEvent

This is a code based approach where you set a function as the handler for the property. Following standard C# practices, you’d add a function like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using UnityEngine;
using com.ootii.Actors.Attributes;

public class AttributeChangeHandler : MonoBehaviour
{
    void Start()
    {
        BasicAttributes lAttributes = gameObject.GetComponent<BasicAttributes>();
        lAttributes.OnAttributeValueChangedEvent += OnValueChanged;
    }

    void OnValueChanged(BasicAttribute rAttribute, object rOldValue)
    {
        // Code here
    }
}

Unity Events

This approach allows you to use standard Unity events for handling an AttributeMessage that describes the value change.

In this case, you assign the function that will handle the AttributeMessage. Then, you can interrogate it. The AttributeMessage class has the following properties:

  • Attribute – BasicAttribute that changed
  • Value – object that is the old value before the change

 

Event System – Dispatcher

If you use my Event System – Dispatcher asset, you can listen for a message where Type is equal to AttributeMessage.MSG_VALUE_CHANGED (5200).

You can see other message ID values here.

 

Notes

Any attribute solution you use will need to implement the IAttributeSource interface. This interface provides the basic blueprint for how to get and set attribute values.

 

Page Contents