unity-atoms/docs/tutorials/actions.md
Miika Lönnqvist c7136d2937
Updated documentation and tutorials (#267)
Some tutorials now have animation in them to give a clearer explanation of how things work. Added a short tutorial about creating Atoms with the new Search function. Documentation about installation has also changed.
2021-06-14 20:32:52 +02:00

1.5 KiB
Raw Blame History

id title hide_title sidebar_label
actions Actions true Actions

Actions

The ChangeFillAmount function created in the listeners tutorial is actually a Response of type UnityEvent. However, in Unity Atoms there is also the possibility to create Responses as Scriptable Objects called Actions. An Action is a function as a Scriptable Object that does not return a value. As a side note there are also Functions in Unity Atoms, which are exactly like Actions, but does return a value. To demonstrate the concept of an Action as a Response lets create an Action called HealthLogger.cs that gives some love to the console and logs the players health whenever it changes:

[CreateAssetMenu(menuName = "Unity Atoms/Examples/Health Logger")]
using UnityEngine;
using UnityAtoms.BaseAtoms;

public class HealthLogger : IntAction
{
    public override void Do(int health)
    {
        Debug.Log("<3: " + health);
    }
}

It is possible to create the HealthLogger by right clicking and go Create / Unity Atoms / Examples / Health Logger (this is available due to the call to CreateAssetMenu). When created we can add it as an Action Response to the HealthListener:

health-listener-v3

Every time the players health is changed we now log out the players health. This particular example is pretty simple, but Im sure you can come up with lots of other use cases for it (for example play a sound or emit some particles).