Notice on Deprecation:
This prefab will be deprecated reasonably soon. Currently I am waiting on the incoming Udon networking upgrade that will feature features that are transformative to this area of VRChat. USEv2, which will replace this, will be dramatically more reliable, scalable and easier to use.
When this repo is deprecated, the link to its replacement will be placed here.
A system by which complex events can be sent over the network in Udon for VRChat. Written in U#.
This includes a simple chat system.
An overview of the system can be found here: https://www.youtube.com/watch?v=77Z2nvkVsf4&feature=youtu.be (Note: the Youtube video is quite out of date)
Assuming you are using UdonSharp:
- Import the UdonStringEvents prefab into your world and modify the EventHandler.cs file with new event names.
- Inside any UdonSharp file, pass in the imported EventReceiver object.
- In any function where you want to send an event with a payload over the network, call the receiver's SendEvent(string eventName, string payload) method with a good event name.
- In the EventHandler UdonSharp file, add a new case to the switch within Handle(). Match it to the name of the event you named in step 3.
- In the case, do whatever you like. The private var EventHandler.newEvent contains a comma-separated string array formed as [eventName, payload, clock], where clock is an incrementing integer that is equal to the total number of events sent. If you send a CSV string as a payload, this will be reflected in the received event.
Take a look at UdonChat for an example of this in use.
- UdonStringEvents
UdonStringEvent hides away event management behind syncing strings, meaning you can avoid having data races between updated synced vars and custom network events.
- UdonChat
An implementation of UdonStringEvent that makes a text chat interface for VRC. The latest prefab is always available on the Releases page.
- UdonKeyboard
A UK English Keyboard built in Udon. Easy to modify for other layouts. Feeds text into an InputField by default, but this is also easy to change. It is built into the UdonChat prefab.
When implementing UdonStringEvents you'll probably want to unpick how UdonChat was made first, as it'll illuminate how to make the systems go brrrr.
EventReceivers pass all events to EventHandlers. An EventHandler is an abstract class that can be described as follows:
EventHandler { string characterName; string newEvent; void HandleEvent(); }
An EventHandler is an UdonBehaviour, meaning this is a clean exit point into UdonGraph or other Udon implementations. As in, yes, you can use UdonStringEvents and its friends if you write in UdonGraph, as long as your EventHandler graph implements the above interface.
There are three problems with this system:
- Speed. This is the slowest possible way to send events in Udon.
- Network load. This is a high-intensity system and will negatively effect your network performance if abused.
- Event volume. If you send more than one event every half second or so via this system, there is a risk that the earlier of the two events is not received. Take care to only use this system when you need a payload, and use SendCustomNetworkEvent otherwise.