Create Communication between Unity and the frontend authored by Tobias Möhring's avatar Tobias Möhring
This page details the process of sending information between the frontend and Unity. Communication occurs via web sockets in a (roughly) event-based manner (somewhat like Socket.IO, but due to lacking a correct server implementation on the Unity-side of things we're using bare web sockets instead).
## Basics
Our implementation makes use of a simple serialization scheme following the pattern `className;payload`, where `className` can be any string and `payload` can be empty, a string or a string-serialized object (JSON).
In both backend and frontend corresponding listeners will be triggered based on the incoming message and the message payload will be delivered to the respective handlers.
## Unity
Currently (as in: subject to change once I get around to it), the backbone of the IPC (inter-process communication) on Unitys side is `WebsocketController`, which is first initialized in the `InitGlobals` in each scene.
### Setting callbacks
We can register callback functions to react to signals from the frontend using several functions (all of which are `static` methods of the `WebsocketController` class):
* `void SimpleCallback(string className, Action<string> callback)`: treats the payload of the incoming message as a bare string
* `void JsonCallback<T>(string className, Action<T> callback)`: automatically deserializes the payload based on the type parameter `T` for the callback.
### Sending messages
To send new information to the frontend, `WebsocketController` defines some further static methods:
* `void Respond(string className, JsonData data)` sends a serialized object where `JsonData` is the base type that has to be extended by the data class
* `void SendMsg(string className, string msg)` sends a raw string to the respective message class for the frontend to react to.
### Implemented Messages (may be non-exhaustive)
Below is a list of messages that (at time of writing) can be used to relegate data to the classroom in Unity:
* `classChange`, payload: `string` serialized object of shape `{room: string, front: string, outside: string, chairs: string, students: Number, messiness: Number, weather: WeatherPattern}`, where all of these fields may be optional
* `bootstrap`, payload: none. Request for Unity to send initial classroom info
* `behave`, payload: `{students: string[], behaviour: string}`. List of students and a behaviour to apply to them
* `askQuestion`, payload: `{students: string[], questions: Number[]}`. List of students and corresponding question ids
* `impulseGiven`, payload: `{students: string[], impulse: string}`. List of students to trigger impulse for
* `themeChange`, payload: `{theme: string, skip: boolean, globalKnowledge: boolean}`
## Frontend