Skip to content

Commit 89ea413

Browse files
committed
Mark ServerRpc.RequireOwnership as deprecated
1 parent 271e53f commit 89ea413

File tree

23 files changed

+110
-84
lines changed

23 files changed

+110
-84
lines changed

com.unity.netcode.gameobjects/Documentation~/advanced-topics/message-system/rpc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ There are a few other parameters that can be passed to either the `Rpc` attribut
204204
| Parameter | Description |
205205
| ----------------------- | ------------------------------------------------------------ |
206206
| `Delivery` | Controls whether the delivery is reliable (default) or unreliable.<br /><br />Options: `RpcDelivery.Reliable` or `RpcDelivery.Unreliable`<br />Default: `RpcDelivery.Reliable` |
207-
| `RequireOwnership` | If `true`, this RPC throws an exception if invoked by a player that does not own the object. This is in effect for server-to-client, client-to-server, and client-to-client RPCs - i.e., a server-to-client RPC will still fail if the server is not the object's owner.<br /><br />Default: `false` |
207+
| `InvokePermission` | Controls how an RPC is allowed to be invoked.<br /><br />Options:<br /> `RpcInvokePermission.Server` - This RPC throws an exception if invoked by a game client that is not the server.<br />`RpcInvokePermission.Owner` - This RPC throws an exception if invoked by a game client that does not own the object.<br />`RpcInvokePermission.Everyone` - This can be invoked by any connected game client.<br />Default: `RpcInvokePermission.Everyone` |
208208
| `DeferLocal` | If `true`, RPCs that execute locally will be deferred until the start of the next frame, as if they had been sent over the network. (They will not actually be sent over the network, but will be treated as if they were.) This is useful for mutually recursive RPCs on hosts, where sending back and forth between the server and the "host client" will cause a stack overflow if each RPC is executed instantly; simulating the flow of RPCs between remote client and server enables this flow to work the same in both contexts.<br /><br />Default: `false` |
209209
| `AllowTargetOverride` | By default, any `SendTo` value other than `SendTo.SpecifiedInParams` is a hard-coded value that cannot be changed. Setting this to `true` allows you to provide an alternate target at runtime, while using the `SendTo` value as a fallback if no runtime value is provided. |
210210

com.unity.netcode.gameobjects/Documentation~/basics/networkvariable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ This works the same way with dynamically spawned NetworkObjects.
145145

146146
The [synchronization and notification example](#synchronization-and-notification-example) highlights the differences between synchronizing a `NetworkVariable` with newly-joining clients and notifying connected clients when a `NetworkVariable` changes, but it doesn't provide any concrete example usage.
147147

148-
The `OnValueChanged` example shows a simple server-authoritative `NetworkVariable` being used to track the state of a door (that is, open or closed) using a non-ownership-based server RPC. With `RequireOwnership = false` any client can notify the server that it's performing an action on the door. Each time the door is used by a client, the `Door.ToggleServerRpc` is invoked and the server-side toggles the state of the door. When the `Door.State.Value` changes, all connected clients are synchronized to the (new) current `Value` and the `OnStateChanged` method is invoked locally on each client.
148+
The `OnValueChanged` example shows a simple server-authoritative `NetworkVariable` being used to track the state of a door (that is, open or closed) using an RPC that is sent to the server. Each time the door is used by a client, the `Door.ToggleServerRpc` is invoked and the server-side toggles the state of the door. When the `Door.State.Value` changes, all connected clients are synchronized to the (new) current `Value` and the `OnStateChanged` method is invoked locally on each client.
149149

150150
```csharp
151151
public class Door : NetworkBehaviour

com.unity.netcode.gameobjects/Documentation~/terms-concepts/ownership.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Netcode for GameObjects also supports building games with a [distributed authori
88

99
## Ownership in client-server
1010

11-
In a client-server topology, the server has ultimate authority over all NetworkObjects. Clients can request ownership of [specific objects](../components/core/networkobject.md#ownership), but the server has the final say in whether to grant or deny these requests.
11+
In a client-server topology, the server has ultimate authority over all NetworkObjects. Clients can request ownership of [specific objects](../components/core/networkobject.md#ownership), but the server has the final say in whether to grant or deny these requests.x
1212

1313
## Ownership in distributed authority
1414

@@ -58,4 +58,4 @@ When requesting ownership of a NetworkObject using [`NetworkObject.RequestOwners
5858

5959
* [Authority](authority.md)
6060
* [Client-server](client-server.md)
61-
* [Distributed authority](distributed-authority.md)
61+
* [Distributed authority](distributed-authority.md)

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,12 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
610610

611611
private const string k_RpcAttribute_Delivery = nameof(RpcAttribute.Delivery);
612612
private const string k_RpcAttribute_InvokePermission = nameof(RpcAttribute.InvokePermission);
613+
614+
#pragma warning disable CS0618 // Type or member is obsolete
615+
// Need to ignore the obsolete warning as the obsolete behaviour still needs to work
613616
private const string k_ServerRpcAttribute_RequireOwnership = nameof(ServerRpcAttribute.RequireOwnership);
617+
#pragma warning restore CS0618 // Type or member is obsolete
618+
614619
private const string k_RpcParams_Server = nameof(__RpcParams.Server);
615620
private const string k_RpcParams_Client = nameof(__RpcParams.Client);
616621
private const string k_RpcParams_Ext = nameof(__RpcParams.Ext);

com.unity.netcode.gameobjects/Runtime/Messaging/RpcAttributes.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ public class ServerRpcAttribute : RpcAttribute
129129
/// When true, only the owner of the NetworkObject can invoke this ServerRpc.
130130
/// This property sets the base <see cref="RpcAttribute.InvokePermission"/> to <see cref="RpcInvokePermission.Owner"/> when "RequireOwnership = true" or to <see cref="RpcInvokePermission.Everyone"/> when "RequireOwnership = false". />.
131131
/// </summary>
132+
/// <remarks>
133+
/// Deprecated in favor of using <see cref="RpcAttribute"/> with <see cref="SendTo.Server"/> and an <see cref="RpcAttribute.InvokePermission"/>.
134+
/// <code>
135+
/// [ServerRpc(RequireOwnership = false)]
136+
/// // is replaced with
137+
/// [Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
138+
/// // or, as InvokePermission has a default setting of RpcInvokePermission.Everyone, you can also use
139+
/// [Rpc(SendTo.Server)]
140+
/// </code>
141+
/// <code>
142+
/// [ServerRpc(RequireOwnership = true)]
143+
/// // is replaced with
144+
/// [Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Owner)]
145+
/// </code>
146+
/// </remarks>
147+
[Obsolete("ServerRpc with RequireOwnership is deprecated. Use [Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)] or [Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Owner)] instead.)]")]
132148
public new bool RequireOwnership;
133149

134150
/// <summary>

com.unity.netcode.gameobjects/Tests/Editor/NetworkBehaviourEditorTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public void AccessNetworkObjectTest()
3535
var gameObject = new GameObject(nameof(AccessNetworkObjectTest));
3636
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
3737

38+
networkBehaviour.NoNetworkRpc();
39+
3840
Assert.That(networkBehaviour.NetworkObject, Is.Null);
3941

4042
var networkObject = gameObject.AddComponent<NetworkObject>();
@@ -77,6 +79,11 @@ internal class DerivedNetworkBehaviour : EmptyNetworkBehaviour
7779

7880
internal class EmptyNetworkBehaviour : NetworkBehaviour
7981
{
82+
[Rpc(SendTo.Everyone)]
83+
public void NoNetworkRpc()
84+
{
85+
Debug.Log("No Network Rpc");
86+
}
8087
}
8188
}
8289
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformParentingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public override void OnNetworkSpawn()
9999
/// A ServerRpc that requests the server to spawn a player object for the client that invoked this RPC.
100100
/// </summary>
101101
/// <param name="rpcParams">Parameters for the ServerRpc, including the sender's client ID.</param>
102-
[ServerRpc(RequireOwnership = false)]
103-
private void RequestPlayerObjectSpawnServerRpc(ServerRpcParams rpcParams = default)
102+
[Rpc(SendTo.Server)]
103+
private void RequestPlayerObjectSpawnServerRpc(RpcParams rpcParams = default)
104104
{
105105
SpawnedPlayer = Instantiate(PlayerPrefab);
106106
SpawnedPlayer.SpawnAsPlayerObject(rpcParams.Receive.SenderClientId);

com.unity.netcode.gameobjects/Tests/Runtime/Rpc/RpcManyClientsTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ internal class RpcManyClientsObject : NetworkBehaviour
1111
{
1212
public int Count = 0;
1313
public List<ulong> ReceivedFrom = new List<ulong>();
14-
[ServerRpc(RequireOwnership = false)]
15-
public void ResponseServerRpc(ServerRpcParams rpcParams = default)
14+
[Rpc(SendTo.Server)]
15+
public void ResponseServerRpc(RpcParams rpcParams = default)
1616
{
1717
ReceivedFrom.Add(rpcParams.Receive.SenderClientId);
1818
Count++;
1919
}
2020

21-
[ClientRpc]
21+
[Rpc(SendTo.ClientsAndHost)]
2222
public void NoParamsClientRpc()
2323
{
2424
ResponseServerRpc();
2525
}
2626

27-
[ClientRpc]
27+
[Rpc(SendTo.ClientsAndHost)]
2828
public void OneParamClientRpc(int value)
2929
{
3030
ResponseServerRpc();
3131
}
3232

33-
[ClientRpc]
33+
[Rpc(SendTo.ClientsAndHost)]
3434
public void TwoParamsClientRpc(int value1, int value2)
3535
{
3636
ResponseServerRpc();
3737
}
3838

39-
[ClientRpc]
40-
public void WithParamsClientRpc(ClientRpcParams param)
39+
[Rpc(SendTo.SpecifiedInParams)]
40+
public void WithParamsClientRpc(RpcParams param)
4141
{
4242
ResponseServerRpc();
4343
}
@@ -114,7 +114,7 @@ public void RpcManyClientsTest()
114114
success = WaitForConditionOrTimeOutWithTimeTravel(() => TotalClients == rpcManyClientsObject.Count);
115115
Assert.True(success, $"Timed out wait for {nameof(rpcManyClientsObject.OneParamClientRpc)}! Only {rpcManyClientsObject.Count} of {TotalClients} was received!");
116116

117-
var param = new ClientRpcParams();
117+
var param = new RpcParams();
118118

119119
rpcManyClientsObject.Count = 0;
120120
rpcManyClientsObject.TwoParamsClientRpc(0, 0); // RPC with two params
@@ -127,8 +127,8 @@ public void RpcManyClientsTest()
127127

128128
rpcManyClientsObject.ReceivedFrom.Clear();
129129
rpcManyClientsObject.Count = 0;
130-
var target = new List<ulong> { m_ClientNetworkManagers[1].LocalClientId, m_ClientNetworkManagers[2].LocalClientId };
131-
param.Send.TargetClientIds = target;
130+
var target = new [] { m_ClientNetworkManagers[1].LocalClientId, m_ClientNetworkManagers[2].LocalClientId };
131+
param.Send.Target = rpcManyClientsObject.RpcTarget.Group(target, RpcTargetUse.Temp);
132132
rpcManyClientsObject.WithParamsClientRpc(param);
133133

134134
messageHookList.Clear();

com.unity.netcode.gameobjects/Tests/Runtime/Rpc/UniversalRpcTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,70 +432,70 @@ public void DefaultToNotAuthorityDeferLocalRpc(RpcParams rpcParams)
432432
{
433433
OnRpcReceived();
434434
}
435-
436-
// RPCs with RequireOwnership = true
437435

438-
[Rpc(SendTo.Everyone, RequireOwnership = true)]
436+
// RPCs with InvokePermission.Owner
437+
438+
[Rpc(SendTo.Everyone, InvokePermission = RpcInvokePermission.Owner)]
439439
public void DefaultToEveryoneRequireOwnershipRpc()
440440
{
441441
OnRpcReceived();
442442
}
443443

444-
[Rpc(SendTo.Me, RequireOwnership = true)]
444+
[Rpc(SendTo.Me, InvokePermission = RpcInvokePermission.Owner)]
445445
public void DefaultToMeRequireOwnershipRpc()
446446
{
447447
OnRpcReceived();
448448
}
449449

450-
[Rpc(SendTo.Owner, RequireOwnership = true)]
450+
[Rpc(SendTo.Owner, InvokePermission = RpcInvokePermission.Owner)]
451451
public void DefaultToOwnerRequireOwnershipRpc()
452452
{
453453
OnRpcReceived();
454454
}
455455

456-
[Rpc(SendTo.NotOwner, RequireOwnership = true)]
456+
[Rpc(SendTo.NotOwner, InvokePermission = RpcInvokePermission.Owner)]
457457
public void DefaultToNotOwnerRequireOwnershipRpc()
458458
{
459459
OnRpcReceived();
460460
}
461461

462-
[Rpc(SendTo.Server, RequireOwnership = true)]
462+
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Owner)]
463463
public void DefaultToServerRequireOwnershipRpc()
464464
{
465465
OnRpcReceived();
466466
}
467467

