-
-
Notifications
You must be signed in to change notification settings - Fork 307
Changing 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.
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:
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.
Getting Started
Network Contract Wizard (NCW)
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
Master Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
-
Connection Cycle Events
-
Rewinding
-
Network Logging
-
Working with Multiple Sockets
-
Modify Master and Standalone servers
-
NAT Hole Punching
-
UDP LAN Discovery
-
Offline Mode
-
Ping Pong
-
Lobby System
-
Upgrading Forge Remastered to Develop branch or different version
-
Forge Networking Classic to Remastered Migration Guide
-
Script to easily use Forge Networking from sources
-
Run Two Unity Instances with Shared Assets for Easiest Dedicated Client Workflow