Skip to content

Commit

Permalink
Cached LocalPlayer
Browse files Browse the repository at this point in the history
• Cached the Networking.LocalPlayer.
• Cleaned up code which validates the local player and uses it.
  • Loading branch information
Nestorboy committed Aug 13, 2023
1 parent 009dfad commit cc64390
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Packages/com.llealloo.audiolink/Runtime/Scripts/AudioLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public class AudioLink : MonoBehaviour
private double _networkTimeMSAccumulatedError;
#if UDONSHARP
private bool _hasInitializedTime = false;
private VRCPlayerApi _localPlayer;
#endif
private double _fpsTime = 0;
private int _fpsCount = 0;
Expand Down Expand Up @@ -305,12 +306,10 @@ void Start()
_rightChannelTestCounter = _rightChannelTestDelay;

// Set localplayer name on start
if (Networking.LocalPlayer != null)
_localPlayer = Networking.LocalPlayer;
if (VRC.SDKBase.Utilities.IsValid(_localPlayer))
{
if (VRC.SDKBase.Utilities.IsValid(Networking.LocalPlayer))
{
UpdateGlobalString(_StringLocalPlayer, Networking.LocalPlayer.displayName);
}
UpdateGlobalString(_StringLocalPlayer, _localPlayer.displayName);
}

// Set master name once on start
Expand Down Expand Up @@ -424,7 +423,7 @@ private void FPSUpdate()
#if UNITY_EDITOR
0.0f,
#else
Networking.LocalPlayer != null && Networking.LocalPlayer.isInstanceOwner?1.0f:0.0f,
_localPlayer.isInstanceOwner ? 1.0f : 0.0f,
#endif
0));

Expand Down Expand Up @@ -587,9 +586,9 @@ private void Update()


#if UDONSHARP
if (Networking.LocalPlayer != null)
if (VRC.SDKBase.Utilities.IsValid(_localPlayer))
{
float distanceToSource = Vector3.Distance(Networking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Head).position, audioSource.transform.position);
float distanceToSource = Vector3.Distance(_localPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Head).position, audioSource.transform.position);
audioMaterial.SetFloat(_SourceDistance, distanceToSource);
}
#endif
Expand Down Expand Up @@ -729,7 +728,7 @@ public void UpdateCustomStrings()
{
#if UDONSHARP
if (!Networking.IsOwner(gameObject))
Networking.SetOwner(Networking.LocalPlayer, gameObject);
Networking.SetOwner(_localPlayer, gameObject);
#endif

UpdateGlobalString(_StringCustom1, customString1);
Expand Down

0 comments on commit cc64390

Please sign in to comment.