468-
[Rpc(SendTo.NotMe, RequireOwnership = true)]
468+
[Rpc(SendTo.NotMe, InvokePermission = RpcInvokePermission.Owner)]
469469
public void DefaultToNotMeRequireOwnershipRpc()
470470
{
471471
OnRpcReceived();
472472
}
473473

474-
[Rpc(SendTo.NotServer, RequireOwnership = true)]
474+
[Rpc(SendTo.NotServer, InvokePermission = RpcInvokePermission.Owner)]
475475
public void DefaultToNotServerRequireOwnershipRpc()
476476
{
477477
OnRpcReceived();
478478
}
479479

480-
[Rpc(SendTo.ClientsAndHost, RequireOwnership = true)]
480+
[Rpc(SendTo.ClientsAndHost, InvokePermission = RpcInvokePermission.Owner)]
481481
public void DefaultToClientsAndHostRequireOwnershipRpc()
482482
{
483483
OnRpcReceived();
484484
}
485485

486-
[Rpc(SendTo.SpecifiedInParams, RequireOwnership = true)]
486+
[Rpc(SendTo.SpecifiedInParams, InvokePermission = RpcInvokePermission.Owner)]
487487
public void SpecifiedInParamsRequireOwnershipRpc(RpcParams rpcParams)
488488
{
489489
OnRpcReceived();
490490
}
491491

