question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

While Reading data PrefabHash comes back as 0

See original GitHub issue

Hello,

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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TwoTenPvPcommented, Feb 3, 2020

You were correct, that fixed the issue. What still has me confused and might be an error with vscode is while debugging I did see the PrefabHash set to 17333001610763986986 and watched the client return 0. I’m probably still missing something about how MLAPI works internally.

I appreciate the help, I’ll close the issue now. Thanks!

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.

1reaction
TwoTenPvPcommented, Feb 3, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

While Reading data PrefabHash comes back as 0 #310
It looks like it simply calculates the hash based on the GameObjects name unless you override it. Here is my current code. Creating...
Read more >
"[MLAPI] Var data read too far. 1 bytes." When spawning ...
When joining a server, the client will spawn the player prefab from other players, but will not spawn it's own prefab. I get...
Read more >
Read buffer is zero'd out when return variable is initialised
I'm such a moron: I just read the FUSE documentation and the implementation of read is supposed to return the number of bytes...
Read more >
Sorter - Unofficial Stationeers Wiki
When the Mode is set to 0 or if there is no WhiteList configured on a computer, the Sorter will act as a...
Read more >
Kit (Logic Switch) - Unofficial Stationeers Wiki
Returns 0 if the button's light is off and 50 if the light is on. Setting, Boolean, Same as Activate but Read Only....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found