#A new player connecting would initialize
1 messages · Page 1 of 1 (latest)
That nullRef (image) is the initial issue, where it doesnt have m_NetworkManager set. The writing attempt (Warning) is in Awake when I now do cs PlayerData pd = new PlayerData(); pd.ints = new NetworkList<int>(); PlayerData.Value = pd;
The ERROR later appears in OnNetworkSpawn when I do:cs if (IsOwner) { PlayerData pd = PlayerData.Value; pd.ints.Add(Random.Range(0, 100)); // The Error PlayerData.Value = pd; }
post the full code here
The Behaviour:
public class PlayerManager : NetworkBehaviour
{
public NetworkVariable<PlayerData> PlayerData = new NetworkVariable<PlayerData>(new PlayerData(), NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
private void Awake()
{
PlayerData pd = new PlayerData();
pd.ints = new NetworkList<int>();
PlayerData.Value = pd;
}
public override void OnNetworkSpawn()
{
if (IsOwner)
{
PlayerData pd = PlayerData.Value;
pd.ints.Add(Random.Range(0, 100)); // The Error
PlayerData.Value = pd;
}
if (NetworkManager.Singleton.IsServer)
Instantiate(Resources.Load<GameObject>("Prefabs/Boat")).GetComponent<NetworkObject>().Spawn();
base.OnNetworkSpawn();
}
private void Update()
{
print(OwnerClientId + " " + PlayerData.Value.ints?.Count);
if (!IsOwner) return;
if (Input.GetKeyDown(KeyCode.T))
{
PlayerData pd = PlayerData.Value;
pd.ints.Add(Random.Range(0, 100));
PlayerData.Value = pd;
}
}
}```
The data:
```cs
public struct PlayerData : INetworkSerializable
{
public NetworkList<int> ints;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
//Serializing other fields that are not collections.
}
}```
I dont know what else there is that I could give you that is relevant at all
PlayerManager is probably a bad name, its basically the behaviour on the player prefab spawned by the NetworkManager
Same thing as I said about the struct before - why do you have a NetworkList inside of something that is already networked?
Because the NativeList doesnt work
You should also be able to use a int[] inside a INetworkSerializable
as posted before
If PlayerData is already a NetworkVariable, just use a normal collection.
If you wish to use a NetworkList, pull it outside of a NetworkVariable.
As I said before, that is always null
You are doing something wrong there. its definitely doable
Obviously Im doing something wrong if I didnt I wouldnt ask for help QwQ
Again, just my two cents. If I'm implementing this, PlayerData is probably going to be called BasicPlayerData and is going to hold simple data like name, level, health, etc. and then I'm just going to have a NetworkList on its own inside PlayerManager that keeps track of items.
I've made several multiplayer games with NGO and other libraries like it, I think I've used a networked collection maybe one time. I definitely have never had a nested collection in a networked value.
Serializing values/collections for networking can be fragile and easy to overcomplicate. Always best to keep it as simple as possible, IMO.
Okay I managed to get another error when using NativeList, my first issue here was the scripting defines not importing before I restarted unity (great) so now theyre there and I can use it to at least get compiled, so Now I use that like so:
public struct PlayerData : INetworkSerializable
{
public NativeList<int> ints;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref ints);
}
}```
The error is because ints of the client is null when NetworkSerialize gets executed, it only errors on the server (Testing with host so maybe other clients)
I could intialize it to not be null in Awake again, just like with the NetworkList before, it gives me a warning instead, saying it doesnt know its NetworkBehaviour yet, I would like to avoid that if at all possible, any ideas?
why
Yeah, it doesnt like it when I assign it in Awake (it doesnt sync values between players), and OnNetworkSpawn seems to happen after NetworkSerialize for new clients
This is working in a fresh project for me
I did have to add UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT in the Scripting Define Symbols
Did you make sure to try it with multiple clients? Because server/host only no issues appear, they only start once you connect
The key difference I can spot to mine is you dont check if(IsOwner) in OnNetworkSpawn, is that on purpose?
Anyways, even removing that and basically having a direct copy of your code, the client's PlayerData is still null
no its server auth by default. I'll try it with owner write perms in a minute
I mean, the server is the one having null
When I put the NetworkWritePermission to server (So default like yours) THEN it errors on the client, but thats obvious since it tries to write and has no rights. What wonders me is that it is still null on the client, even tho, just like you, I removed the if(IsOwner) so the server SHOULD write on it and it shouldnt be null