Ownership
Understanding how to use ownership, as well how it affects clients and the server is essential for any project.
What is Ownership?
Ownership is a core concept in Fish-Net, determining which client has control over an object.
Every NetworkObject in Fish-Net can be assigned a client to own it, or the object can have no assigned owner. The server is exclusively responsible for changing ownership of objects, even though it cannot own an object itself.
When a client owns an object, they are considered its rightful user, capable of actions like moving a character or using a weapon. Ownership can also be temporarily granted for interactions with world objects, such as using a turret.
Ownership checks are essential for ensuring proper control over objects. Common scenarios include:
Validating if a client can manipulate an object.
Checking ownership status for player-related values like name or score.
Ensuring only the owner executes specific network calls (e.g., ServerRpcs).
Assigning Ownership
Ownership can be granted in several ways:
Spawning with Ownership: When creating an object, ownership can be assigned to a specific connection.
Changing or Adding Ownership: If an object is already spawned, ownership can be modified at any time. Previous owners are replaced with the new owner.
Removing Ownership: Ownership can be revoked from a client when needed.
ownerConnection
and newOwnerConnection
above represent a NetworkConnection that will be set as the new owner.
Checking Ownership
Ownership can be verified via the following NetworkObject or NetworkBehaviour properties:
Simple Movement Example
A client must own an object to execute certain actions. Below is an example of movement logic restricted to the object's owner:
Transferring Ownership
Only the server can assign, transfer, or remove ownership. Typically, ownership is granted when spawning an object.
Automatic Ownership Assignment: The PlayerSpawner script (found in the NetworkManager prefab) ensures the spawned player object is owned by its respective client.
Last updated