Jump to content

[SOLVED] OnSave(ConfigNode node) hangs the game


Recommended Posts

Edit: Solved problem thanks to cybutek - Simple try-catch made it work flawless for some reason (probably null value detection was wrong)

Hi,

I've got a problem with my OnSave function - if it's not commented out, it hangs the game on going to flight scene or going to space center.

I never really did anything that included saving or loading data (except some one-line something.Load(string file) stuff), and I have no idea why it fails.

resNames is a List<string> of part resource names, and this code is supposed to save and load the part resource's amount value 'cause automatic persistence doesn't work for them for some reason. If I comment out OnSave, OnLoad works, so OnSave has to be wrong.

    public override void OnSave(ConfigNode node)
{
if (!HighLogic.LoadedSceneIsFlight) return;
foreach (string r in resNames)
if(part.Resources[r] != null)
node.AddValue("Res", r + ";" + part.Resources[r].amount);
}
public override void OnLoad(ConfigNode node)
{
if (!HighLogic.LoadedSceneIsFlight) return;
string[] resData = node.GetValues("Res");
foreach (string r in resData)
{
string[] s = r.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
part.Resources[s[0]].amount = float.Parse(s[1]);
}
}


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

class ModuleKrResourceTank : PartModule
{
[KSPField(guiActive = true, guiName = "Resource mass", guiFormat = "F3", guiUnits = "t")]
public float resourceMass;
[KSPField(guiActive = true, guiName = "Total mass", guiFormat = "F3", guiUnits = "t")]
public float totalMass;
[KSPField]
public string resources = "";
protected List<string> resNames;
protected Dictionary<String, VInfoBox> resIndicators;

/// <summary>
/// Initializes values
/// </summary>
/// <param name="state"></param>
public override void OnStart(PartModule.StartState state)
{
resNames = new List<string>(resources.Split(';'));
resIndicators = new Dictionary<string, VInfoBox>();
foreach (string r in resNames)
print("resname:" + r);
}

/// <summary>
/// Updates context menus and info boxes
/// </summary>
void Update()
{
if (!HighLogic.LoadedSceneIsFlight) return;

resourceMass = part.GetResourceMass();
totalMass = resourceMass + part.mass;
foreach (string r in resNames)
{
if (part.Resources[r] == null) return;
if (part.Resources[r].amount > 1e-4)
{
if (!resIndicators.ContainsKey(r))
{
resIndicators.Add(r, part.stackIcon.DisplayInfo());
resIndicators[r].SetMessage(r);
resIndicators[r].SetMsgBgColor(Color.clear);
resIndicators[r].SetMsgTextColor(Color.white);
resIndicators[r].SetProgressBarBgColor(Color.clear);
resIndicators[r].SetProgressBarColor(Color.white);
}
resIndicators[r].SetValue(part.Resources[r].amount / part.Resources[r].maxAmount);
}
else if (resIndicators.ContainsKey(r))
{
part.stackIcon.RemoveInfo(resIndicators[r]);
resIndicators.Remove(r);
}
}
}

public override void OnSave(ConfigNode node)
{
if (!HighLogic.LoadedSceneIsFlight) return;
foreach (string r in resNames)
if(part.Resources[r] != null)
node.AddValue("Res", r + ";" + part.Resources[r].amount);
}
public override void OnLoad(ConfigNode node)
{
if (!HighLogic.LoadedSceneIsFlight) return;
string[] resData = node.GetValues("Res");
foreach (string r in resData)
{
string[] s = r.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
part.Resources[s[0]].amount = float.Parse(s[1]);
}
}
}
using System;

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