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.

NetworkShow and the CheckObjectVisibility Delegate

See original GitHub issue

Description

i think to fix several other issues in the NetworkList, a major feature was overlooked and removed. Currently if a “CheckObjectVisibility” delegate is set on a network object and returns false. Then a manual call with “NetworkShow” is made on the network object. No matter what it will never spawn on the client. I think this should be regressed. The developer should have full control over the objects visibility. This change removes developer choice.

Currently, all objects are set to hidden in my project. I would assume that would be the case for a good majority of devs. Or otherwise a lot of bandwidth will undoubtedly be used. If I ever wanted to show an objects visibility forcibly, I cannot anymore. Unless I create another set of workarounds. The only two options are lambda functions, which can be designed with more local data in the method. Or using a delegate, which only the ID of the client is usable…

Before NGO 1.4 release, this was not an issue. If possible can we design NGO to add a parameter to force the “NetworkShow” call and ignore the “CheckObjectVisibility”…

Reproduce Steps

  1. Assign “CheckObjectVisibility” delegate, return false.
  2. Spawn the network object.
  3. Show visibility of network object with “NetworkShow.”

Actual Outcome

A warning appears with method execution also ending before spawn implementation.

Expected Outcome

Visibility of network object is shown to client. Due to server having a call on the network object to show visibility.

Environment

Additional Context

Current “NetworkObject” class, starting line number 339

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/27176860f9f25eb089d807ae61413ed44b1bdd1a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs#L339


public void NetworkShow(ulong clientId)
        {
            if (!IsSpawned)
            {
                throw new SpawnStateException("Object is not spawned");
            }

            if (!NetworkManager.IsServer)
            {
                throw new NotServerException("Only server can change visibility");
            }

            if (Observers.Contains(clientId))
            {
                throw new VisibilityChangeException("The object is already visible");
            }

            if (CheckObjectVisibility != null && !CheckObjectVisibility(clientId))
            {
                if (NetworkManager.LogLevel <= LogLevel.Normal)
                {
                    NetworkLog.LogWarning($"[NetworkShow] Trying to make {nameof(NetworkObject)} {gameObject.name} visible to client ({clientId}) but {nameof(CheckObjectVisibility)} returned false!");
                }
                return;
            }
            NetworkManager.SpawnManager.MarkObjectForShowingTo(this, clientId);
            Observers.Add(clientId);
        }

A possible fix for this would be…


public void NetworkShow(ulong clientId, bool isForced = false)
        {
            if (!IsSpawned)
            {
                throw new SpawnStateException("Object is not spawned");
            }

            if (!NetworkManager.IsServer)
            {
                throw new NotServerException("Only server can change visibility");
            }

            if (Observers.Contains(clientId))
            {
                throw new VisibilityChangeException("The object is already visible");
            }

            if (!isForced)
            {
                if (CheckObjectVisibility != null && !CheckObjectVisibility(clientId))
                {
                    if (NetworkManager.LogLevel <= LogLevel.Normal)
                    {
                        NetworkLog.LogWarning($"[NetworkShow] Trying to make {nameof(NetworkObject)} {gameObject.name} visible to client ({clientId}) but {nameof(CheckObjectVisibility)} returned false!");
                    }
                    return;
                }
            }

            NetworkManager.SpawnManager.MarkObjectForShowingTo(this, clientId);
            Observers.Add(clientId);
        }

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Reactions:1
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
NoelStephensUnitycommented, May 23, 2023

@CosmicStud @ezoray In the next update you will have a NetworkObject.SpawnWithObservers property (default is true) that you can set to false and it will spawn the NetworkObject with no observers. The only change you will have to make on your end is to remove the CheckObjectVisibility handler and just set NetworkObject.SpawnWithObservers to false. 👍

1reaction
ezoraycommented, May 23, 2023

@NoelStephensUnity that addition is much appreciated thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NetworkObject CheckObjectVisibility delegate not invoked ...
When spawning objects on the server and supplying a value for the CheckObjectVisibility delegate, this delegate is not being invoked when ...
Read more >
Class NetworkObject | Netcode for GameObjects | 1.0.2
CheckObjectVisibility. Delegate invoked when the netcode needs to know if the object should be visible to a client, if null it will assume ......
Read more >
Class NetworkObject | Netcode for GameObjects | 1.5.1
CheckObjectVisibility. Delegate invoked when the netcode needs to know if the object should be visible to a client, if null it will assume ......
Read more >
MLAPI Versions
Made sure the CheckObjectVisibility delegate is checked and applied, upon NetworkShow attempt. · Changed NetworkTransform authority handles delta checks on each ...
Read more >
ezoray - github record :)
NetworkShow and the CheckObjectVisibility Delegate. That makes sense from my perspective as currently I set CheckObjectVisibility to always return false as ...
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