Questions about VRCSDKBaseNetworking and synchronization

Since I’m new to vrchat (Hello all :wave:) and don’t have much experience with unity, I’m a bit surprised about VRCSDKBaseNetworking. I am open for help and general tips.

  • The mob(capsule) should follow the player that triggers the collider, but this currently only works with player0(room master) .
  • The variable “hasTarget” is currently not synchronized.
  • Player1 only gets the distance log.

THH_PlayerManager (https://github.com/TheHelpfulHelper/UDON-Collection/tree/master/THH_PlayerManager) is used to assign an object to each player.


Questions:

  1. Do I have to work with CustomNetworkEvent to synchronize data?
  2. There is no real way to test networking if you have not reached the User Trust rank, is there? Only found GotoUdon (https://github.com/GotoFinal/GotoUdon) which does not work with the current version.
  3. Networking.LocalPlayer.playerId is not unique. is there any other possibility than the name?
  4. Is OnTriggerEnter called via the network or only locally for the user?

using UdonSharp;
using UnityEngine;
using UnityEngine.AI;
using VRC.SDKBase;


public class MobAi : UdonSharpBehaviour
{
    private NavMeshAgent _navMeshAgent;
    private VRCPlayerApi _localPlayer;
    private float _timer = 0F;
    private THH_PlayerObjectHandler _playerObjectHandler = null;

    [HideInInspector]
    [UdonSynced] public bool hasTarget = false;

    public float updateTime = 1F;
    public float maxDistance = 8F;
    public THH_PlayerManager manager = null;

    private void Start()
    {
        _localPlayer = Networking.LocalPlayer;
        _navMeshAgent = GetComponent<NavMeshAgent>();

        if (!manager)
        {
            Debug.LogError("The manager must be set");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (!hasTarget)
        {
            var otherAsString = ((object) other).ToString();
            if (otherAsString.Contains("VRCPlayer") &&
                _localPlayer.playerId == int.Parse(otherAsString.Split(' ')[2]))
            {
                hasTarget = true;
                UpdatePlayerPosition();
                _playerObjectHandler = manager.GetPlayerObjectHandler(_localPlayer);

                if (!_playerObjectHandler)
                {
                    ResetMob();
                    Debug.LogError("Player handler cannot be set");
                    return;
                }

                Debug.Log("Set target to: " + _playerObjectHandler.name);
            }
        }
    }

    private void UpdatePlayerPosition()
    {
        _navMeshAgent.SetDestination(_localPlayer.GetPosition());
    }

    private void ResetMob()
    {
        hasTarget = false;
        _playerObjectHandler = null;
        _navMeshAgent.ResetPath();
    }

    private void Update()
    {
        if (!hasTarget || !_playerObjectHandler)
        {
            return;
        }

        _timer += Time.deltaTime;
        if ((_timer < updateTime)) return;
        _timer = 0F;


        float dist = Vector3.Distance(this.transform.position, _localPlayer.GetPosition());
        Debug.Log("Distance to user:" + dist);

        if (dist < maxDistance)
        {
            UpdatePlayerPosition();
        }
        else
        {
            ResetMob();
        }
    }
}

Answers;

  1. Currently we have to use SendCustomNetworkEvent and synced variables. The new update should have SendCustomNetworkEvent with parameters, so it will be easier to sync one time data then.

  2. We can not test VRChat networking without being in the client. So it will require you to either do a test build (limited networking) or upload it (secondary account may be required to fully test networking)

  3. LocalPlayer.PlayerId is the instance ID, so it’s unique per instance. As for a unique identifier, we got displayNames, which is the closet we get to something like that. There is however a canny for exposing the users unique id: https://feedback.vrchat.com/vrchat-udon-closed-alpha-feedback/p/expose-unique-user-ids-to-udon

  4. Any Unity event is local only. However, players you see are also part of the local client. So if you have it on a layer that can detect other players, it will trigger. (But that doesn’t make it networked, you got to add that in the code)

Okay, thanks. Do you know when the new feature will be released?

Soon™ is all I can give you. It’s supposed to be in the next Udon update, so will have to wait and see.

1 Like

Ok then I wait for the next update and test what else is possible. :+1: