Load list of monobehaviour

Discussion and help for Easy Save 3
Post Reply
RiseBasti
Posts: 5
Joined: Tue Nov 01, 2022 7:09 am

Load list of monobehaviour

Post by RiseBasti »

Hi

I'm currently reworking my load and save scripts and while I have an idea of how I'd like to change them, I don't know how to implement them properly.
I want to change the current system as I store each value in a separate key. This requires more storage space than I would like. That's why I want to combine the values in a key and save the class directly. (I have already tested it and can save up to 50%...)

My setup:
1. I have a “Script A” with different values (including item data, ScriptableObject B). This script inherits from MonoBehaviour.
2. I have a “ScriptableObject B” with item data. With this script I instantiate a prefab of an element, add the values and add "Script A" as a component to the GameObject.
3. I have a “Script C” with a list and all instances of ScriptA are added to this list.

My problem:
When I save and load the list from ScriptB, ES3 automatically creates an object and adds the component.

What I would like to do:
If possible, I would load the values from “Script A”. These values contain the article data (ScriptableObject B). Using this script, I was able to instantiate the element prefab and then add the Script A component to the instantiated element.

Is there a way to do this?

Another option would be to create a “SaveData” class and put all the values from “Script A” into this class. Then I can instantiate my element, add the component, load the SaveData class and set it to the Script A instance. But that doesn't feel right...

Thank you for an answer in advance.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Load list of monobehaviour

Post by Joel »

Hi there,
When I save and load the list from ScriptB, ES3 automatically creates an object and adds the component.
This indicates that you're saving a GameObject, not a script. Instead you should save the script and use ES3.LoadInto to load the data into an existing script instance.
Another option would be to create a “SaveData” class and put all the values from “Script A” into this class. Then I can instantiate my element, add the component, load the SaveData class and set it to the Script A instance. But that doesn't feel right...
If you're looking for efficiency (which it sounds like you are if you're reducing file size), then this would be the most efficient route to go.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
RiseBasti
Posts: 5
Joined: Tue Nov 01, 2022 7:09 am

Re: Load list of monobehaviour

Post by RiseBasti »

Hi

Thank you for your reply!

I'm not sure, but I think I save a MonoBehaviour script and it creates a GameObject and adds the script.

But that doesn't matter anymore because I did it the second way (the one that didn't make me feel good... :lol: )

I just have one more question. I'm not sure whether I should save my items together in a list or separately (each item as a single entry).
Certainly a list would be more efficient, but I'm not sure it's a bad idea to do it that way. If I change the count of an item and want to save that one item, I have to save the entire list. Could that become a problem?

EDIT:
I would like to add:
1. The largest lists currently have a planned 1,000-5,000 entries.
2. The currently planned size of my save files is approx. 0.5-5 MB.
I have no idea what this is like in terms of performance and whether it can cause problems or whether it involves such small amounts of data that it's not worth mentioning.

Greetings,
RiseBasti
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Load list of monobehaviour

Post by Joel »

Hi there,

Performance very much depends on the data, the hardware you're running on, and when you save. If you're frequently changing individual items in the array and need to save that change immediately then it would make sense to save items as individual keys. However, if you do this you will definitely need to use caching which is described here:

https://docs.moodkie.com/easy-save-3/es ... rformance/

But generally in game development it's recommended that you only start optimizing if you know that your code needs optimizing. So the best thing to do would be to test the array method (the simpler method) and see if you're getting any significant performance issues. If so, that's when you consider saving out to separate keys and use caching.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
RiseBasti
Posts: 5
Joined: Tue Nov 01, 2022 7:09 am

Re: Load list of monobehaviour

Post by RiseBasti »

Thank you Joel for this informations.

I have 0 knowledge about read/writes of data and it's nice to get some infos of someone with more experience. I'm already using caching and by now everything works fine. I just see saving as the most "dangerous" part to deal with updates and I want to do it as "perfect" as I can make it before I start to release any versions of my game.

All the best,
RiseBasti
Post Reply