Jump to content

How do I get a current "GAME" ConfigNode?


Recommended Posts

Hi all,

First off, I now realize you long-time modders are waaaayyyy under-appreciated.  KSP does not make modding easy.  I have literally spent weeks of agony trying to get data from KSP. 

BLUF: Want to get current game data to send to web server in JSON format.  Prefer to just send all the data and let the server deal with it.

I am trying to send data to a server for an operations tracking program.  I have the overall system working, but I noticed that the data I get from "HighLogic.CurrentGame.config" is not the most current.  I.E. if I am in a new vessel in flight, there is not even a "VESSEL" node for that vessel until I switch scenes again.  I have also tried "HighLogic.CurrentGame.Updated().config" with no change.  I have also tried loading the "game.config" from the GameEvents.onGameStateSaved.  I have also tried loading the ConfigNode returned by GameEvents.onGameStateSave.  Lastly, I tried reading in the persistent.sfs to a ConfigNode.

ALL of these options worked, EXCEPT they are all "old" persistent data.  I am trying to get what the game is actually seeing "right now".  Do I need to go through and serialize everything myself?  I wrote my own ConfigNode to Json function to format ConfigNodes, but I do realize that most of the unity objects are serializable by themselves.

THANK YOU

using System;
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Text;
using System.Threading.Tasks;

namespace KOMS
{
    [KSPAddon(KSPAddon.Startup.AllGameScenes, true)]
    public class KOMS : MonoBehaviour
    {
        public void Start()
        {
            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");
            if (HighLogic.CurrentGame != null)
            {
                //ConfigNode gameConfig = HighLogic.CurrentGame.Updated().config;
                ConfigNode gameConfig = game.config;
                string jsonToSend = ConfigNodeUtilities.NodeToJson(gameConfig, false);
                KomsApi.Post(jsonToSend);
            }
            else
            {
                Debug.Log("HighLogic.CurrentGame is null");
            }
        }
    }

    public class ConfigNodeUtilities
    {
        public static string NodeToJson(ConfigNode node, bool prettify, Output output = Output.All, int tabLevel = 0)
        {
            ...
        }
    }

    public class KomsApi : MonoBehaviour
    {
        public static async void Post(string jsonData)
        {
            ...
        }
    }
}

 

Link to comment
Share on other sites

The game's active data is stored in objects with types specific to the data in question, not ConfigNodes; ConfigNodes are only generated when saving to disk. If you rely on having everything in ConfigNodes, your data will always be out of date, as you've noticed.

For example, the current vessel is a Vessel object that can be accessed via FlightGlobals.ActiveVessel:

Link to comment
Share on other sites

Thanks so much for the quick response.

It looks like I will have to gather all the data and send one or more requests to my web server. 

Your help is a great relief, as I was going crazy searching for an easy way to just output all the game tracked data (roster, facilities, vessels, etc.) to a database and that does not exist.

Thanks again.

Link to comment
Share on other sites

22 hours ago, orionguy said:

I am trying to get what the game is actually seeing "right now"

This data doesn't exists, not under a serialized (ConfigNode) form.

KSP holds its data in various subsystems, most of it is easy to get from various static accessors or fields in singleton top level classes.
For vessels (and everything they are made of) and bodies, you have FlightGlobals, for Kerbals you have KerbalRoster.
Some other relevant top level classes are all subclasses of ScenarioModule, like ResearchAndDevelopment, ScenarioUpgradeableFacilities, Funding, Reputation, ContractSystem...

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...