SpaceWarpDev / SpaceWarp

A C# modding API for KSP2
MIT License
91 stars 38 forks source link

Add API registration for mods that want to use game's built-in saving and loading of data stored in the save game files #270

Closed Falki-git closed 1 year ago

Falki-git commented 1 year ago

This adds 3 API methods for mods to:

Usage is:

SpaceWarp.API.SaveGameManager.ModSaves.RegisterSaveLoadGameData(
            "my.mod.guid",
            MySaveDataObject,
            (savedData) =>
            {
                // This function will be called when a SAVE event is triggered.
                // If you don't need to do anything on save events, pass null instead of this function.
            },
            (loadedData) =>
            {
                // This function will be called when a LOAD event is triggered and BEFORE data is loaded to your saveData object.
                // You don't need to manually update your data. It will be updated after this function.
                // If you don't need to do anything on load events, pass null instead of this function.
            }
        );

MySaveDataObject can be any kind of object.

I'll keep this as a draft for a while to get comments and so I could test it in real scenarios more thoroughly.

cheese3660 commented 1 year ago

Would it be possible to change the API to instead be a generic T RegisterSaveLoadGameData<T>(string key, Action<...> onSave, Action<...> onLoad, T defaultValue = null) where it returns a default constructed object (Via Activator.CreateInstance(typeof(T))), or the default value if one was passed, that represents the saved data

Falki-git commented 1 year ago

Like so?

cheese3660 commented 1 year ago

Yep, exactly like so