While Reading data PrefabHash comes back as 0
See original GitHub issueHello,
For my project I am not using any prefabs but instead I dynamically create everything at runtime. Below is some example code to give an idea what I’m doing in my unity project.
// starting a host
networkingManager.NetworkConfig = CreateConfigWithCode() // sets transport and settings
networkingManager.StartHost();
// Lets pretend GetHash() returns the same hash as the string "String to be hashed..."
MLAPI.Spawning.SpawnManager.RegisterSpawnHandler (GetHash (), (position, rotation) => {
var go = new GameObject("MyCustomSpawnedObject");
go.AddComponent<MyScript>();
var netObj = go.AddComponent<MLAPI.NetworkedObject>();
netObj.PrefabHashGenerator = "String to be hashed...";
});
The issue
On the Host side of things (Getting ready to tell the client to spawn stuff), when it eventually calls writer.WriteUInt64Packed(netObject.PrefabHash)
the hashcode is set to 17333001610763986986
. I also ensured that MLAPI.Spawning.SpawnManager.customSpawnHandler
has an entry for this hashcode for both client and host. So far this seems to be working correctly.
On the Client side of things, it will eventaully call prefabHash = continuationReader.ReadUInt64Packed()
prefabHash is 0 after this call. This is where I believe the issue is happening.
Other Notes
This is how I handle spawning player objects on the server side when a client connects.
protected void OnClientConnected (ulong clientId)
{
Debug.Log ("Client Connected!!!!" + clientId);
if (networkingManager.IsServer)
{
// Server creates the entity, Note: CreateEntity just creates a gameObject with a player
//script and a NetworkedObject and returns a MonoBehaviour called Entity.
var entity = spawnManager.CreateEntity<Shared.Spawning.PlayerSpawner> (Vector3.zero, Quaternion.identity);
// Server tells the client to create the same entity.
entity.NetworkedObject.SpawnAsPlayerObject (clientId);
}
}
My project at the moment is small and only tries to spawn a player object. I’m not sure if this is a bug with the BitReader not reading the header correctly or if this is a simple setup error on my part.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
What happened probably was that you managed to get OnValidate to run. The PrefabHash updates only when OnValidate runs. Thats a Unity method. So the MLAPI spawned the object. Afterwards, you tried to inspect the PrefabHash which caused Unity to run OnValidate and actually set the PrefabHash. But at that point, the value 0 had already been sent.
By looking at the code. It does make total sense for PrefabHash to be written as 0 though. In your spawn code, you call ExampleSpawnPlayer(). That instantiates the prefab. At that point, PrefabHashGenerator will be empty (“”) and the PrefabHash will be 0. Then, you set the PrefabHashGenerator. At this point the PrefabHashGenerator will be “player” and the PrefabHash will still be 0. Then you spawn it. Causing the MLAPI to write 0 to the wire. It makes total sense.
I suggest you just set the PrefabHash instead of the Generator.