Jump to content

GUI Window Saving To Config im Lost..


Recommended Posts

Been trying to make mission controllers windows be saved..

.. This method doesn't seem to work anymore I get a No Suitable Method To Overide on both OnSave and OnLoad.

This is the code that doesn't seem to work anymore.

public override void OnSave(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<MissionController>();
config.SetValue("MainWindow", mainWindowPosition);
config.save();
}

public override void OnLoad(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<MissionController>();
config.load();
mainWindowPosition = config.GetValue<Rect>("MainWindow");
}

Anyone have a better way of saving the windows config files.

For instance All I want to do is save the settings to start of for.

private Rect mainWindowPosition = new Rect (300, 70, 300, 690);

Or do I have to rewrite the way mission controllers GUI is handled? To support this?

If you wanna take a look at the code.. Its on Github

Thanks I figured after 3 days of trying to get this to work.. Its time to ask for help. ;)

Edited by malkuth
Link to comment
Share on other sites

I am using KSPAddon to start my plugin and don't have OnSave() so I just call this function every ten seconds from Update() to save my settings :)


private void save_plugin_settings()
{
plugin_settings_node = new ConfigNode();
plugin_settings_node.AddValue("main_window_pos", main_window_pos.x + "," + main_window_pos.y);
plugin_settings_node.AddValue("ui_icon_pos", ui_icon_pos.x + "," + ui_icon_pos.y);
plugin_settings_node.Save(path);
}

Link to comment
Share on other sites

A problem with KSPAddons is there is nothing telling you when to load or save, unlike PartModules which have the OnLoad and OnSave methods. Two options are:

  1. Implement OnDestroy (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDestroy.html). It is called whenever the GameObject your MonoBehaviour is attached to is destroyed, like when exiting the scene -- unless you used DontDestroyOnLoad. See an example here: https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/FuelBalanceController.cs#L78.
  2. Implement OnLevelWasLoaded (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html). It is called whenever the game changes scenes, i.e. from Flight to the Space Center.

Note that when "overriding" methods from MonoBehaviour, you do not use the override keyword. Unity does not use true overriding, but instead does some trickery with reflection.

Also note that neither solution works with Quicksave/Quickload. Do not save data that you expect to be loaded when the user quickloads. The only workaround I have found for that is to add a PartModule to the vessel so that it can notify me when the game is being loaded or saved. See https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/LifeSupportModule.cs#L50.

P.S. I recommend using ConfigNode (as Iannic-ann-od did) instead of PluginConfiguration.

Link to comment
Share on other sites

Well, when writing to a custom file anyway you should be able to listen to GameEvents.onGameStateSaved. That way you don't have to do it every few seconds. They sadly do not give you a reference to the current config node via those events, though, so we still have to save stuff via parts. HighLogic.CurrentGame.scenarios looks like another promising way to save some data, though definitively not easier than part modules. It is a shame they not simply pass along those config nodes on that events and let us save whatever we want :(

Link to comment
Share on other sites

Did finally get this to work.. Wohoo. :) What was holding me back at first was that I had the save always overwriting the load.. So it always reset everything to 0. hahaha.. Finally got it to load the way I wanted and well.. That was that.. :)

No I continue my search to find out how the heck I can tell when the Player Pushes the Recruit Applicant Button in the Astronaut complex.. :)

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