Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Changing Ownership

Peter Halasz edited this page Mar 30, 2020 · 4 revisions

Ways to change ownership

There are two different methods to use for changing ownership of an object; one for the client and one for the server. The client method is TakeOwnership and has no arguments. Any client can call this method on the networkObject to request ownership of that object. The method that you would use to force ownership from the server is AssignOwnership which has an argument NetworkingPlayer (the player who is going to be the new owner for the network object). Only the server can call this method on the networkObject due to it's authoritative nature. If a client were to call this method, nothing would happen.

Validating ownership change

We live in a world where we just can't trust our clients to send us messages that they should be in the way that we wish them to. Clients may want to cheat the system and try to exploit the network for fun or just so that they can win.

By default when a client calls TakeOwnership() the server will not check if the client can actually get ownership or not and simply allow the change. To actually get the server to check and approve the ownership change there is one method to override. Before continuing with this part of the documentation, please review the Extending Generated Classes documentation to learn more about partial classes.

When we use the Network Contract Wizard (NCW), it will create 2 generated classes; one for our MonoBehaviour and one for our NetworkObject.

The one that we want to focus on in this example is the generated NetworkObject. So let's say that we opened up the Network Contract Wizard (NCW) and we created a contract named Car ; this will generate a NetworkObject class named CarNetworkObject.

Now in another folder (not in the Generated folder) you will create a new C# script called CarNetworkObject. In here you will create your partial class for the CarNetworkObject and you will override the AllowOwnershipChange method like so:

AllowOwnershipChange

protected override bool AllowOwnershipChange(NetworkingPlayer newOwner)
{
	// The newOwner is the NetworkingPlayer that is requesting the ownership change, you can get the current owner with just "Owner"
}

Notice that you need to return a boolean from this function. If you return true then the ownership change will be allowed to go through, it will be invoked on the server and on the clients. If you return false then the ownership change will not be invoked at all and will be dropped.

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Forge Networking Alloy
Steamworks
Clone this wiki locally