Skip to content

Instantly share code, notes, and snippets.

@SteffenBlake
Created May 30, 2021 03:42
Show Gist options
  • Save SteffenBlake/6e80edd11fdd7190fca08d67a0809222 to your computer and use it in GitHub Desktop.
Save SteffenBlake/6e80edd11fdd7190fca08d67a0809222 to your computer and use it in GitHub Desktop.
private int[] PlayerBroadcasters = new int[0];
[SerializeField] private Toggle BroadcastToggle;
public void OnBroadcastToggle()
{
BroadcasterId = Networking.LocalPlayer.playerId;
Broadcast = BroadcastToggle.isOn;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
RequestSerialization();
}
// Synched Events
private int BroadcasterId = 0;
[UdonSynced] private int SynchedBroadcasterId = 0;
private bool Broadcast = false;
[UdonSynced] private bool SynchedBroadcast = false;
public override void OnPreSerialization()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
SynchedBroadcasterId = BroadcasterId;
SynchedBroadcast = Broadcast;
}
public override void OnDeserialization()
{
if ((BroadcasterId != SynchedBroadcasterId) || (Broadcast != SynchedBroadcast))
OnBroadcasterSynch();
}
private void OnBroadcasterSynch()
{
BroadcasterId = SynchedBroadcasterId;
Broadcast = SynchedBroadcast;
var players = new VRCPlayerApi[VRCPlayerApi.GetPlayerCount()];
players = VRCPlayerApi.GetPlayers(players);
foreach (var player in players)
{
if (player.playerId != BroadcasterId)
return;
if (Broadcast)
{
player.SetVoiceDistanceFar(10000);
player.SetVoiceDistanceNear(10000);
player.SetVoiceGain(0);
PlayerBroadcasters = AddIfNotContains(PlayerBroadcasters, BroadcasterId);
}
else
{
player.SetVoiceDistanceFar(VoiceCarrySlider.value);
player.SetVoiceDistanceNear(0);
player.SetVoiceGain(15);
PlayerBroadcasters = RemoveAll(PlayerBroadcasters, BroadcasterId);
}
}
}
private int[] AddIfNotContains(int[] inArray, int value)
{
var outArray = new int[inArray.Length+1];
for (var n = 0; n < inArray.Length; n++)
{
if (inArray[n] == value)
return inArray;
outArray[n] = inArray[n];
}
outArray[outArray.Length] = value;
return outArray;
}
private int[] RemoveAll(int[] inArray, int value)
{
var hitCount = 0;
for (var n = 0; n < inArray.Length; n++)
{
if (inArray[n] == value)
hitCount++;
}
var offset = 0;
var outArray = new int[inArray.Length - hitCount];
for (var n = 0; n < inArray.Length; n++)
{
if (inArray[n] == value)
{
offset++;
continue;
}
outArray[n - offset] = inArray[n];
}
return outArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment