LogoLogo
  • SWNetwork SDK Overview
  • Installation
    • Custom Unity Package
  • Tutorials
    • Third-Person Shooter
      • Starter Project Introduction
      • Install SWNetwork
      • Setting up the NetworkClient
      • Sync Player Transform
      • Setting up the Scene Spawner
      • Test and Play
      • Remote Events
      • SyncProperties
      • Player Respawn
      • Updating Room States
      • Winning the Game
    • Basic Lobby
      • Lobby-start
      • Installing SWNetwork SDK and configuring NetworkClient
      • Registering Player
      • Room CRUD
      • Managing Teams
      • Chat
  • SDK
    • Network Client
      • Check-in
      • Game Servers
      • Network Status Event (beta)
      • Classes
        • NetworkClient Class
      • Methods
        • CheckIn() Method
        • ConnectToRoom(Action<bool>) Method
        • DisconnectFromRoom () Method
        • FindSpawner(ushort) Method
    • Game Play
      • Network ID
      • Realtime Agent
      • Remote Event
      • Sync Property
        • Restore Sync Properties
        • Conflict Resolution
      • SceneSpawner
      • RoomPropertyAgent
      • RoomRemoteEventAgent
    • Lobby
      • Register Player
      • Message Player
      • Create Room
      • Change Room Settings
      • Get Rooms
      • Filter Rooms
      • Join Room
      • Message Room
      • Get Players in Room
      • Get Room Custom Data
      • Change Room Custom Data
      • Kick Players
      • Start Room
      • Leave Room
      • Lobby Room Events
        • OnLobbyConnectedEvent
        • OnPlayerMessageEvent
        • OnRoomCustomDataChangeEvent
        • OnNewPlayerJoinRoomEvent
        • OnPlayerLeaveRoomEvent
        • OnNewRoomOwnerEvent
        • OnRoomStartingEvent
        • OnRoomReadyEvent
        • OnFailedToStartRoomEvent
        • OnKickedEvent
        • OnRoomMessageEvent
      • Classes
        • SWLobby Class
        • SWPlayer Class
        • SWRoom Class
        • SWRegisterReply Class
        • SWGetRoomReply Class
        • SWJoinRoomReply Class
        • SWGetRoomCustomDataReply Class
        • SWGetPlayersReply Class
        • SWLobbyIndexData Class
        • SWLobbyFilterData Class
        • SWGetRoomFilterReply Class
        • SWLobbyError Class
        • SWMessagePlayerEventData Class
        • SWMessageRoomEventData Class
        • SWRoomCustomDataChangeEventData Class
        • SWJoinRoomEventData Class
        • SWLeaveRoomEventData Class
        • SWRoomChangeOwnerEventData Class
        • SWStartRoomEventData Class
        • SWRoomReadyEventData Class
        • SWFailedToStartRoomEventData Class
      • Methods
        • Register(Action<bool, SWRegisterReply, SWLobbyError>) Method
        • MessagePlayer(string, string, Action<bool, SWLobbyError>) Method
        • CreateRoom(bool, int, Action<bool, string, SWLobbyError>) Method
        • ChangeRoomSettings(int, int, Action<bool, SWLobbyError>) Method
        • GetRooms(int, int, Action<bool, SWGetRoomReply, SWLobbyError>) Method
        • FilterRoom(SWLobbyFilterData, byte, Action<bool, SWGetRoomFilterReply, SWLobbyError>) Method
        • JoinRoom(string, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinRoomRandomly(Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinOrCreateRoom(bool, int, int, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • MessageRoom(string, Action<bool, SWLobbyError>) Method
        • GetRoomCustomData(Action<bool, SWGetRoomCustomDataReply, SWLobbyError>) Method
        • GetPlayersInRoom(Action<bool, SWGetPlayersReply, SWLobbyError>) Method
        • ChangeRoomCustomData(string, Action<bool, SWLobbyError>) Method
        • StartRoom(Action<bool, SWLobbyError>) Method
        • LeaveRoom(Action<bool, SWLobbyError>) Method
  • Open Source Software Used
    • Credits
Powered by GitBook
On this page
  • Create Remote Event
  • Invoke Remote Event
  • Examples
  • Handle Remote Event

Was this helpful?

  1. SDK
  2. Game Play

Remote Event

For sending events of GameObjects

PreviousRealtime AgentNextSync Property

Last updated 5 years ago

Was this helpful?

Create Remote Event

Remote Events are created in the Unity Editor Inspector by selecting the Add Remote Event button.

You can have up to 250 remote events in one Remote Event Agent.

Maximum message size of a remote event is 4KB.

Name

Function

Event Name

Unique name of the Event. The Event name is used to find and trigger event across different network. Event name must be unique in the same Remote Event Agent Component.

Target

Controls the recipients of the remote event.

All: All the players in the room.

Exclude self: All the players in the room except the sender.

Host only: Room host only.

Invoke Remote Event

Remote Events are invoked by calling SWNetwork API in C# scripts.

Invoke a Remote event

public void Invoke(string eventName)

Invoke a Remote event with the specified message

public void Invoke(string eventName, SWNetworkMessage msg)

Examples

The eventName string parameter pass to the Invoke method has to match with Event Name value in the Remote Event Agent Inspector.

This example uses SWNetworkMessage to send the bullet launch position(vector3) and aim direction (vector3) along with the remote event.

using SWNetwork;
using SWNetwork.RPC;

public class PlayerSkill : MonoBehaviour {

    public Gun gun;
    public Transform launchPosition;
    
    RemoteEventAgent remoteEventAgent;

    void Start () {
        remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();
    }

    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            fireBullet();
        }
    }

    void fireBullet()
    {
        SWNetworkMessage msg = new SWNetworkMessage();
        msg.Push(launchPosition.position);
        msg.Push(transform.forward);
        remoteEventAgent.Invoke("fire", msg);
    }
}

Handle Remote Event

Handling remote event is done by adding listener to the remote event.

Adding Listener using the Inspector

Adding Listener using C# code

void Start () {
    remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();
    remoteEventAgent.AddListener("fire", RemoteFire);
}

void OnDestroy()
{
    remoteEventAgent.RemoveListener("fire", RemoteFire);
}

Implement the handler function

In this example, we used the PopVector3() method on the SWNetworkMessage objet to obtain the launch position and the aim direction of the fire remote event.

public void RemoteFire(SWNetworkMessage msg)
{
    Vector3 position = msg.PopVector3();
    Vector3 direction = msg.PopVector3();
    gun.fireBullet(position, direction);
}