- Home /
ScriptableObject for custom items & spells in-game
Hey there. So I've got an inventory set up that stores a list of ScriptableObjects that make up all of my items, including weapons and armour, etc., as well as a "Spellbook" section for holding all of the player's spell objects. The player can equip a sword SO that references a mesh, which will be displayed on screen, but all of the damage calculation happens through the stats held within the SO.
ScriptableObjects are great for quickly creating objects inside the editor, but if I want to implement some sort of system where the player can create their own objects with custom stats, is there any way to do this with SO's?
I want the player to be able to go into a spellmaking interface, where they can select a "Fire Projectile" or "Heal Self" type and then set the variables for damage and speed inside that interface, then save all of this to a spell object that gets added to the "Spellbook" list. I guess what I'm describing is the process of making a SO through "Assets->Create" in the editor, but in the game instead. Is this possible?
This concept also extends to creating enchantments for weapons and armour as well. If the player has an Iron Sword that's the same as all the other Iron Swords (due to being created through a SO), and they want to create an enchantment that modifies the damage of that weapon, or the weight or any of the predefined stats from the SO, would this be possible?
Basically will SO's work with these ideas or will I have to go back to using prefabs with monobehaviours?
Thanks, hopefully that all made sense!
Answer by Sabre-Runner · Jul 09, 2018 at 05:45 AM
Scriptable Objects are more configuration and settings items. You can use them for templates and let the player start with a copy of a Scriptable Object you made previously. But then you will have to serialise their creation (into JSON, for example) and to load them up again when the session is restarted.
When you say that I would need to serialise their creation, do you mean in order to use it in game after its creation or in order to have it be able to be saved and loaded?
In order to reload once a session ends and restarted. When you generate the object in runtime, you can just keep the object in memory. But it won't persist past shutdown.
Answer by JedBeryll · Jul 09, 2018 at 05:41 AM
Yes and no... You can create scriptable objects at runtime but you can't save them normally (without using asset store tools). The closest best thing would be to save the user created data like a save game with custom classes and load those into scriptable objects that you create at the start of your game. See the answers here for more details: https://answers.unity.com/questions/896298/savingloading-scriptableobject-during-runtime.html
So if I used ScriptableObject.CreateInstance, declared the variable values and then added this new instance to the list of spells the player has, then saved the whole list, would this work? Or would it not work specifically because the new instance was created at runtime?
(Edit: I have Save Game Pro from the asset store that supports saving lists and custom types, if this would help matters)
Yes, I think Save Game Pro would work, tho I don't have it so I can't confirm. But saving as a list or any other way would not work because you can't normally serialize anything that derives from UnityEngine.Object.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers
Filter data in scriptable objects 0 Answers