Runtime Variables
Scriptable Variable
In some cases, you might want to still enjoy the benefits of Scriptable Variables (like the OnValueChanged event system) without having to create the assets in the project beforehand. For example, let’s imagine you want to have multiple enemies, each with a health bar. You want to use a generic health component and a generic health bar connected by a Scriptable Variable float (health). In those cases, you could create Scriptable Variables at runtime.
The concept is simple: instead of already referencing the Scriptable Variable in the components, you create it in Awake() or Start() using the SoapUtils static method CreateRuntimeInstance<T>:
Once the variable instance is created, you can pass it to your different components, either using an event system or directly calling a method if the components are in the same context (scene or prefab).
For a practical example of how to use runtime variables, please refer to the '8_RuntimeVariables_Examples_Scene' in the Example scene and its accompanying documentation.
Note: I have done this myself in a few games in the past, but each game is different. Make sure to assess if this pattern fits your game. This is a more advanced pattern, and as such, I would not recommend it for people who have just started using Soap. There are other ways to solve the same issue.
Last updated