Skip to main content

Command Event

We can't use UnityEvent in a command because UnityEvent doesn't support cross-scene reference. So instead, we use CommandEvent to achieve the same functionality.

Not only as a replacement, but CommandEvent also has features which UnityEvent does not support. A command event supports public setter properties and methods with up to four parameters, with the following parameter types:

  • string
  • float
  • int
  • bool
  • Color
  • AnimationCurve
  • Vector3
  • Vector3Int
  • Vector2
  • Vector2Int
  • Vector4
  • Rect
  • Bounds
  • Enum
  • UnityEngine.Object and its subclasses
  • TargetObject<>

Just like UnityEvent, command event also supports both static and dynamic parameters.

Static Parameters​

When declared as CommandEvent without generic arguments, then the event will invoke the callbacks with static parameter(s) that are defined through the command node.

...
public class InvokeEventExampleCommand : Command, ICommandAutoDraw
{

public override float nodeWidth => 350f;

public CommandEvent myEvent;

public override void Execute(CommandExecutionFlow _flow)
{
myEvent?.Invoke();
Exit();
}
}

A built-in Invoke Event command is already available if all that you need is only a simple command that invokes a CommandEvent. However, you will need to create a custom command if you require more advanced conditions before invoking the event.

Dynamic Parameters​

A CommandEvent with dynamic parameters is needed when the parameter can't be pre-defined through the command node, since the value can only be retrieved during runtime.

Similar to UnityEvent, we can also create our own dynamic command event type by declaring a class derived from CommandEvent<>.

[System.Serializable]
[RegisterCommand("Custom/Invoke Int Event", typeof(SequineFlowCommandData))]
public class InvokeIntEventCommand : Command, ICommandAutoDraw
{
public override float nodeWidth => 350f;

[System.Serializable]
public class IntEvent : CommandEvent<int> { }

public IntEvent myIntEvent;

public override void Execute(CommandExecutionFlow _flow)
{
int randomInt = Random.Range(0, 100);
myIntEvent?.Invoke(randomInt);
Exit();
}

}