Jump to content

Partmodule OnSave/Load timing vs. scene existence


Recommended Posts

Alright, I have a somewhat nebulous problem I've been struggling with for a few days.

This has to do with saving data to a part module from code that runs in a scene.

This is for the action groups mod I am trying to finalize but I'm seeing two symptoms that I can't nail down the cause for.

1) Action groups just vanish. Sometimes my test vessel will lose a configured action, but only one. It has 10 actions configured, I'm come back to in later and there is now only 9 actions configured.

2) Random null refs. I'll do something 4 times and it works fine, the 5th causes a null ref error. I reload without changing a thing and everything works fine again.

Having banged my head against this wall for too long, my best guess is that I'm running into issues where the Partmodule OnSave/OnLoad runs on it's own time and not limited by the Scene in anyway.

All my code for actually doing stuff runs in my KSPAddon.Startup.Flight (or .EditorAny), I'm just using the Partmodule (and its OnSave/OnLoad) for data storage.

So my best guess is that most of the time, my code in KSPAddon.Flight OnStart runs before the PartModule's OnLoad so the data transfers correctly, but every so often this happens the other way around and I 'lose' data because the OnLoad runs before my Flight Scene OnStart code.

When I started this, I understand that code runs sequentially, so I wrote the version I am currently struggling with under the assumption that my Flight Scene OnStart code would always execute before any Partmodule's OnLoad would, but it is starting to look like that is not the case.

Does anyone have experience with something similar that they could offer me advice?

I'm starting to fear I'm going to have to break my code up and have only the GUI running in KSPAddon.Flight and all my calculations running in the PartModule's code.

Thoughts?

D.

Link to comment
Share on other sites

Does it always run OnSave/OnLoad, even when you are first launching from inside the VAB/SPH? I've never really checked, but I'm assuming it does.

I had some issues with the both OnSave and OnLoad being called every time you load, it was screwing up some confignodes. Just using debug printout I saw that OnSave was always being called after OnLoad, usually separated by quite a bit of other entries. I didn't pay attention to OnStart, but I'm fairly sure it was always being called before OnSave.

So maybe, if you only need to run something once, you could do it in OnUpdate, and put a bool check on the code. Set the bool to true in OnSave (which should be happening long after everything else), then set it to false after everything in OnUpdate runs. Would that work, or would that cause issues later on with OnSave when you're actually saving? Or maybe set the bool to true in the KSPAddon OnStart.

Link to comment
Share on other sites

What I think I'm going to have to do is make an OkayToSave bool that is set true once everything is loaded, then error trap the Partmodule's OnSave so that if OkayToSave is false, the OnSave code does not run.

I'm actually trying to do this with my current setup of the code running in the Flight OnStart and it does work most of the time.

I'm starting to think that my data storage strategy is simply flawed. I originally started with no code at all in the PartModule, it was simply 2 strings for data storage and everything, including save/load, would happen in the Flight (or Editor) module.

Then once I got into it and realized I needed the automatic save that the PartModule's OnSave grants, I move some of my save/load code to the PartModule, but some stayed in the Flight Module. This is the disconnect I think is causing my problems, the save/load code being split into two different places.

I think I need to go all the way and move all the save/load code to the PartModule and have the Flight Module code be only the GUI, calling data from the PartModules as needed.

I don't want to do this as I am yet again tearing apart pretty much then entire backend of my mod, but I simply can't get my current setup stable enough to release.

D.

Link to comment
Share on other sites

When I started this, I understand that code runs sequentially, so I wrote the version I am currently struggling with under the assumption that my Flight Scene OnStart code would always execute before any Partmodule's OnLoad would, but it is starting to look like that is not the case.

The PartModule Save/load code will run sequentially but you can't count on the vessel being loaded on the first frame. Putting all your save/load code in one place is the way to go regardless, in my opinion.

There's also FlightGlobals.ready that you can check before messing with the vessel

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