Skip to content

Commit bed5b1f

Browse files
authored
Develop into main to publish NGO 2.3.0 (#1464)
2 parents b3489b0 + ba9d98f commit bed5b1f

File tree

181 files changed

+15305
-1903
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+15305
-1903
lines changed

docs/advanced-topics/networkobject-parenting.md

+31
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,37 @@ If you aren't completely familiar with transform parenting in Unity, then it's h
2525
- When `OnNetworkObjectParentChanged` is invoked, on the server side, adjust the child's transform values within the overridden method.
2626
- Netcode for GameObjects will then synchronize all clients with the child's parenting and transform changes.
2727

28+
### Parenting, distributed authority, and NetworkObject redistribution
29+
30+
Parenting within a [distributed authority](../terms-concepts/distributed-authority.md) network topology follows the same rules as outlined above, with the exception that you can change any references to the server with references to authority.
31+
32+
#### Rule adjustments
33+
34+
- Whichever client has authority over a NetworkObject can control its parenting.
35+
- You can parent under mixed authority. This means that Client-B can parent a NetworkObject it has authority over under another NetworkObject that Client-A has authority over.
36+
37+
#### Distributable permissions and child hierarchies
38+
39+
- **When a client disconnects:**
40+
- Any root parent with [distributable permission](../basics/ownership.md#ownership-permission-settings) set that was owned by the disconnected client is redistributed.
41+
- If the root parent was locked when the client disconnected, then it's unlocked prior to redistributing.
42+
- Children of the root parent change ownership with the root parent only if:
43+
- The child was owned by the client that disconnected.
44+
- The child has either the distributable or transferable permission set.
45+
- If a child's ownership permission is locked, it's unlocked prior to redistributing.
46+
- Any child of the root parent that's owned by a different client won't get redistributed.
47+
- **When a client connects:**
48+
- Any root parent with [distributable permission](../basics/ownership.md#ownership-permission-settings) set that doesn't have ownership locked is considered for redistribution.
49+
- If a root parent is redistributed to a newly connecting client then:
50+
- Any child under the root parent that has the same owner as the root parent changes ownership with the root parent.
51+
- Unless the child has ownership locked.
52+
- Any child under the root parent that has the distributable or transferrable permission set can be redistributed as well.
53+
- Unless the child has ownership locked.
54+
55+
#### Distributable permissions and in-scene placed `NetworkObject` instances:
56+
By default, any root in-scnee placed `NetworkObject` instance will have the `Distributable` permissions flag unless it already has the `SessionOwner` permission flag set. This assures in-scene placed `NetworkObject` instances will always distributed amongst clients or a newly promoted session owner.
57+
58+
2859
:::tip
2960
When a NetworkObject is parented, Netcode for GameObjects synchronizes both the parenting information along with the child's transform values. Netcode for GameObjects uses the `WorldPositionStays` setting to decide whether to synchronize the local or world space transform values of the child NetworkObject component. This means that a NetworkObject component doesn't require you to include a NetworkTransform component if it never moves around, rotates, or changes its scale when it isn't parented. This can be beneficial for world items a player might pickup (parent the item under the player) and the item in question needs to adjustment relative to the player when it's parented or the parent is removed (dropped). This helps to reduce the item's over-all bandwidth and processing resources consumption.
3061
:::

docs/advanced-topics/transports.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ A transport layer can provide:
2020

2121
## Unity Transport package
2222

23-
Netcode's default transport Unity Transport is an entire transport layer that you can use to add multiplayer and network features to your project with or without Netcode. See the Transport [documentation](../../../transport/current/about) for more information and how to [install](../../../transport/current/install).
23+
Netcode's default transport Unity Transport is an entire transport layer that you can use to add multiplayer and network features to your project with or without Netcode. Refer to the [Transport documentation](https://docs-multiplayer.unity3d.com/transport/current/about/) for more information and how to [install the Transport package](https://docs-multiplayer.unity3d.com/transport/current/install/).
2424

2525
## Unity's UNet Transport Layer API
2626

docs/basics/networkvariable.md

+29-14
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ title: NetworkVariables
55

66
`NetworkVariable`s are a way of synchronizing properties between servers and clients in a persistent manner, unlike [RPCs](../advanced-topics/message-system/rpc.md) and [custom messages](../advanced-topics/message-system/custom-messages.md), which are one-off, point-in-time communications that aren't shared with any clients not connected at the time of sending. `NetworkVariable`s are session-mode agnostic and can be used with either a [client-server](../terms-concepts/client-server.md) or [distributed authority](../terms-concepts/distributed-authority.md) network topology.
77

8-
`NetworkVariable` is a wrapper of the stored value of type `T`, so you need to use the `NetworkVariable.Value` property to access the actual value being synchronized. A `NetworkVariable` is synchronized with:
8+
`NetworkVariable` is a wrapper of the stored value of type `T`, so you need to use the `NetworkVariable.Value` property to access the actual value being synchronized. A `NetworkVariable` handles value synchronization for:
99

1010
* New clients joining the game (also referred to as late-joining clients)
11-
* When the associated NetworkObject of a `NetworkBehaviour` with `NetworkVariable` properties is spawned, any `NetworkVariable`'s current state (`Value`) is automatically synchronized on the client side.
12-
* Connected clients
13-
* When a `NetworkVariable` value changes, all connected clients that subscribed to the `NetworkVariable.OnValueChanged` event (prior to the value being changed) are notified of the change. Two parameters are passed to any `NetworkVariable.OnValueChanged` subscribed callback method:
14-
- First parameter (Previous): The previous value before the value was changed
15-
- Second parameter (Current): The newly changed `NetworkVariable.Value`
11+
* All connected clients during the game
12+
13+
When using a [distributed authority](../terms-concepts/distributed-authority.md) network topology, A `NetworkVariable` ensures value synchronization is handled during:
14+
15+
* Ownership changes of the associated NetworkObject
16+
* Object redistribution during client connection or disconnection
1617

1718
You can use `NetworkVariable` [permissions](#permissions) to control read and write access to `NetworkVariable`s. You can also create [custom `NetworkVariable`s](custom-networkvariables.md).
1819

@@ -25,13 +26,16 @@ You can use `NetworkVariable` [permissions](#permissions) to control read and wr
2526
- A `NetworkVariable`'s value can only be set:
2627
- When initializing the property (either when it's declared or within the Awake method).
2728
- While the associated NetworkObject is spawned (upon being spawned or any time while it's still spawned).
29+
- A `NetworkVariable` can't be defined with the `static` keyword. Variables defined with the static keyword have a lifetime that lasts for the entire runtime of an application, rather than the much shorter lifetime of a `GameObject`. This results in incorrect and invalid state management where `NetworkVariable`s don't behave as expected.
2830

29-
### Initializing a NetworkVariable
31+
### Initializing a NetworkVariable
3032

31-
When a client first connects, it's synchronized with the current value of the `NetworkVariable`. Typically, clients should register for `NetworkVariable.OnValueChanged` within the `OnNetworkSpawn` method. A `NetworkBehaviour`'s `Start` and `OnNetworkSpawn` methods are invoked based on the type of NetworkObject the `NetworkBehaviour` is associated with:
33+
When a NetworkObject with an associated `NetworkBehaviour` with `NetworkVariable` properties is spawned, the `NetworkVariable` is initialized and associated with its relevant `NetworkBehaviour`. When any new client connects, it's synchronized with the current value of the `NetworkVariable` before the `NetworkBehaviour.OnNetworkSpawn` is invoked.
34+
35+
`NetworkVariable`s are initialized during the associated `NetworkBehaviour` initialization. The [Start](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/MonoBehaviour.Start.html) and `OnNetworkSpawn` methods are invoked based on the type of NetworkObject the `NetworkBehaviour` is associated with:
3236

3337
- In-scene placed: Since the instantiation occurs via the scene loading mechanism(s), the `Start` method is invoked before `OnNetworkSpawn`.
34-
- Dynamically spawned: Since `OnNetworkSpawn` is invoked immediately (that is, within the same relative call-stack) after instantiation, the `Start` method is invoked after `OnNetworkSpawn`.
38+
- Dynamically spawned: Since `OnNetworkSpawn` is invoked immediately (that is, within the same relative call-stack) after instantiation, the `Start` method is invoked after `OnNetworkSpawn`.
3539

3640
Typically, these methods are invoked at least one frame after the NetworkObject and associated `NetworkBehaviour` components are instantiated. The table below lists the event order for dynamically spawned and in-scene placed objects respectively.
3741

@@ -43,8 +47,17 @@ Start | OnNetworkSpawn
4347

4448
You should only set the value of a `NetworkVariable` when first initializing it or if it's spawned. It isn't recommended to set a `NetworkVariable` when the associated NetworkObject isn't spawned.
4549

50+
### Using a NetworkVariable
51+
52+
After initialization, subscribing to the `NetworkVariable.OnValueChanged` event notifies connected clients of value changes. Clients must be subscribed to this event prior to the value being changed. Typically, clients should register for `NetworkVariable.OnValueChanged` within the `OnNetworkSpawn` method.
53+
54+
Two parameters are passed to any `NetworkVariable.OnValueChanged` subscribed callback method:
55+
56+
- First parameter (Previous): The previous value before the value was changed
57+
- Second parameter (Current): The newly changed `NetworkVariable.Value`
58+
4659
:::tip
47-
If you need to initialize other components or objects based on a `NetworkVariable`'s initial synchronized state, then you can use a common method that's invoked on the client side within the `NetworkVariable.OnValueChanged` callback (if assigned) and `NetworkBehaviour.OnNetworkSpawn` method.
60+
If you need to update other components or objects based on the state of a `NetworkVariable`, then you can use a common method that's invoked within the `NetworkBehaviour.OnNetworkSpawn` method and the `NetworkVariable.OnValueChanged` callback.
4861
:::
4962

5063
## Supported types
@@ -141,8 +154,8 @@ public class TestNetworkVariableSynchronization : NetworkBehaviour
141154
```
142155

143156
If you attach the above script to an in-scene placed NetworkObject, make a standalone build, run the standalone build as a host, and then connect to that host by entering Play Mode in the Editor, you can see (in the console output):
144-
- The client side `NetworkVariable` value is the same as the server when `NetworkBehaviour.OnNetworkSpawn` is invoked.
145-
- The client detects any changes made to the `NetworkVariable` after the in-scene placed NetworkObject is spawned.
157+
- The client side `NetworkVariable` value is the same as the server when `NetworkBehaviour.OnNetworkSpawn` is invoked.
158+
- The client detects any changes made to the `NetworkVariable` after the in-scene placed NetworkObject is spawned.
146159

147160
This works the same way with dynamically spawned NetworkObjects.
148161

@@ -152,6 +165,8 @@ The [synchronization and notification example](#synchronization-and-notification
152165

153166
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.
154167

168+
It's important to note how [RPCs](../advanced-topics/message-system/rpc.md) can be used in tandem with `NetworkVariable` state. When an `RPC` call is used in tandem with a `NetworkVariable`, the `RPC` handles instantaneous notification of state, while the `NetworkVariable` handles synchronization across clients of that state.
169+
155170
```csharp
156171
public class Door : NetworkBehaviour
157172
{
@@ -599,7 +614,7 @@ public class TestFixedString : NetworkBehaviour
599614

600615
public override void OnNetworkDespawn()
601616
{
602-
m_TextString.OnValueChanged -= OnTextStringChanged;
617+
m_TextString.OnValueChanged -= OnTextStringChanged;
603618
}
604619

605620
private void OnTextStringChanged(FixedString128Bytes previous, FixedString128Bytes current)
@@ -628,5 +643,5 @@ public class TestFixedString : NetworkBehaviour
628643

629644

630645
:::note
631-
The above example uses a pre-set list of strings to cycle through for example purposes only. If you have a predefined set of text strings as part of your actual design then you would not want to use a FixedString to handle synchronizing the changes to `m_TextString`. Instead, you would want to use a `uint` for the type `T` where the `uint` was the index of the string message to apply to `m_TextString`.
646+
The above example uses a pre-set list of strings to cycle through for example purposes only. If you have a predefined set of text strings as part of your actual design then you would not want to use a FixedString to handle synchronizing the changes to `m_TextString`. Instead, you would want to use a `uint` for the type `T` where the `uint` was the index of the string message to apply to `m_TextString`.
632647
:::

docs/installation/installation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Follow the instructions on this page to set up Netcode for GameObjects in your U
1111
Before you begin, you need the following:
1212

1313
- An active Unity account with a valid license.
14-
- A supported version of Unity. Check [Netcode for GameObjects' requirements](#netcode-installation-requirements) for the specific version details.
14+
- A supported version of Unity.
1515
- An existing Unity project. If you're new to Unity, you can refer to the [get started](../tutorials/get-started-with-ngo.md) section for guidance.
1616

1717
### Compatibility

docs/learn/bitesize/bitesize-usecases.md

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ Each scene includes a tutorial to help you locate the scripts and GameObjects it
1717

1818
The tutorials that open with each scene use the [Tutorial Framework package](https://docs.unity3d.com/Packages/[email protected]/manual/index.html). You can open each tutorial at any time from the **Tutorials** menu.
1919

20+
## The Anticipation scene
21+
22+
The Anticipation scene demonstrates the Client Anticipation feature of Netcode for GameObjects in the following use cases:
23+
- [`AnticipatedNetworkVariable`](https://docs.unity3d.com/Packages/[email protected]/api/Unity.Netcode.AnticipatedNetworkVariable-1.html):
24+
- Anticipate server actions based on player interaction to change NetworkVariables responsively.
25+
- Compensate for latency that the server causes when it changes a value.
26+
- Handle incorrect anticipation.
27+
- [`AnticipatedNetworkTransform`](https://docs.unity3d.com/Packages/[email protected]/api/Unity.Netcode.Components.AnticipatedNetworkTransform.html):
28+
- Responsive server-authoritative player movement.
29+
- Smooth player movement across clients.
30+
2031
## The NetvarVsRpc scene
2132

2233
The NetvarVsRpc scene explains why to use NetworkVariables instead of Remote Procedure Calls (RPCs) to perform state synchronization.

0 commit comments

Comments
 (0)