Jump to content

Can I store a persistent variable on a part?


Recommended Posts

I want to store some variables on parts, and have those variables saved in the save game.

But what is tricky - and forgive me i'm finding it hard to explain this -- is I want to interact with an instance of the part on a vessel, doing various calcs and what not, but I then want to store data on the BASE of that part, so that it isn't just on that instance, but is on the part permanently in the VAB and on any OTHER vessels the part gets put on.

1) You go into the VAB and make a Vessel123. You place Fuel Tank SuperB onto your vessel.

2) You go fly Vessel123 and my KSPAddon/PartModule is doing shady things in the background to Fuel Tank SuperB on Vessel123, and is saving data back on the root Fuel Tank SuperB.

3) You crash, or retrieve, or ignore, Vessel123

4) You go into the VAB and make Vessel456. You place Fuel Tank SuperB onto your vessel.

5) You go fly Vessel456 and my KSPAddon can see the previously recorded values from your Vessel123 flight.

Does that make sense?

Basically I need to store persistent data on every part in the game and I really don't want to have to do that on my own in my own Addon. It would be ideal if I could just store it on the parts themselves.

Link to comment
Share on other sites

As you are trying to save data across different parts and vessels, I would actually recommend you look at a scenario module instead.

Here's a quick example that loads a scenario in flight mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using UnityEngine;

namespace OnScreenJoyStick
{
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)] //install our scenario module. is a partmodule so all calculations can be done in it, no need for a seperate plugin class in the scene
class OSJMainMenu : MonoBehaviour
{
public void Start()
{
var game = HighLogic.CurrentGame;
ProtoScenarioModule psm = game.scenarios.Find(s => s.moduleName == typeof(OSJFlight).Name);
if (psm == null)
{
psm = game.AddProtoScenarioModule(typeof(OSJFlight), GameScenes.FLIGHT);
}
}
}
public class OSJFlight : ScenarioModule //this runs on flight scene start, no need for the KSPAddon as it is already loaded by the code in the SpaceCenter screen.
{
//All your plug in code here. Start, Update, FixedUpdate, OnSave, OnLoad, etc. all work as they do in a partmodule.
}
}

Then make a list of Parts -> Data you want to save in the scenario module and save the data in there.

D.

Link to comment
Share on other sites

Yes I want to store data across all parts. I'm basically making a custom career system and need to store information about parts so that it affects the gameplay.

I thought scenarios were the tutorials system, IE outside of the standard "Sandbox" or "Career Mode"?

My point still stands though. I'm tryign to avoid having to maintain an internal list of all parts and the data on them. I was really hoping I could let the part store it for me?

Link to comment
Share on other sites

Maybe i'm overcomplicating things. If I make a PartModule, that gets attached to a part and it has data in its ConfigNode. It has default values that obviously take effect when you create the part by putting it on a vessel. Can't I just store dynamic data into that same confignode?

Link to comment
Share on other sites

Maybe i'm overcomplicating things. If I make a PartModule, that gets attached to a part and it has data in its ConfigNode. It has default values that obviously take effect when you create the part by putting it on a vessel. Can't I just store dynamic data into that same confignode?

The config node is unique to each part with that part module. Every time you add another instance of that part it will start-over with its initial settings.

Diazo's probably right. Scenario modules are used for the tutorials but they can be used for much more. They save data for the entire save file (the R&D center is a scenario module, you can look at its entry in the persistent file and see things like which tech nodes have been researched and which science experiments have been conducted) so you can store your data in there and tell your part module to get whatever it needs out of the scenario module. You'll only need to store one copy of every variable that you're interested in, then each part module should be able to use the most updated value for that variable.

Link to comment
Share on other sites

The shortcut I use in thinking about ScenarioModules is that they are essentially a partModule. But where a partModule is assigned to a part, a scenarioModule is assigned to a game (on a per save, not per installation, basis).

Then everything, including OnLoad and OnSave, work pretty much the same.

On your thought about assigning stuff to the "base" part, that will not work because the base part you are talking about does not exist.

How parts work is that every time KSP loads, it reads all the part.cfg files in the GameData folder and creates a library of protoParts. Then when that part is loaded in game, a copy of the part is created and then the Load pass is run to restore data from the persistence file for that copy of the part only.

So in essence, the "default config node" you are asking about in your previous post is in actuality the part.cfg file attached to that part in the gamedata directory.

Hope that helps.

D.

Link to comment
Share on other sites

This line here:

 psm = game.AddProtoScenarioModule(typeof(OSJFlight), GameScenes.FLIGHT);

You would have to check the object browser for the syntax, but the GameScenes.FLIGHT variable is what gets changed.

D.

Link to comment
Share on other sites

This line here:

 psm = game.AddProtoScenarioModule(typeof(OSJFlight), GameScenes.FLIGHT);

You would have to check the object browser for the syntax, but the GameScenes.FLIGHT variable is what gets changed.

D.

Yeah I figured that. I thought it was a bitfield or something of that type, but nothing I do seems to make it load in multiple scenes. Hmmm.

Link to comment
Share on other sites

You just put something like this in when you first initialize the scenario module. One problem to be aware of is that you might not be able to add extra target gamescenes once the scenario module has been created for the first time, at least not using the methods common in other mods.

So when you're still tinkering around you might want to start a new save file or come up with a method to add more scenes target scenes to the module. Or you could just manually add them in the persistent file, I don't think I've ever tried that, but it should work (it should be the first line after the name of the scenario module, though I'm not sure which numbers correlate to which game scenes).


Game g = HighLogic.CurrentGame;
var p = g.AddProtoScenarioModule(typeof(DMScienceScenario), GameScenes.FLIGHT, GameScenes.SPACECENTER, GameScenes.TRACKSTATION);

Edit: Here are the game scene numbers:


LOADING = 0,
LOADINGBUFFER = 1,
MAINMENU = 2,
SETTINGS = 3,
CREDITS = 4,
SPACECENTER = 5,
EDITOR = 6,
FLIGHT = 7,
TRACKSTATION = 8,
SPH = 9,
PSYSTEM = 10,

Edited by DMagic
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...