Skip to content

Test of remote includer tool #3542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 2.4.4-docs-fixes
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Netcode will use the `Instantiate` and `Destroy` methods in place of default spa

The following example is from the Boss Room Sample. It shows how object pooling is used to handle the different projectile objects. In that example, the class `NetworkObjectPool` is the data structure containing the pooled objects and the class `PooledPrefabInstanceHandler` is the handler implementing `INetworkPrefabInstanceHandler`.

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Infrastructure/NetworkObjectPool.cs
```
<!-- CodeExample: object-pooling-NetworkObjectPool https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Infrastructure/NetworkObjectPool.cs -->
[!code-csharp[NetworkObjectPool](../snippets/NetworkObjectPool.cs)]

Let's have a look at `NetworkObjectPool` first. `PooledPrefabsList` has a list of prefabs to handle, with an initial number of instances to spawn for each. The `RegisterPrefabInternal` method, called in `OnNetworkSpawn`, initializes the different pools for each Prefab as `ObjectPool`s inside the `m_PooledObjects` dictionary. It also instantiates the handlers for each Prefab and registers them. To use these objects, a user then needs to obtain it via the `GetNetworkObject` method before spawning it, then return the object to the pool after use with `ReturnNetworkObject` before despawning it. This only needs to be done on the server, as the `PooledPrefabInstanceHandler` will handle it on the client(s) when the network object's `Spawn` or `Despawn` method is called, via its `Instantiate` and `Destroy` methods. Inside those methods, the `PooledPrefabInstanceHandler` simply calls the pool to get the corresponding object, or to return it.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ In cases where we don't use the Player Object approach and instead manually attr

Here is an example from the Boss Room sample, showing some simple session management. The game uses the Player Object approach and a GUID to identify unique players.

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/SessionManager.cs
```
<!-- CodeExample: session-management-SessionManager https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/SessionManager.cs -->
[!code-csharp[SessionManager](../snippets/SessionManager.cs)]

This class allows Boss Room to handle player session data, represented by a struct `T` implementing the `ISessionPlayerData` interface, by providing mechanisms to initialize, get and edit that data, and to associate it to a specific player. It also handles the clearing of data that's no longer used and the reinitialization of data between sessions.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ Netcode for GameObjects provides a way to customize the [connection approval pro

Boss Room provides one example of how to handle limiting the number of players through the connection approval process:

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs

```
<!-- CodeExample: maxnumberplayers-HostingState https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs -->
[!code-csharp[HostingState](../snippets/HostingState.cs)]

The code below shows an example of an over-capacity check that would prevent more than a certain pre-defined number of players from connecting.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,42 @@ See the [RPC vs NetworkVariable](rpcvnetvar.md) tutorial for more information.

Boss Room uses RPCs to send movement inputs.

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs
```
<!-- CodeExample: rpcnetvarexamples-ClientInputSender https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs -->
[!code-csharp[ClientInputSender](../snippets/ClientInputSender.cs)]

Boss Room wants the full history of inputs sent, not just the latest value. There is no need for `NetworkVariable`s, you just want to blast your inputs to the server. Since Boss Room isn't a twitch shooter, it sends inputs as reliable `RPC`s without worrying about the latency an input loss would add.

## Arrow's GameObject vs Fireball's VFX

The archer's arrows use a standalone `GameObject` that's replicated over time. Since this object's movements are slow, the Boss Room development team decided to use state (via the `NetworkTransform`) to replicate the ability's status (in case a client connected while the arrow was flying).

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Projectiles/PhysicsProjectile.cs
```
<!-- CodeExample: rpcnetvarexamples-PhysicsProjectile https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Projectiles/PhysicsProjectile.cs -->
[!code-csharp[PhysicsProjectile](../snippets/PhysicsProjectile.cs)]

Boss Room might have used an `RPC` instead (for the Mage's projectile attack). Since the Mage's projectile fires quickly, the player experience isn't affected by the few milliseconds where a newly connected client might miss the projectile. In fact, it helps Boss Room save on bandwidth when managing a replicated object. Instead, Boss Room sends a single RPC to trigger the FX client side.

```csharp reference

