Jump to content

[SOLVED] Backup/Restore tech tree and experiments


Recommended Posts

Hi all,

I'm looking for a way to backup and restore the tech tree and the experiment history. I know it is possible to get that data from the persistent.sfs file. However I need a method to get and also restore the data while KSP is running.

What I tried so far is using ResearchAndDevelopment.Instance:


ResearchAndDevelopment rdinstance;
//Backup
rdinstance = ResearchAndDevelopment.Instance
//Restore
ResearchAndDevelopment.Instance = rdinstance

However it didn't work. Unfortunately I didn't find any projects that are accessing the R&D data in a way I would like to do.

Any help would be appreciated!

Edited by nadvornm
problem solved
Link to comment
Share on other sites

R&D.Instance might not be available until a few frames have passed, so work in some way of waiting for it to exist before trying to use it. If you're really lucky, you might be able to get the game to do your heavy lifting for you by calling R&D.OnLoad/OnSave yourself. Otherwise, here's an example snippet that unlocks any parts that haven't been "bought" in tech nodes that have been unlocked. It might give you some ideas:

foreach (var part in PartLoader.LoadedPartsList)
{
var techNode = ResearchAndDevelopment.Instance.GetTechState(part.TechRequired);

if (techNode != null)
if (ResearchAndDevelopment.GetTechnologyState(techNode.techID) == RDTech.State.Available)
if (!ResearchAndDevelopment.PartModelPurchased(part) && !techNode.partsPurchased.Contains(part))
{
techNode.partsPurchased.Add(part);
ResearchAndDevelopment.Instance.SetTechState(techNode.techID, techNode);
}
}

Link to comment
Share on other sites

I think the problem you're running into is the old Reference vs. Value problem. Short version is, I think the 'ResearchAndDevelopment.Instance' is just returning a reference to the data you're trying to save, a memory pointer. Saving the reference doesn't do what you're hoping to, since someone else can change the values in that area of memory. You'll need to dig through that object to find & copy all the values you're interested in (and set those values back when you want to restore).

Link to comment
Share on other sites

R&D.Instance might not be available until a few frames have passed, so work in some way of waiting for it to exist before trying to use it. If you're really lucky, you might be able to get the game to do your heavy lifting for you by calling R&D.OnLoad/OnSave yourself. Otherwise, here's an example snippet that unlocks any parts that haven't been "bought" in tech nodes that have been unlocked. It might give you some ideas:

foreach (var part in PartLoader.LoadedPartsList)
{
var techNode = ResearchAndDevelopment.Instance.GetTechState(part.TechRequired);

if (techNode != null)
if (ResearchAndDevelopment.GetTechnologyState(techNode.techID) == RDTech.State.Available)
if (!ResearchAndDevelopment.PartModelPurchased(part) && !techNode.partsPurchased.Contains(part))
{
techNode.partsPurchased.Add(part);
ResearchAndDevelopment.Instance.SetTechState(techNode.techID, techNode);
}
}

Thanks for your reply xEvilReeperx. As I not only need to purchase the parts but also really unlock the tech nodes it is not enough to just iterate through the parts and purchase them. So I will try to use the OnLoad and OnSave methods. However the methods take a ConfigNode as parameter. Can you give me a hint on how to call the methods and where to get the ConfigNode parameter from? Unfortunately I'm not a very experienced programmer...

Link to comment
Share on other sites

I think the problem you're running into is the old Reference vs. Value problem. Short version is, I think the 'ResearchAndDevelopment.Instance' is just returning a reference to the data you're trying to save, a memory pointer. Saving the reference doesn't do what you're hoping to, since someone else can change the values in that area of memory. You'll need to dig through that object to find & copy all the values you're interested in (and set those values back when you want to restore).

Okay now I understand why it was not working. However I can see the methods GetExperiment(string) and GetTechnologyState(string) but for setting the values there is only SetTechState(string, ProtoTechNode) but no mehtod for setting the experiments. So if there's someone who could tell me how to put an experiment into the R&D instance I would be very lucky

Link to comment
Share on other sites

Okay now I understand why it was not working. However I can see the methods GetExperiment(string) and GetTechnologyState(string) but for setting the values there is only SetTechState(string, ProtoTechNode) but no mehtod for setting the experiments. So if there's someone who could tell me how to put an experiment into the R&D instance I would be very lucky

You're looking for ScienceSubjects, data that comes as a result of performing experiments

Link to comment
Share on other sites

You're looking for ScienceSubjects, data that comes as a result of performing experiments

Well I know I need ScienceSubject. It is also returned from R&D's GetExperiment() method. But my question is how to restore a ScienceSubject once I have saved it

Link to comment
Share on other sites

Well I know I need ScienceSubject. It is also returned from R&D's GetExperiment() method. But my question is how to restore a ScienceSubject once I have saved it

GetExperiment() returns ScienceExperiment. You could use the ScienceExperiment to get a ScienceSubject related to a particular experiment, but your case doesn't seem interested in specific subject + experiment + situation + body combinations.

SubmitScienceData seems likely to me. Assuming OnSave/OnLoad didn't work for you?

Link to comment
Share on other sites

Can you give me a hint on how to call the methods and where to get the ConfigNode parameter from? Unfortunately I'm not a very experienced programmer...
GetExperiment() returns ScienceExperiment. You could use the ScienceExperiment to get a ScienceSubject related to a particular experiment, but your case doesn't seem interested in specific subject + experiment + situation + body combinations.

SubmitScienceData seems likely to me. Assuming OnSave/OnLoad didn't work for you?

Well I didn't try OnSave/Load. As I already said I don't know how to call the methods. It would be nice if you could explain it to me :)

Link to comment
Share on other sites

Well I didn't try OnSave/Load. As I already said I don't know how to call the methods. It would be nice if you could explain it to me :)

OnSave/OnLoad seem to work, you should use those (after modifying R&D). Use OnLoad with an empty ConfigNode to load an initial state.

Well I didn't try OnSave/Load. As I already said I don't know how to call the methods. It would be nice if you could explain it to me :)

Most of the time you'll just have to experiment. I will help you on this one but that's it.

var saveRnd = new ConfigNode();
ResearchAndDevelopment.Instance.OnSave(saveRnd);
Debug.Log(saveRnd.ToString());
ResearchAndDevelopment.Instance.OnLoad(new ConfigNode()); // reset tech tree

Link to comment
Share on other sites

OnSave/OnLoad seem to work, you should use those (after modifying R&D). Use OnLoad with an empty ConfigNode to load an initial state.

Most of the time you'll just have to experiment. I will help you on this one but that's it.

var saveRnd = new ConfigNode();
ResearchAndDevelopment.Instance.OnSave(saveRnd);
Debug.Log(saveRnd.ToString());
ResearchAndDevelopment.Instance.OnLoad(new ConfigNode()); // reset tech tree

Worked perfectly. Thanks a lot!

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