Skip to content

Commit

Permalink
Make the notion of an EventHandler abstract.
Browse files Browse the repository at this point in the history
HandleEvent previously required you to modify
the event receiving code. Now, the event receiver
will just pass new events to an abstract UdonBehaviour with the correct var names and
event name.
  • Loading branch information
FairlySadPanda committed Apr 25, 2020
1 parent 1eff887 commit 88a0212
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
42 changes: 42 additions & 0 deletions ChatEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Example Handler
using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common.Interfaces;

/// <Summary>An example handler for events.</Summary>
public class ChatEventHandler : UdonSharpBehaviour
{
public UdonLogger logger;

private string characterName;
private string newEvent;

public void Handle()
{
bool ownerOfGame = true;
if (Networking.LocalPlayer != null)
{
ownerOfGame = Networking.LocalPlayer.IsOwner(gameObject);
}

// As it stands, hard-code your events in this function.
// This is pretty basic. Once maps and lists exist in Udon, this can be improved.
string[] e = newEvent.Split(',');
Debug.Log("Got an event named " + e[0] + " with payload " + newEvent);
switch (e[0])
{
case "ChatMessage":
string message = characterName + ": " + e[1];
message = message.Replace("|", ",");
logger.Notice(message);
break;
default:
logger.Notice("Got an event named " + e[0] + " but didn't know what to do with it.");
break;
}
}

}
43 changes: 4 additions & 39 deletions UdonStringEvent/EventReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,7 @@ public class EventReceiver : UdonSharpBehaviour
{
/// <Summary>An array of all Emitters in the system.</Summary>
public GameObject[] emitters;

/// <Summary>A logger that events can be output to. This is optional.</Summary>
public UdonLogger logger;

public GameObject master;

[UdonSynced]
private bool gameIsNotInProgress;
private bool lateJoiner;

public void Start()
{
if (Networking.LocalPlayer != null && Networking.LocalPlayer.IsOwner(gameObject))
{
gameIsNotInProgress = true;
}
}
public UdonBehaviour handler;

public void Update()
{
Expand Down Expand Up @@ -61,28 +45,9 @@ public GameObject GetEmitter(string characterName)

private void HandleUpdate(string characterName, string eventString)
{
bool ownerOfGame = true;
if (Networking.LocalPlayer != null)
{
ownerOfGame = Networking.LocalPlayer.IsOwner(master);
}

// As it stands, hard-code your events in this function.
// This is pretty basic. Once maps and lists exist in Udon, this can be improved.
string[] e = eventString.Split(',');
Debug.Log("Got an event named " + e[0] + " with payload " + eventString);
switch (e[0])
{
case "ChatMessage":
string message = characterName + ": " + e[1];
message = message.Replace("|", ",");
Debug.Log("Notice: " + message);
logger.Notice(message);
break;
default:
logger.Notice("Got an event named " + e[0] + " but didn't know what to do with it.");
break;
}
handler.SetProgramVariable("characterName", characterName);
handler.SetProgramVariable("newEvent", eventString);
handler.SendCustomEvent("Handle");
}

/// <Summary>Get an empty emitter and assign it to the new player.</Summary>
Expand Down
14 changes: 4 additions & 10 deletions UdonStringEvent/PlayerEventReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PlayerEventReceiver : UdonSharpBehaviour
{
/// <Summary>An array of all Emitters in the system.</Summary>
public PlayerEventEmitter[] emitters;
public UdonBehaviour handler;

private string characterName;

Expand Down Expand Up @@ -77,15 +78,8 @@ public void FreeAllEmitters()

private void HandleUpdate(string characterName, string eventString)
{
// As it stands, hard-code your events in this function.
// This is pretty basic. Once maps and lists exist in Udon, this can be improved.
string[] e = eventString.Split(',');
Debug.Log("Got an event for player " + characterName + " named " + e[0] + " with payload " + eventString);
switch (e[0])
{
default:
Debug.Log("Got an event called " + e[0] + " but I don't know what to do with it.");
break;
}
handler.SetProgramVariable("characterName", characterName);
handler.SetProgramVariable("newEvent", eventString);
handler.SendCustomEvent("Handle");
}
}

0 comments on commit 88a0212

Please sign in to comment.