Jump to content

Recommended Posts

Hi

I'm working on getting AutoRove to work again, and I think I have the core up and running. But the stock toolbar is playing a trick on me.

I have succeeded in adding the button :)

But when game scene load, the button keeps replicating. So the question is, how can I make sure that I'm not creating the button if it already exists, or at least have it removed before the scene changes or something?

 

private static ApplicationLauncherButton appButton = null;

        private void createStockButton()
        {
            Texture icon = GameDatabase.Instance.GetTexture("AutoRove/appLauncherButton", false);
            ApplicationLauncher.AppScenes visibility = ApplicationLauncher.AppScenes.MAPVIEW | ApplicationLauncher.AppScenes.SPACECENTER | ApplicationLauncher.AppScenes.TRACKSTATION | ApplicationLauncher.AppScenes.FLIGHT;
            // registering the button
            if (ApplicationLauncher.Ready && appButton == null)
            {
                removeStockButton();
                ApplicationLauncherButton appButton = ApplicationLauncher.Instance.AddModApplication(toggleAppLauncher, toggleAppLauncher, null, null, null, null, visibility, icon);
            }
        }
        private void callRemoveStockButton(GameScenes scene) { removeStockButton(); }
        private void removeStockButton()
        {
            if (appButton != null)
            {
                ApplicationLauncher.Instance.RemoveModApplication(appButton);
                appButton = null;
            }
        }

        void Start()
        {
            GameEvents.onGUIApplicationLauncherReady.Add(new EventVoid.OnEvent(createStockButton));
            GameEvents.onGUIApplicationLauncherUnreadifying.Add(new EventData<GameScenes>.OnEvent(callRemoveStockButton));

            DontDestroyOnLoad(this);
        }

 

I found this, but it seems I'm missing something - /topic/78231-application-launcher-and-mods

Link to comment
Share on other sites

dont bother removing it and setting the reference to null

Simple toolbar template: https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Toolbar/AppLauncherFlight.cs

Called from: https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/PilotAssistantFlightCore.cs#L61-L64

Note how the button is only ever created once (when the static var is null), even though I can (and do) call create a number of times

Link to comment
Share on other sites

12 hours ago, Crzyrndm said:

dont bother removing it and setting the reference to null

Simple toolbar template: https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Toolbar/AppLauncherFlight.cs

Called from: https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/PilotAssistantFlightCore.cs#L61-L64

Note how the button is only ever created once (when the static var is null), even though I can (and do) call create a number of times

Thanks...

I will try that.

Link to comment
Share on other sites

The adding and removing of the button itself is pretty trivial. To add:

if (ApplicationLauncher.Instance != null)
	appButton = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, OnHover, OnHoverOut, Enable, Disable,
		ApplicationLauncher.AppScenes.<TheScene>, appButtonTexture);

To remove:

if (ApplicationLauncher.Instance != null && appButton != null)
	ApplicationLauncher.Instance.RemoveModApplication(appButton);

But the trick is WHEN to add and to remove. And there you have two solutions based on whether your plugin is persistent or not. If it's

[KSPAddon(KSPAddon.Startup.<Whatever>, false)]

That means the plugin is NOT persistent, and is instantiated every time the relevant scene is loaded, and destroyed every time the scene is exited. In this case the good idea would be to create the button in Awake and remove it in Destroy:

public void Awake () {
  GameEvents.onGUIApplicationLauncherReady.Add(AddButton);
  GameEvents.onGUIApplicationLauncherUnreadifying.Add(RemoveButton);
}

public void OnDestroy() {
  GameEvents.onGUIApplicationLauncherReady.Remove(AddButton);
  GameEvents.onGUIApplicationLauncherUnreadifying.Remove(RemoveButton);
}

But if there is "true" in the persistent state, then it means that the plugin won't be created and destroyed every time the scene changes, and you should probably listen to the SceneChanged GameEvent.

Link to comment
Share on other sites

@Morse That is an awfully complex way to do it. You only ever need to load the buttons once, and you shouldn't need to ever destroy it.... You should be adding your buttons through the onGUIApplicationLauncherReady event, and add methods to this event in the main menu scene, a bit like what is done here. If you're removing adding it each time you enter each scene, you're disregarding the scene restriction parameter. Not only that, but the only time that event launches is in the main menu now.

Here I'm checking if the launcher is ready and if the button has been added, but this check is not necessary and only there as a "safety" in case something in KSP goes horribly wrong.

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