Managing State through scriptableObjects in RunTime

Updated on July 29, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on July 25, 2019

Im trying to Architect my game in a way where it s easy to modify values like ( current health , curr stamina hunger etc… ) and keep things modular. So I have an inventory system with cosumable items that increases the values above.

so i want to  create a scriptableObject that Has current health stam and hunger .. and those values change in Run Time  and create a script that manages the UI of health , stamina hunger bar which reads the values in That scriptable Object . and Finaly for the consumable objects i want them to modify Values directly in the scriptableObject. 

is it a good approach and is it actually possible to do ? what are the downsides and if possible i want to hear other approaches 🙂 And thank you for ur time 

  • Liked by
Reply
0 on July 25, 2019

Possible? Absolutely. Weather it’s good or not is kinda a matter of opinion, and how complex your game is going to be.

Limitations I think would be; scriptable objects don’t save when you exit, so you would still need a way to save the player state when they leave the game. If you have enemies with health, stam, hunger, etc, they should also use a similar system to keep your game logic consistent. It’s kinda unnecessary for consumables though, as they can just directly modify the player health with no need for the middle man.

 

This would be fine for something like a solo survival game, or something without a lot of enemies or other players, as you would need to create their scriptables at runtime, and assign them for basically no extra functionality. For the player, it’s really only useful for updating the UI without causing possible NULL REF ERRORs

If the camera is part of the player’s prefab, then you’d probably be just as well off setting a direct reference to the player’s stats through your UI manager.

 

Alternatives would be like I mentioned above, and just set the references directly, or to use a mediator to relay information from the players stats to the UI manager or consumables. The mediator would actually work very similar to the scriptable object, but would not hold any of the data, you wouldn’t need to make one for everything that has stats (because they can all share the 1 mediator), and would only update the UI when you told it to, not all the time (unless you told it to, lol).

 

All of these are fine options depending on the scope of your game, and how much you want these systems to do.

  • Liked by
Reply
Cancel
0 on July 29, 2019

Thank you for your answer. you re right about the limitation of scriptable objects: they cannot be saved so instead i used a simple c# script to mange Health and other propreties it s simple to work with and easy to save . it works for now, but i don’t know how it will work out in the future. 

thank you again it really helped me 🙂 

  • Liked by
Reply
Cancel