Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.1k stars 430 forks source link

Add callback Action<> to SpawnManager.InstantiateAndSpawn() between Instantiate() and SpawnWithOwnership() #2832

Open CodeSmile-0000011110110111 opened 4 months ago

CodeSmile-0000011110110111 commented 4 months ago

Is your feature request related to a problem? Please describe.

When using the convenient helper InstantiateAndSpawn there is no way to initialize components of the object before OnNetworkSpawn runs.

Specifically, this does not allow to set the initial value of a NetworkVariable so that the clients can read the intended value in OnNetworkSpawn rather than having to wait until a OnValueChanged event occurs.

But it is also relevant for initializing non-synchronized properties that the server may need to use during OnNetworkSpawn events.

For these cases one currently cannot use InstantiateAndSpawn at all, which limits its usefulness quite a bit.

Describe the solution you'd like

A callback Action (or Action) should be an optional parameter that allows one to do this:

´InstantiateAndSpawn(obj, ... , (instance) => { / use instance to initialize components before spawn / });´

The callback Action<> is invoked after Instantiate() and right before SpawnWithOwnership().

Describe alternatives you've considered There's no alternative besides changing from the helper method back to manual Instantiate, assigning transform values, initializing components, and then calling SpawnWithOwnership. Essentially unwrapping the helper method.

See also my forum post here with code example changing from InstantiateAndSpawn to doing it all manually again.