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
  • RoomPropertyAgent
  • Adding "PlayerScores" SyncProperty
  • Rules for wining the game
  • GameData and PlayerScore
  • Update the GameSceneManager.cs script

Was this helpful?

  1. Tutorials
  2. Third-Person Shooter

Updating Room States

3 - 5 minutes read

PreviousPlayer RespawnNextWinning the Game

Last updated 5 years ago

Was this helpful?

RoomPropertyAgent

You have used SyncPropertyAgent to update player properties. RoomPropertyAgent is similar to SyncPropertyAgent, but it is designed to updates room level properties.

Select the GameManager GameObject in the Scene Hierarchy, Click the Add Component Button and search for "GameDataManager". Select RoomPropertyAgent to Attach it to the GameManager GameObject.

Similar to SceneSpawner, a Network System ID component is attached automatically. You can have multiple RoomPropertyAgents, make sure you assign different Agent Id to them.

Adding "PlayerScores" SyncProperty

Click Add Game Sync Property button and name the new SyncProperty "PlayerScores". Set Type to Serializable.

Rules for wining the game

For this tutorial, the player who killed opponents 3 times is the winner of the game. You can use serializable objects to store player scores.

GameData and PlayerScore

Create a new script and call it GameDataModels.cs. Add GameData and PlayerScore Serializable Classes.

using System;
using System.Collections.Generic;

[Serializable]
public class PlayerScores
{
    public List<Score> scores = new List<Score>();
}

[Serializable]
public class Score
{
    public string playerRemoteId;
    public int score;
}

Update the GameSceneManager.cs script

GameSceneManager should have a reference to its RoomPropertyAgent component.

RoomPropertyAgent roomPropertyAgent;

private void Start()
{
    roomPropertyAgent = GetComponent<RoomPropertyAgent>();
}

Added a method to update player's score.

    public void PlayerScored(string playerId)
    {
        // Read the current value of the "PlayerScores" SyncProperty.
        PlayerScores playerScores = roomPropertyAgent.GetPropertyWithName("PlayerScores").GetValue<PlayerScores>();

        // Initialize the playerScores object.
        if (playerScores == null)
        {
            playerScores = new PlayerScores();
        }

        bool foundPlayerScore = false;

        // If player already have a score, increase it by 1.
        foreach (Score s in playerScores.scores)
        {
            if (s.playerRemoteId == playerId)
            {
                s.score++;
                foundPlayerScore = true;
            }
        }

        // If player has not scored yet, add a new score for the player and set its value to 1.
        if (!foundPlayerScore)
        {
            Score ps = new Score();
            ps.playerRemoteId = playerId;
            ps.score = 1;
            playerScores.scores.Add(ps);
        }

        // Modify the "PlayerScores" SyncProperty
        roomPropertyAgent.Modify<PlayerScores>("PlayerScores", playerScores);
    }