Jump to content

KSPAddon.Startup option for the mission builder scene?


Recommended Posts

Hello.

I'm currently trying to figure out how to get set a KSPAddon.Startup working for the mission builder scene. As far as I can tell, none of the options available works for this, except for KSPAddon.EveryScene. However this one for some reason acts very strange, where it runs multiple times in a row on the same scene load.

With a quick test like this:

[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class HUDReplacerMissionBuilder : MonoBehaviour
{
	public void Awake()
	{
		Debug.Log("HUDReplacer: Scene switch test: " + HighLogic.LoadedScene);
	}
}

You'll see in the log that this was fired twice for the SPACECENTER scene:

[LOG 22:53:28.641] [HighLogic]: =========================== Scene Change : From MAINMENU to SPACECENTER (Async) =====================
[LOG 22:53:28.848] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:53:28.848] HUDReplacer: Scene switch test: SPACECENTER
[LOG 22:53:28.848] [UIMasterController]: HideUI
[LOG 22:53:29.556] [AddonLoader]: Instantiating addon 'CustomConfigsManager' from assembly 'ModuleManager'
[LOG 22:53:29.558] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:53:29.558] HUDReplacer: Scene switch test: SPACECENTER

Moreso loading into the VAB it'll fire off FOUR times:

[LOG 22:54:51.467] [HighLogic]: =========================== Scene Change : From SPACECENTER to EDITOR (Async) =====================
[LOG 22:54:51.611] [UIApp] OnDestroy: AlarmClock
[LOG 22:54:51.612] ScaleModList: listSize 41 maxListSize 3233
[LOG 22:54:51.612] [UIApp] OnDestroy: KSPedia
[LOG 22:54:51.612] ScaleModList: listSize 41 maxListSize 3233
[LOG 22:54:51.680] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:54:51.681] HUDReplacer: Scene switch test: EDITOR
[LOG 22:54:51.681] [UIMasterController]: HideUI
[LOG 22:54:52.399] UICanvasPrefabSpawner SceneLogic spawning Editor
[LOG 22:54:52.421] No Input Locks in effect right now
[LOG 22:54:52.492] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:54:52.492] HUDReplacer: Scene switch test: EDITOR
[LOG 22:54:54.275] [UIMasterController]: HideUI
[LOG 22:54:54.584] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:54:54.584] HUDReplacer: Scene switch test: EDITOR
[LOG 22:54:54.585] [UIMasterController]: ShowUI
[LOG 22:54:54.738] [AddonLoader]: Instantiating addon 'HUDReplacerMissionBuilder' from assembly 'HUDReplacer'
[LOG 22:54:54.738] HUDReplacer: Scene switch test: EDITOR

 

This doesn't happen using any of the explicit options of KSPAddon.Startup such as GameScenes.SpaceCentre, though none of these fires off at the mission builder scene. Unless I'm missing something obvious, how would I go about getting one that's only targetting the mission builder?

Link to comment
Share on other sites

  • 1 month later...

My guess is that KSPAddon.Startup.EveryScene may be equivalent to all of the explicit options of KSPAddon.Startup, among which there are four that specify "VAB":

  • EditorAny (both VAB and SPH)
  • EditorVAB (only VAB)
  • Two more (EditorFlightAndTracking or similar names)

Possible fix: create a timer.
Whenever your function that runs on scene change is called, check if a certain amount of time (like 2 sec.) has elapsed since last time the timer was reset; if it is the case then it's likely that it's the first of the (possibly many) calls to the function on this scene change. So just reset the timer (so it is at 0 sec.). If it is less than 2 sec. or something, then don't do anything.

Link to comment
Share on other sites

17 minutes ago, Nazalassa said:

My guess is that KSPAddon.Startup.EveryScene may be equivalent to all of the explicit options of KSPAddon.Startup, among which there are four that specify "VAB":

  • EditorAny (both VAB and SPH)
  • EditorVAB (only VAB)
  • Two more (EditorFlightAndTracking or similar names)

Possible fix: create a timer.
Whenever your function that runs on scene change is called, check if a certain amount of time (like 2 sec.) has elapsed since last time the timer was reset; if it is the case then it's likely that it's the first of the (possibly many) calls to the function on this scene change. So just reset the timer (so it is at 0 sec.). If it is less than 2 sec. or something, then don't do anything.

Hmm it does make sense that this is the case why different scenes call the startup method varying amount of times, but it also doesn't seem like this should be the correct behavior? How many modders don't know about this and are accidentally initializing their mods more than once for no reason? Does this contribute to the longer and longer scene loading times we experience due to an increase in number of mods?

The other problem is that MissionBuilder never really got its own KSPAddon.Startup option, since it was added in a dlc later on. It seems a bit strange that I have to reference a startup option that runs in several different scenes, just to make a check for this specific one, and not only that also have to create a workaround for the "random amount of initializations."

There used to exist a sort of solution for these problems back in 2014. Someone made an extension to KSPAddon to handle scenes as bitflags. Unfortunately it was never updated to recent versions of KSP, but it would be interesting to rework this and add in the MissionBuilder scene as well.

 

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