- Home /
Handling hundreds of conditions in ScriptableObjects
Hi there, got a question that's sort of complicated (I think). I have about 500 scriptable objects, each one with a different sound. I want them to be played when a specific condition (or multiple conditions) happen in my game. How should I go about doing this? Should I have events or conditions in the scriptable object itself? Should I have a reference to my GameManager manager in each of the scriptable objects and check every frame if that specific condition is met? Or the other way around, and have a reference to every scriptable object in my GameManager and check for every condition? Should I have events instead? If so, how should I go about it?
I appreciate any help on how to solve this issue, thanks!
Answer by jtok4j · Nov 03, 2020 at 08:34 PM
Greetings, From your description, you may want to check out Events and Delegates.
Essentially this allows all the checking to done in the background and not by your scripts running endless checks in update or similiar high-resource requiring behaviour.
Essentially, it's like putting a sensor on your umbrella in your closet. Every time the sensor detects rain, it beeps, so you hear it when leaving the house and take it with you. Events and Delegates will allow you to put on the sensor and leave the sensor to detect the condition you want it to act upon once detected.
These 3 resources have helped me understand and use events and delegates:
https://learn.unity.com/tutorial/delegates
http://www.unitygeek.com/delegates-events-unity/
https://unitydojo.blogspot.com/2015/03/how-to-use-delegates-in-unity-like-boss.html
Keep on Creating!
Justin of JustinTime Studio http://unity3d.expert
I am aware to Events/Unity Events, but how would you use them in this specific situation? Each SO should have multiple conditions and there will be hundreds of SO's
Create the events for the conditions, then go through your SOs and subscribe them to the correct events. If an SO requires multiple conditions to be met (an AND situation), then you will need additional logic capturing when events happen and only play the sound when all conditions have been met. If an SO may be triggered in a variety of conditions (an OR situation), then you just play the sound.
I'm not sure this would work in my situation, as the owner/investor wants the SO's to be able to select the conditions in the editor.
I've decided to go with reflection for the time being.
I've got an enum he can choose from, and when it's the appropriate time, I run through the methods with the same name.
$$anonymous$$aybe I should store them in a Dicti at runtime though Dictionary>.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting a reference to a group of ScriptableObjects implemented at runtime? 0 Answers
How to Update ScriptableObject 1 Answer