Reference to instances of scriptable objects

Discussion and help for Easy Save 3
Post Reply
TheGaijin87
Posts: 5
Joined: Sat Nov 27, 2021 5:09 am

Reference to instances of scriptable objects

Post by TheGaijin87 »

I read through the documentation but either i dont see the connection or its not in there...
my problem is that i have an item system that uses scriptable objects as items. now some of those items have fields that could change on a per item basis so instead of using the scriptable object directly i make instances of them and add them to my inventory. then i can change these fields and have different items of the same kind (e.g. the same pants but with different colors).

now when i save my inventory it works as long as i set the scriptable objects themselves but when i pick something up / put something in chests etc the game uses the instance and not the "reference" base scriptable object. ive seen that prefabs have a script that i can attach to get the reference to the prefab but as thats a monobehavior i cant add it to scriptable objects. is there another way to get a reference to the "base" scriptable object item so that ES can automatically handle them like prefabs?

im probably just missing some obvious way but on the chance i dont i thought i would ask here.

edit: okay so... i was playing around with manually adding references. i got an itemholder component that has a list of all items (which are all scriptable objects) and at the start i manually add all of them to the reference manager with their ID (my internal item ID) as reference ID. i then save the inventory (that holds the items) and when it loads it loads the slot and its item correctly, with the difference that the saved item was an instance of said scriptable object but the loaded item is the scriptable object itself. thats okay for many of the "normal" items but i need instanced scriptable object items for stuff like differently colored clothes, weapon effects, durability etc. and that wouldnt work well with those.

i am saving with saving the gameobject as whole right now, in case thats important. i just need it to make instances when loading item scriptable objects.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Reference to instances of scriptable objects

Post by Joel »

Hi there,

I’m afraid it’s difficult to understand what you mean. Please could you create a brand new project with a very simple scene which demonstrates your issue and private message it to me with step by step instructions on how to replicate it and what you expect to happen.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
TheGaijin87
Posts: 5
Joined: Sat Nov 27, 2021 5:09 am

Re: Reference to instances of scriptable objects

Post by TheGaijin87 »

Didnt have time to check it out over the weekend but will try again tomorrow.

In essence its quite simple: i have scriptable objects that represent my items. now if i have the same sword twice in my inventory they need to have different durability values (or maybe different stats altogether) so i make an instance of the scriptable object and use that during playmode.

when i exit playmode and start again i cant load it because its just an instance that obviously isnt present at the current time and would have to be created via instance from its "parent/prefab" scriptable object. like with prefabs i can attack the prefab script that gives them an ID and creates them based on that. i would essentially need the same for scriptable objects.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Reference to instances of scriptable objects

Post by Joel »

Hi there,

When you have time, please could you show me the code you're using to save and load your ScriptableObjects? Hopefully this will be enough for me to see whether you are doing anything wrong.

Many thanks,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
TheGaijin87
Posts: 5
Joined: Sat Nov 27, 2021 5:09 am

Re: Reference to instances of scriptable objects

Post by TheGaijin87 »

Right now im doing it very basic with a simple:

ES3.Save(this.name, this.gameObject);

on the inventory that is holding all the information about its contents, e.g. which item (scriptable object instance) and amount.

when i exit playmode and enter it again and try to load it just says that the object couldnt be found, which is obvious as ES3 doesnt seem to know which scriptable object is the "parent" of the SO instance. if i add them not as instance but as a normal SO then it works or if i use the instance but not leave playmode before loading then it works as well, as the instance is still available.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Reference to instances of scriptable objects

Post by Joel »

Hi there,

You’re saving the GameObject which contains a field which contains the ScriptableObject, not the ScriptableObject itself. This means you’re storing it by reference, not by value.

To store it by value you need to save the ScriptableObject separately (or an array of ScriptableObjects).

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
TheGaijin87
Posts: 5
Joined: Sat Nov 27, 2021 5:09 am

Re: Reference to instances of scriptable objects

Post by TheGaijin87 »

i read that one already in another post about essentially the same problem and unfortunately it didnt help him nor me. or i am misunderstanding it somehow but i did add a code snippet that saves each scriptable object instance once when it gets created but he still couldnt find it. i will try to make an empty example project tomorrow to showcase the problem

i read multiple topic about the same problem (instanced scriptable objects) and none of them seemed to have a working solution and only workarounds. i will comment tomorrow again with the progress on the example project. maybe it solves itself when its stripped down to an easy example.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Reference to instances of scriptable objects

Post by Joel »

Hi there,

If that didn’t resolve your issue then please could you create a new project with a simple scene which replicates this and private message it to me with step by step instructions.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
TheGaijin87
Posts: 5
Joined: Sat Nov 27, 2021 5:09 am

Re: Reference to instances of scriptable objects

Post by TheGaijin87 »

send you a pm with an example of what i mean and some instructions. if anything is unclear please let me know.

i think the problem is that ES3 handles instances of scriptable objects the same as scriptable objects. they are their own object. it would have to work more like prefabs where the instance of the SO has a prefab/parent reference to create it when loading but that doesnt seem to be possible right now. if he saves all the values (which he does) then it would be enough to instantiate a new temporary scriptable object of the same type and change the values (although i would prefer having instances of the correct parent object but it would still be better than nothing)

edit: okay its a bit more difficult than that. when i make a list with only the items, without the step in-between with the slot that holds the item and the amount then it works. when i use the slot it doesnt work. gonna play around a little bit more.

edit2: the problem seems to be the constructor that uses parameters. i probably did something wrong with it and have to adjust it.

edit3: at least i thought so as it was very briefly working a few seconds ago after i changed it to a parameterless constructor. but now i changed it back again and its not working again with neither parameter or parameterless... this drives me crazy lol

edit4: as it seems now i can "force" it to do the right stuff by going into the Es3scriptableobjecttype.cs and in the ReadObject method, where he checks for the type of the scriptable object i have to manually add a check if the type is the same as the scriptable object type (if(propertyName == ES3ReferenceMgrBase.referencePropertyName && refMgr != null && type != Type.GetType("TestItemSO,Assembly-CSharp"))). then it works and he doesnt go into the first "if branch" but into the "else branch" instead and then it works. i am not quite sure why he doesnt go there on its own. it seems to have something to do with the "ES3ReferenceMgrBase.referencePropertyName" which should fail, as the other part of the check only checks if the RefMgr is null but as its in the scene that will always not be null (if i understand it correctly). i also changed the settings to value type only instead of ref and value but that didnt change anything.

edit5: okay i guess im just super confused now. i tested around a bit more and when i do the edit4 change to the Es3scriptableobjecttype.cs then it does indeed create those correctly but i didnt check if they also contain the correct information and apparently they are all just blank... so this didnt help. when i do have a separate item list that also contains all referenced items then it does load these correctly but the ones in the slot dont get loaded. if i directly load again it then loads them correctly as the pure item list already created them. so ES3 can do what i need it to do but it doesnt do it the way i think it would do and i havent found a way to get it to work as i want yet lol... sorry for the confusion but maybe when you see it you know what im doing wrong.
Post Reply