Skip to content
VitalRouter
Esc
navigateopen⌘Jpreview

ICommand

First, define the data types of the event/message you want to dispatch. In VitalRouter this is called “command”. Any data type that implements ICommand will be available as a command, no matter what the struct/class/record is.

For example, the following definitions are all valid.

public readonly record struct FooCommand(int X, string Y) : ICommand;
public readonly struct FooCommand : ICommand
{
    public int X { get; init; }
    public string Y { get; init; }
}
public class FooCommand : ICommand
{
    public Guid Id { get; init; }
    public Vector3 Destination { get; init; }
}

The ability to identify the type of event being sent by type has many advantages, so VitalRouter enforces this.

One of the advantages of event being a data type is that it is serializable.

[Serializable]       // < When you want to serialize to a scene or prefab in Unity.
[MessagePackObject]  // < When you want to go through file or network I/O by MessagePack-Csharp.
[YamlObject]         // < When you want to go through configuration files etc by VYaml.
public readonly struct CharacterSpawnCommand : ICommand
{
    public long Id { get; init; }
    public CharacterType Type { get; init; } 
    public Vector3 Position { get; init; }      	
}

Next, define the publisher/subscriber for the command. This can be done in some styles.

  1. Define a class that defines declarative routing.
  2. Simply register an event handler directly.

Was this page helpful?