Solving Dependencies
1_ScriptableVariable
Last updated
1_ScriptableVariable
Last updated
In this scene, I want to illustrate how SOAP can help you solve dependencies with 2 simple examples. For the first example, select the “prefab_player” and check its Player component. You can see that there is a reference to a SV of type Player.
If you open the code, you will see that the only thing this script does is to assign the value of the SV in Awake(). Ignore the Scriptable List logic for now. Now that the player value is set, we can access it from any other class simply by referencing the SV of type player (example_player). To see how this is done, select the PlayerNotifier in the scene hierarchy.
You notice that it has a reference to our example_player variable. Now if you open the code, you can see that we can access our player very easily. This is nice, no need for singletons or other spaghetti code. Also, you can even reference this variable in prefabs, or scriptable objects as the SV example_player is an asset! This can be very powerful (for example when implementing abilities that require the player) and clean. I cover this in the youtube tutorials, so if you want to know more, I highly recommend watching those.
If you don't want to always create a custom variable type, you can use the ComponentVariable and perform a cast operation when reading the value. It is less performant, but its more convenient ^^. In this case it would like like this with a ComponentVariable instead of a PlayerVariable:
The second example, if you click on the “prefab_player”, you can look at the Movement Controller and the Input Detector.
If you press play, you can move the player with WASD / Arrow keys.
Here, we are solving the dependency of the movement controller with a scriptable variable. Input Detector writes to example_vector2_inputs (ScriptableVariableVector2) and Movement Controller reads from example_vector2_inputs.
Those 2 classes don’t know each other and the only thing they need is a scriptable object to write/read data. You can imagine how this can be useful in other cases as well. You can have entire parts of your game independent from another one, relying only on scriptable objects for communication.
This is particularly useful when working across different scenes. For example, your UI elements can be in their own scene and reference Scriptable variables without needing to access anything specific to a scene.