Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Spawned object and Sync var hook possible race condition. #3874

Closed
JesusLuvsYooh opened this issue Jul 26, 2024 · 1 comment
Closed
Labels
duplicate This issue or pull request already exists

Comments

@JesusLuvsYooh
Copy link
Contributor

JesusLuvsYooh commented Jul 26, 2024

Describe the bug
A network spawned object as sync var, triggers a hook call when changed, but is not found or ready to use in hook code.
Start player host, spawn network object, have a sync var hook set this sync var object in player code.
Join via a second player, you can see the hooks called, but it is not usable in hooks code.

  • This only happens if object is network spawned (not set in scene), and with a late joining player. -
[SyncVar(hook = nameof(OnPickupChangedHook))]
public NetworkIdentity pickedUpNetworkObject;

void OnPickupChangedHook(NetworkIdentity _old, NetworkIdentity _new)
{
  print("OnPickupChangedHook 1");

  if (pickedUpNetworkObject)
  {
    print("OnPickupChangedHook 2");
    PickupResult();
  }
  else
  {
  print("OnPickupChangedHook 3");
    DropResult();
  }
}

print("OnPickupChangedHook 1"); is called, but print("OnPickupChangedHook 2"); is not.

Adding a 1 frame delay works, example:

 [SyncVar(hook = nameof(OnPickupChangedHook))]
        public NetworkIdentity pickedUpNetworkObject;

        void OnPickupChangedHook(NetworkIdentity _old, NetworkIdentity _new)
        {
            StartCoroutine(WaitForResult());
        }

        private IEnumerator WaitForResult()
        {
            // we use a delay, as network spawned objects may not be spawned in and ready, hooks get called very early
            yield return new WaitForEndOfFrame();
            print("OnPickupChangedHook 1" + pickedUpNetworkObject);
            if (pickedUpNetworkObject)
            {
                print("OnPickupChangedHook 2" + pickedUpNetworkObject);
                PickupResult();
            }
            else
            {
                print("OnPickupChangedHook 3");
                DropResult();
            }
        }
@MrGadget1024
Copy link
Collaborator

Duplicate of #3097

@MrGadget1024 MrGadget1024 marked this as a duplicate of #3097 Jul 26, 2024
@MrGadget1024 MrGadget1024 added the duplicate This issue or pull request already exists label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants