Jump to content

ApplicationLauncher mod button with KSP 1.2


Recommended Posts

My code seemed to be working fine for KSP 1.1, but now the stock app toolbar stuff is all wonky. Buttons are appearing in scenes that they shouldn't and stuff like that.

Code for KSP 1.1:

// a MonoBehavior class

public void Awake()
{
    appButton = ApplicationLauncher.Instance.AddModApplication(
        ButtonPressed,
        ButtonReleased,
        null,
        null,
        null,
        null,
        ApplicationLauncher.AppScenes.MAPVIEW | ApplicationLauncher.AppScenes.TRACKSTATION,
        GameDatabase.Instance.GetTexture("PATH_GOES_HERE", false)
        );
}

 

From what I understand, the ApplicationLauncher should do all the showing/hiding based on the AppScene parameter. This worked fine for 1.1, but in 1.2 it's showing the button in scenes where it doesn't belong, including the main menu sometimes.

I looked through the forums and other source code, and it looks like people are using event callback functions from the application launcher to add and remove their buttons based on those. So I changed my code to this:

//a MonoBehavior class
public void Awake()
{
	GameEvents.onGUIApplicationLauncherReady.Add(AddAppButtons);
    GameEvents.onGUIApplicationLauncherDestroyed.Add(RemoveAppButtons);
}

public static void AddAppButtons()
{
	if (ApplicationLauncher.Ready)
    {
        appButton = ApplicationLauncher.Instance.AddModApplication(
            ButtonPressed,
            ButtonReleased,
            null,
            null,
            null,
            null,
            ApplicationLauncher.AppScenes.MAPVIEW | ApplicationLauncher.AppScenes.TRACKSTATION,
            GameDatabase.Instance.GetTexture("PATH_GOES_HERE", false)
            );
    }
}

public static void RemoveAppButtons()
{
	ApplicationLauncher.Instance.RemoveModApplication(appButton);
    Destroy(appButton);
}

 

Something like that. I tried to mimic what I saw.

I don't understand why you'd have to manually add and remove buttons, I thought the point of the ApplicationLauncher was to remove that kind of tedium. I also can't find any documented changes made going into 1.2 with the way the ApplicationLauncher works, so I'm thoroughly confused. Maybe my code before was flawed and it just happened to work anyway. Any help would be greatly appreciated!

 

Update

So it looks like the callback functions can't be static or else it throws a null pointer exception and nothing happens. I made the callback functions non-static, but I'm still having MAJOR issues getting my button to NOT appear in scenes where it doesn't belong. I moved my code around, and now it looks like this:

[KSPAddon(KSPAddon.Startup.AllGameScenes, false)]  //Does this need to be something specific? Removing it seems to make it never load
class MyClass : MonoBehavior
{
	ApplicationLauncherButton button = null;

	protected virtual void Awake()
	{
		GameEvents.onGUIApplicationLauncherReady.Add(AppLancherReadyCallback);
        GameEvents.onGUIApplicationLauncherUnreadifying.Add(AppLauncherUnreadifyingCallback);
	}

	protected virtual void OnDestroy()
	{
		GameEvents.onGUIApplicationLauncherReady.Remove(AppLancherReadyCallback);
        GameEvents.onGUIApplicationLauncherUnreadifying.Remove(AppLauncherUnreadifyingCallback);
	}

    public void AppLancherReadyCallback()
    {
		if (ApplicationLauncher.Ready)
		{
            button = ApplicationLauncher.Instance.AddModApplication(
                ButtonPressed,
                ButtonReleased,
                null,
                null,
                null,
                null,
                ApplicationLauncher.AppScenes.MAPVIEW | ApplicationLauncher.AppScenes.TRACKSTATION,
                icon
            );   
		}
    }

    public void AppLauncherUnreadifyingCallback(GameScenes scene)
    {
        if (ApplicationLauncher.Instance != null && button != null)
        {
        	ApplicationLauncher.Instance.RemoveApplication(appButtonBiomeOverlay);
        }
    }
}

At first it seems to work (no button shows up until I'm either in the tracking station or map view in flight), but when I return to the main menu, and sometimes when I go to the space center, the button shows up. Not always, just under specific circumstances. I'm very confused and getting pretty frustrated that I can't get a simple application launcher button to behave like it used to before the update.

Edited by Wheffle
Link to comment
Share on other sites

  • 2 weeks later...
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...