Open fernando-cortez opened 1 year ago
@fernando-cortez With in-scene placed NetworkObjects, you can use the awake method to accomplish this type of functionality.
using System;
using Unity.Netcode;
using UnityEngine;
public class ServerIngredient : ServerObjectWithIngredientType
{
}
public enum IngredientType
{
Red,
Blue,
Purple,
MAX // should be always last
}
public class ServerObjectWithIngredientType : NetworkBehaviour
{
// NOTE: this is a workaround on an issue NGO-side. NetworkVariables should be able to be set pre-spawn.
[SerializeField]
private IngredientType m_StartingIngredientType;
private NetworkVariable<IngredientType> m_CurrentIngredientType;
public event Action IngredientDespawned;
private void Awake()
{
m_CurrentIngredientType = new NetworkVariable<IngredientType>(m_StartingIngredientType);
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}
Debug.Log($"Current Ingredient Type: {m_CurrentIngredientType.Value}");
}
public override void OnNetworkDespawn()
{
IngredientDespawned?.Invoke();
}
}
Replace the ClientDriven bite sample's ServerIngredient file with the above adjustments. It just instantiates the NetworkVariable in Awake based on the m_StartingIngredientType property's value.
The NetworkVariable documentation provides an order of operations table (dynamically spawned vs in-scene placed). For both dynamically spawned and in-scene placed NetworkObjects, you can instantiate a NetworkVariable
within the Awake method and assign the initial value based on a serialized property's value.
Side note: Since this type of functionality can be achieved in NGO, I am going to mark this as a feature request. I do think we can make an adjustment that would allow you to accomplish this without having to instantiate the NetworkVariable within the Awake method.
Description
One cannot modify NetworkVariables on in-scene placed NetworkObjects. When one does, you get the following warning message:
and the NetworkVariable is reset to the default value.
Reproduce Steps
Actual Outcome
NetworkVariables on in-scene placed NetworkObject was reset.
Expected Outcome
NetworkVariables on in-scene placed NetworkObject should not be reset and should be saved in the scene.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
Additional Context
Add any other context about the problem here. Logs, code snippets would be useful here but please also consider attaching a minimal Unity project that reproduces the issue.