492-
[Rpc(SendTo.Authority, RequireOwnership = true)]
492+
[Rpc(SendTo.Authority, InvokePermission = RpcInvokePermission.Owner)]
493493
public void DefaultToAuthorityRequireOwnershipRpc()
494494
{
495495
OnRpcReceived();
496496
}
497497

498-
[Rpc(SendTo.NotAuthority, RequireOwnership = true)]
498+
[Rpc(SendTo.NotAuthority, InvokePermission = RpcInvokePermission.Owner)]
499499
public void DefaultToNotAuthorityRequireOwnershipRpc()
500500
{
501501
OnRpcReceived();

testproject/Assets/Samples/SpawnObject/SpawnObjectHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ private void AutoSpawnObject(ulong ownerId, int selection)
6565
SetMotion(networkObject.gameObject);
6666
}
6767

68-
[ServerRpc(RequireOwnership = false)]
69-
private void AutoSpawnObjectServerRpc(int selection, ServerRpcParams serverRpcParams = default)
68+
[Rpc(SendTo.Server)]
69+
private void AutoSpawnObjectServerRpc(int selection, RpcParams serverRpcParams = default)
7070
{
7171
AutoSpawnObject(serverRpcParams.Receive.SenderClientId, selection);
7272
}
@@ -88,8 +88,8 @@ public void AutoSpawnClick()
8888
}
8989
}
9090

91-
[ServerRpc(RequireOwnership = false)]
92-
private void ManualSpawnObjectServerRpc(int selection, ServerRpcParams serverRpcParams = default)
91+
[Rpc(SendTo.Server)]
92+
private void ManualSpawnObjectServerRpc(int selection, RpcParams serverRpcParams = default)
9393
{
9494
AutoSpawnObject(serverRpcParams.Receive.SenderClientId, selection);
9595
}

0 commit comments

Comments
 (0)