- Home /
How to create a searchable list of ScriptableObjects?
I am currently working on a multiplayer network game and the problem I am looking to solve is the following. My game contains effects that can be applied to the player. I have a created a "BaseEffect" scriptable object that is used to create different effects with all of the values/logic contained to be applied to a player. I would like to avoid passing the entire effect object over the network when being applied to another player, instead I would like to pass EffectId = 0 so the server/client can simply look up the effect via its ID and apply it to the player.
What would be the best way of doing this, while keeping them as scriptable objects?
Should I create a static list of these scriptable objects that is accessible at runtime for reference? Or is there a cleaner way to have a searchable list of scriptable objects.
Also: Is my idea of passing over the effectID vs the entire effect object a bad idea? if so why?
Example:
PlayerA uses "Slow" on PlayerB
PlayerA send request to server to apply effect with ID = 2 to PlayerB
Server verifies request, tells PlayerB client to apply effect with ID = 2 to itself
PlayerB looks up effect with ID = 2 and applies the slow effect.
Thanks for any advice!
Answer by GetLitGames · Mar 11, 2021 at 09:46 PM
Add an Id property, whether an int or string
use the Unity Singleton pattern on a class like public class ItemDefinition: Singleton, and attach it to a gameobject in your scene like ItemManager https://wiki.unity3d.com/index.php/Singleton
add an array[] or List Items of your scriptableobject class as a [serializedfield] (ItemDefinition is a scriptableobject in this example)
drag all your scriptables from your project into your array/list on the gameobject inspector and save the scene
add a public method like ItemDefinition GetItemById(int id)
ItemDefinition GetItemById(int id) { return Items.FirstOrDefault(x => x.Id == id); }
anywhere you need it just use ItemManager.Instance.GetItemById(1); and it will return the ItemDefinition scriptable or a null if the id isn't found
assign the return to a variable, and use the variable multiple times - don't just paste GetItemById everywhere
Answer by jesse-small · Mar 18, 2021 at 04:56 PM
I accepted @SubtleTechnology answer as it ended up being very similar to my implementation. I ended up creating a pseudo "database" object that accepted a list of scriptable objects and had a custom editor button to initialize all of its IDs for me. This ensured I could give each SO a unique ID and had an easy way to reset the IDs if needed. (My logic does not depend on IDs staying the same between sessions, and this is very handy for debugging purposes).
Your answer
