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
  • Why using SyncProperty
  • SyncPropertyAgent
  • Updating the PlayerHP.cs Script
  • Handling the OnReady SyncProperty Event
  • Updating the GotHit() Method
  • Handling the OnChanged SyncProperty Event
  • Adding listeners to the "hp" SyncProperty
  • Play

Was this helpful?

  1. Tutorials
  2. Third-Person Shooter

SyncProperties

5 - 10 minutes read

PreviousRemote EventsNextPlayer Respawn

Last updated 5 years ago

Was this helpful?

In this section, you will use SyncProperty to update player's hp.

Why using SyncProperty

SyncProperties are designed for updating states that are not constantly changing.

  • Only send/receive when the property updates

  • Reliable

  • Supports serializable objects

  • Supports user-defined classes and structs.

  • Supports conflict resolution

SyncPropertyAgent

Select the Player Prefab. Click the Add Component button and Search "Agent", select Sync Property Agent to attach it to the Player GameObject.

Click the Add Sync Property button to create a SyncProperty for the Player. The Property's name is important and has to be unique in the same SyncPropertyAgent component. Set Name to "hp" and set Type to Int. The Conflict Resolution rule is default to Mine. You can use Mine for this tutorial. For more information on Conflict Resolution, please visit the following page.

Updating the PlayerHP.cs Script

Add two properties: networkId, and syncPropertyAgent.

NetworkID networkId;
SyncPropertyAgent syncPropertyAgent;

You can remove the currentHP property as the player hp will be stored in the "hp" SyncProperty.

In the Start() method, and initialize the networkId, and syncPropertyAgent

void Start()
{
    networkId = GetComponent<NetworkID>();
    syncPropertyAgent = gameObject.GetComponent<SyncPropertyAgent>();
}

Handling the OnReady SyncProperty Event

    public void OnHPReady()
    {
        Debug.Log("OnHPPropertyReady");

        // Get the current value of the "hp" SyncProperty.
        currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();

        // Check if the local player has ownership of the GameObject. 
        // Source GameObject can modify the "hp" SyncProperty.
        // Remote duplicates should only be able to read the "hp" SyncProperty.
        if (networkId.IsMine)
        {
            int version = syncPropertyAgent.GetPropertyWithName("hp").version;

            if (version != 0)
            {
                // You can check the version of a SyncProperty to see if it has been initialized. 
                // If version is not 0, it means the SyncProperty has been modified before. 
                // Probably the player got disconnected from the game. 
                // Set hpSlider's value to currentHP to restore player's hp.
                hpSlider.value = currentHP;
            }
            else
            {
                // If version is 0, you can call the Modify() method on the SyncPropertyAgent to initialize player's hp to maxHp.
                syncPropertyAgent.Modify("hp", maxHp);
                hpSlider.value = maxHp;
            }
        }
        else
        {
            hpSlider.value = currentHP;
        }
    }

Updating the GotHit() Method

    public void GotHit(int damage)
    {
        // Only the source GameObject can modify the "hp" SyncProperty.
        if (networkId.IsMine)
        {
            currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();

            // Check if the player is already dead.
            if (currentHP == 0)
            {
                return;
            }

            Debug.Log("Got hit: old currentHP= " + currentHP);

            if (currentHP > 0)
            {
                currentHP = currentHP - damage;
                
                // if hp is lower than 0, set it to 0.
                if (currentHP < 0)
                {
                    currentHP = 0;
                }
            }

            Debug.Log("Got hit: new currentHP= " + currentHP);

            // Apply damage and modify the "hp" SyncProperty.
            syncPropertyAgent.Modify("hp", currentHP);
        }
    }

Handling the OnChanged SyncProperty Event

    public void OnHpChanged()
    {
        // Update the hpSlider when player hp changes
        currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();
        hpSlider.value = currentHP;
    }

Adding listeners to the "hp" SyncProperty

Make sure you applied the changes to the Player Prefab and disabled the Player GameObject in the Hierarchy.

Play

You can follow the steps in section "Test and Play" to test out the "hp" SyncProperty.

Conflict Resolution