https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Projectiles/FXProjectile.cs

```
<!-- CodeExample: rpcnetvarexamples-FXProjectile https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Projectiles/FXProjectile.cs -->
[!code-csharp[FXProjectile](../snippets/FXProjectile.cs)]

## Breakable state

Boss Room might have used a "break" `RPC` to set a breakable object as broken and play the appropriate visual effects. Applying the "replicate information when a player joins the game mid-game" rule of thumb, the Boss Room development team used `NetworkVariable`s instead. Boss Room uses the `OnValueChanged` callback on those values to play the visual effects (and an initial check when spawning the `NetworkBehaviour`).

```csharp reference

https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs#L59-L78

```
<!-- CodeExample: rpcnetvarexamples-Breakable-L59-L78 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs -->
[!code-csharp[Breakable](../snippets/Breakable.cs#L59-L78)]

The visual changes:

```csharp reference

https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs#L146-L156

```
<!-- CodeExample: rpcnetvarexamples-Breakable-L146-L156 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Breakable.cs -->
[!code-csharp[Breakable](../snippets/Breakable.cs#L146-L156)]

**Error when connecting after imps have died**: The following is a small gotcha the Boss Room development team encountered while developing Boss Room. Using `NetworkVariable`s isn't magical. If you use `OnValueChanged`, you still need to make sure you initialize your values when spawning for the first time. `OnValueChanged` isn't called when connecting for the first time, only for the next value changes.

![imp not appearing dead](../images/01_imp_not_appearing_dead.png)


```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerAnimationHandler.cs#L23-L30
```
<!-- CodeExample: rpcnetvarexamples-ServerAnimationHandler-L23-L30 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerAnimationHandler.cs -->
[!code-csharp[ServerAnimationHandler](../snippets/ServerAnimationHandler.cs#L23-L30)]

## Hit points

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ If we sent an RPC to all clients, then all players connecting mid-game after tha

In that case, it's preferable to use `NetworkVariable`s as show below:

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/SwitchedDoor.cs#L10-L26
```
<!-- CodeExample: rpcvnetvar-SwitchedDoor-L10-L26 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/GameplayObjects/SwitchedDoor.cs -->
[!code-csharp[SwitchedDoor](../snippets/SwitchedDoor.cs#L10-L26)]

It uses a `BoolNetworkVariable` to represent the `IsOpen` state. If one player opens the door and a second player connects after this, the host replicates all the world's information to that new player, including the door's state.

Expand All @@ -44,21 +43,18 @@ Actions in Boss Room are a great example for this. The area of effect action (`A

`AoeActionInput.cs` Shows the input being updated client side and not waiting for the server. It then calls an RPC when clicking on the area to affect.

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/Input/AoeActionInput.cs
```
<!-- CodeExample: rpcvnetvar-AoeActionInput https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/Input/AoeActionInput.cs -->
[!code-csharp[AoeActionInput](../snippets/AoeActionInput.cs)]

`AOEAction.cs` has server-side logic detecting enemies inside the area and applying damage. It then broadcasts an RPC to tell all clients to play the VFX at the appropriate position. Character's state will automatically update with their respective `NetworkVariable`s update (health and alive status for example).

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/ConcreteActions/AOEAction.cs#L8-L-40
```
<!-- CodeExample: rpcvnetvar-AOEAction-L8-L-40 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/ConcreteActions/AOEAction.cs -->
[!code-csharp[AOEAction](../snippets/AOEAction.cs#L8-L-40)]

The following snippet of code is triggered by an RPC coming from the server

```csharp reference
https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/ConcreteActions/AOEAction.cs#L77-L82
```
<!-- CodeExample: rpcvnetvar-AOEAction-L77-L82 https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/v2.2.0/Assets/Scripts/Gameplay/Action/ConcreteActions/AOEAction.cs -->
[!code-csharp[AOEAction](../snippets/AOEAction.cs#L77-L82)]

If you want to make sure two variables are received at the same time, RPCs are great for this.

Expand Down
Loading