Decoupled Bootstrap
This is something I've noticed while working on a tangential piece of code and wanted to open for suggestions and discussion. None of this is particularly pressing, but with modularization efforts in mind this feels like an apt thing to bring up.
The Problem
Currently, due to the way we bootstrap information for the frontend, ClassInfo.FromClass
requires access to several classes outside of the scope of the TeachR.Student
namespace (and module). In my opinion this leads to the following issues:
- hard links and unnecessary coupling
- currently requires direct access to info in e.g.
TeachR.Atmosphere
only to serialize it and push it to the frontend - may introduce new hard links further down the line as new features are added => potentially causes conflicts or circular references that would need to be worked around
- inflexibility
- no way to programmatically add, remove or alter fields other than through changes to module code
- requires module code to be altered by anyone that needs new bootstrap data
Currently the following code governs what is sent over at bootstrap-time:
var res = Object.FindObjectsByType<StudentController>(
FindObjectsInactive.Exclude, FindObjectsSortMode.None)
.Select(s => new StudentData(s)).ToList();
var request = new BootstrapData
{
students = res,
noiseLevel = AmbientSound.CurrentNoiseLevel,
maleQuestions = SoundLoader.MaleQuestionClips,
femaleQuestions = SoundLoader.FemaleQuestionClips,
scene = SceneController.LastLoadedScene.ToString(),
weather = "sunny",
};
return request;
Proposal
We could make use of the fact that currently the bootstrap data simply flows from Unity to the frontend, and thus can very easily be serialized from a generic Dictionary<string,object>
using the Newtonsoft JSON parsers.
Thus we would merely need to determine some canonical way to get information from various places into the bootstrap controller at the point in time when it wants to send info to the frontend.
This could look a little bit like this, e.g. when adding the current noise level to the info package:
// Anywhere in the code
BootstrapController.AddData("noiseLevel", () => AmbientSound.CurrentNoiseLevel);
where we have the signature BootStrapController.AddData(string, Function<object>);
, which makes use of a simple callback to calculate the desired information at call-time and add it to the bootstrap data package.
This adds a bit of boilerplate, but also gives us the flexibility to add info to the bootstrap package dynamically and entirely dependent on our current needs without ever having to touch it's module code.
Challenges
The most obvious challenge here should be, that we need to make sure that callbacks are registered before the bootstrap info is compiled and sent. Similarly, the frontend will need to be braced against potentially missing fields and might need to be able to re-request specific fields should they be missing.
This also inevitably makes working with the Bootstrap info a bit more complex within the C# codebase, since info may not be located in the same place anymore (although it should still be fairly straight-forward to follow references in the editor).