Jump to content

Maybe I don't understand OnSave()?


Recommended Posts

Very very new to KSP mods but not new to programming.  

I set up my IDE and used the example mod from the forums and everything works great.  

I'm just confused about OnSave.  I will eventually want to send a message with the persistence file to a node.js api every time the game has been saved by the user or by persistence.  First step is knowing when the game has been saved.  I have tried the following...

using System;
using UnityEngine;

namespace TestMod
{
    [KSPScenario(ScenarioCreationOptions.AddToNewGames | ScenarioCreationOptions.AddToExistingGames, GameScenes.SPACECENTER | GameScenes.MAINMENU | GameScenes.FLIGHT)]
    public class TestMod : ScenarioModule
    {
        public override void OnLoad(ConfigNode node)
        {
            base.OnLoad(node);
        }
        public override void OnSave(ConfigNode node)
        {
            Debug.Log("A save happened!");
            base.OnSave(node);
        }
    }
}

Nothing happens.  Maybe I don't understand when OnSave is triggered or why?  or how?  

Thanks for any help you can provide.

Link to comment
Share on other sites

On 8/28/2022 at 3:11 PM, Jason Melancon said:

Does it work if you delete "override" from the method definitions? That modifier is absent in the "Getting Started" thread examples (https://forum.kerbalspaceprogram.com/index.php?/topic/153765-getting-started-the-basics-of-writing-a-plug-in/).

Thanks for the help, but that change didn't do the trick.

However, I saw other examples where folks used KSPAddon instead of KSPScenario and bound their own load and save functions to the  GameEvents.  This seems to work.

using System;
using UnityEngine;

namespace TestMod
{
    [KSPAddon(KSPAddon.Startup.MainMenu, true)]
    public class TestMod : MonoBehaviour
    {
        public void Awake()
        {
            Debug.Log("Testmod Awake");

            GameEvents.onGameStateCreated.Add(GameLoad);
            GameEvents.onGameStateSaved.Add(GameSave);
        }
        public void GameLoad(Game game)
        {
            Debug.Log("Load triggered");
        }
        public void GameSave(Game game)
        {
            Debug.Log("Save triggered");
        } 
    }
}

 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...