Skip to content

Instantly share code, notes, and snippets.

@foopis23
Created February 28, 2022 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foopis23/2d05c1d779031071fac700433a73dcf8 to your computer and use it in GitHub Desktop.
Save foopis23/2d05c1d779031071fac700433a73dcf8 to your computer and use it in GitHub Desktop.
VRChat Send Events to Specific Players
public class ExampleUsage : UdonSharpBehaviour {
public void OnGameFinished() {
// determine the winner somehow up here
VRCSDKBaseVRCPlayerApi wiwnner = ...
// find the winner and run the teleport command on them
for (var i = 0; i < networkManager.playerIds.Length; i++)
{
if (networkManager.playerIds[i] == winner.playerId) {
networkManager.clients[i].SendCustomNetworkEvent(NetworkEventTarget.Owner, "TeleportPlayerToSpawn");
}
}
}
}
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
public class NetworkClient : UdonSharpBehaviour
{
public Transform spawnPoint;
// event to run for specific player
public void TeleportPlayerToSpawn()
{
Networking.GetOwner(gameObject).TeleportTo(spawnPoint.position, spawnPoint.rotation);
}
}
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
public class NetworkManager : UdonSharpBehaviour
{
public int[] playerIds;
[SerializeField] public NetworkClient[] clients;
void Start()
{
playerIds = new int[16];
for (var i = 0; i < playerIds.Length; i++)
{
playerIds[i] = -1;
}
}
public override void OnPlayerJoined(VRCPlayerApi player)
{
if (!Networking.IsOwner(gameObject)) return;
for (var i = 0; i < playerIds.Length; i++)
{
if (playerIds[i] != -1) continue;
clients[i].gameObject.SetActive(true);
playerIds[i] = player.playerId;
Networking.SetOwner(player, clients[i].gameObject);
break;
}
}
public override void OnPlayerLeft(VRCPlayerApi player)
{
if (!Networking.IsOwner(gameObject)) return;
for (var i = 0; i < playerIds.Length; i++)
{
if (playerIds[i] != player.playerId) continue;
clients[i].gameObject.SetActive(false);
playerIds[i] = -1;
break;
}
}
}
@foopis23
Copy link
Author

Potential Improvements:

  • Make it easier to call a command on a specific player (I don't want to have to for loop in my main code every time)
  • Make the command calling work in 2 ways. Right now it only works when sending events to players from the NetworkManager Owner (most likely the instance owner)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment