Skip to main content

Create Network Event

Define structure of event

public class MyEvent: IRagonEvent
{
public string Message;

public void Serialize(RagonSerializer serializer)
{
serializer.WriteString(Message);
}

public void Deserialize(RagonSerializer serializer)
{
Message = serializer.ReadString();
}
}

Register event in Game Manager

public class GameNetwork : MonoBehaviour, IRagonListener
{
[SerializeField] private GameObject CharacterPrefab;

private void Start()
{
RagonNetwork.Event.Register<MyEvent>(); // Here

RagonNetwork.AddListener(this);
RagonNetwork.Connect();
}

Subscribe on event in Ragon Behaviour

public override void OnAttachedEntity()
{
Entity.OnEvent<MyEvent>((player, evnt) =>
{
Debug.Log($"{player.Name} sent MyEvent with message {evnt.Message}");
});
}

Replicate Event

 public override void OnUpdateEntity()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Entity.ReplicateEvent(new MyEvent() { Message = "Test event"});
}
}

Press play, and then you spawned, press Space, you will see next logs event-log