Search the Community
Showing results for tags 'scenariomodule'.
-
Hi! This is my first post to this forum, so I hope everything is alright. I've been searching here and on Google for a week and hit an enormous amount of dead ends. As the title says, I've hit a wall with the KSPScenario system, trying to save/load data from my mod. I have my save/load methods properly overridden in a ScenarioModule tagged with the KSPScenario attribute. I have a static Dictionary in another class with the data I want to save. Beyond there is where the problems begin. My custom PartModule has to do some math upon startup to deduct resources. Initially I figured, great, I'll do it in OnStart. However, the Scenario Module always runs OnLoad after the PartModule's OnStart, loading data into memory from BEFORE the update. Here is the current flow: -- Flight Scene is loaded -- PartModule.OnStart(..) Check tracking info, which is already in memory, in State 1 Update tracking to State 2 ScenarioModule.OnLoad(..) Loads tracking info still in State 1 - correct data is overridden! ScenarioModule.OnSave(..) PartModule.FixedUpdate(..) Sometimes starts as early as before ScenarioModule.OnLoad(..) PartModule.OnStart takes the initial values (State 1), updates them (State 2), and then returns. When ScenarioModule.OnLoad is called, it overwrites the data, putting it back in State 1. Ideally OnLoad would only trigger when loading from a persistence file, but I haven't found a way to control for that. I have tried an enormous amount of workarounds, including: Manually invoke ScenarioModule.OnSave(..) at the end of OnStart with a modified, correct ConfigNode. OnLoad still loaded the stale data. Move OnStart logic to OnInitialize. This worked when loading a vessel already in flight, although OnInitialize started an indeterminate time after FixedUpdate began iterating. The problem arose when creating a new vessel (i.e. vessel rollout). There, OnInitialize was triggered so EARLY that the PartModule's reference to vessel was null. Try manually writing/loading to persistent.sfs at the end of OnStart, similar to item #1. Made me uneasy, but no matter - it didn't work anyway. Try waiting for FlightGlobals.ready before doing the initialization logic. This flag sometimes triggered before OnLoad, and sometimes after. Block the loader and/or the game from proceeding until the other had reached an acceptable point. These worked in a vacuum, but are impossible to get working when multiple vessels are active, for the simple reason that I cannot tell whether OnLoad is simply loading from a scene change, or from a user-instigated persistence file load. Manually modify the ScenarioModule's ConfigNode (via snapshot.GetData()). Tweak the KSPScenario attributes. I've tried just SpaceCenter, just PSystem (OnLoad/Save never fired), and the full collection of in-game scenes (SC, Tracking, Flight). The problem is that if I remove some of these scenes, then it will not load, but it also will not save. This was easily tested by trying to quicksave, then checking the log file. Throw an exception in OnLoad to manually check the stack track for "GamePersistence.LoadGame." This would work for OnSave (which I don't need to touch), but for OnLoad the source is ScenarioRunner.AddModule every time. ...and more How can I save data to the persistence file (using a ScenarioModule, I assume) without it being overwritten? I've tried so many things at this point, I'm starting to go crazy. It doesn't seem like anything involving the load sequence is deterministic. I also suspect (from the aforementioned stack traces) that the game is actually adding a new copy of the scenario to every scene via ScenarioRunner.AddModule. Even if I just specify one scene (e.g. GameScenes.SPACECENTER), it does this every time I load the SpaceCenter. Every post I've found says ScenarioModules are the way to go if saving data to the persistence file, but it seems like such a convoluted way to operate. On the other hand, I used to be load/saving via GameEvents.OnGameStateSaved.Add(..), and I suspect I'd have the same problems there. ...Bottom line is, I'm just looking to save some data. Any feedback on how I can do that? Thanks!
- 6 replies
-
- scenariomodule
- saving
-
(and 1 more)
Tagged with: