Network ID

Used to Identify a GameObject in a networked game.

NetworkID Class

Find out if local player is the owner of a Networked GameObject

public bool IsMine;

Removes the networked GameObject across the network. The optional parameter specifies the amount of delay in seconds before destroying the networked GameObject.

public void Destroy(float delay = 0.0f);

FAQ

Question: What would be the best way to restore a player status (position, health, etc.) after he closes the game and comes back a moment later? The SceneSpawner would just spawn a new player GameObject, but I'd like to restore its status to when he disconnected

Answer: If players leave temporarily and reconnect back to the rooms, you probably want to use a longer playerTimeToLive to create rooms. So players don't get removed from rooms too quickly.

Background: Every Networked Object must have a NetworkID component, and the SDK assigns a unique id to each NetworkID component.

The id has a range from 1 to 65535.

1-25000 is reserved for player-owned objects. each player can have up to 100 networked objects so id of 100-199 is owned by player 1 in the game, 200-299 is owned by player 2 in the game...

When players connect to the room game server, the game server will assign roomPlayerIDs to them, and roomPlayerID 1 will be assigned to the first player connects to the room. roomPlayerID 2 will be assigned to the second player connects to the room...

If the first player went offline and reconnect back to the room game server before the room playerTimeToLive is over, the room game server still remembers the player's roomPlayerID(which is 1 in this case). so the player will gain ownership of the GameObjects with ID in the 100-199 range. If the first player went offline for too long and got kicked by the room keeper, the player's roomPlayerID will be recycled and appended to the end of the available roomPlayerID queue. So if the player joins the same room again, a new roomPlayerID will be assigned. Say the new roomPlayerID is 3, the player will create new sets of GameObjects and own GameObjects with ID in the 300-399 range.

Examples

Only the GameObjects that the local player owns should receive input updates.

In this example, networkId.IsMine is called to find out if the local player has the authority to move the GameObject.

using UnityEngine;
using SWNetwork;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 6.0F;
    
    CharacterController characterController;
    NetworkID networkId;

    void Start()
    {
        networkId = GetComponent<NetworkID>();
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (networkId.IsMine)
        {
            float speedX = Input.GetAxis("Horizontal") * moveSpeed;
            float speedZ = Input.GetAxis("Vertical") * moveSpeed;
            
            Vector3 movement = new Vector3(speedX, verticalVelocity, speedZ);
            characterController.Move(movement * Time.deltaTime);
        }
    }
}

Last updated