Jump to content

Access existing instance of KSPAddon?


Recommended Posts

I have two KSPAddons in the same namespace. I need one to be able to talk to the existing instance of the other. I'm unclear on how KSP creates and manages instances of the Addons. Both are instantiated presumably by KSP at the SpaceCentre:

[KSPAddonFixed(KSPAddon.Startup.SpaceCentre, false, typeof(blah))]

I just don't know how to get the instance of one, from the other, so that I can make calls to it.

Link to comment
Share on other sites

If you use the public static bool like I was suggesting, since it's static, you can just do Classname.variablename from the other class. Static variables exist globally, not per class instance, so you don't have to get the instance.

Link to comment
Share on other sites

Either C# is very different from C++ in class vs instance concepts, or KSP is doing odd things.

My understanding of the process (and please correct me where I might be wrong) is something like this:

1) A class registers itself with whatever KSP uses to manage Addons, and specifies when it should be instantiated, and whether it should be destroyed after, or left alone.

2) When the proper scene happens, KSP Addon manager creates an instance of the registered class, and calls its Start() method. Start() is an instance method, not a class method.

That instance now exists and will stay (since I tell it to) until KSP exists. It will not get recreated again when the scene loads again. Now since it is an instance, how would another Class B's act of setting the static variable mean anything to the already existing instance?

Link to comment
Share on other sites

1) A class registers itself with whatever KSP uses to manage Addons, and specifies when it should be instantiated, and whether it should be destroyed after, or left alone.

Not quite. The "once" parameter of KSPAddon tells KSP whether to instantiate it every time that scene starts, or only for the first time. It will always destroy it when the scene ends. But you can use Unity's "GameObject.DontDestroyOnLoad(this);" in your constructor to prevent destruction. Then you also set your static class variable to "this", and use that from that point on.

Link to comment
Share on other sites

Not quite. The "once" parameter of KSPAddon tells KSP whether to instantiate it every time that scene starts, or only for the first time. It will always destroy it when the scene ends. But you can use Unity's "GameObject.DontDestroyOnLoad(this);" in your constructor to prevent destruction. Then you also set your static class variable to "this", and use that from that point on.

Thank you for correcting me on that. That is a very important difference.

Link to comment
Share on other sites

I'd suggest leaving at as start once = false, and then have a public static bool notFixed = true

then in the Start(), do if(notFixed) { notFixed = false; reload; }

Then in the Switcher code, whenever it switches, do SpaceCenterFixer.notFixed = true;

(or whatever your class name was; I forget >.> )

Link to comment
Share on other sites

I'd suggest leaving at as start once = false, and then have a public static bool notFixed = true

then in the Start(), do if(notFixed) { notFixed = false; reload; }

Then in the Switcher code, whenever it switches, do SpaceCenterFixer.notFixed = true;

(or whatever your class name was; I forget >.> )

Yep this is pretty much exactly what i'm doing now that I understand where my misunderstanding was.

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