Jump to content

How to make a NON-PART plugin?


Recommended Posts

I`m thinking to make a mod that is didn`t use any parts such us:

Kerbal Alarm Clock, Mission Controller, Chatter or HyperEdit.

So my question to the modding community is:

How to start a non-part plugin?

How to create the GUI and windows for the plugin?

Any coding tutorial for KSP mods? Where should I look for tutorial or what to learn to make plugins?

What programs and skill required for this type of plugin?

What program do I use to create/edit the DLL file for the plugin?

Any suggestion, recommendation or help is welcome in this thread. :)

I`m a noob about programming! Please help!

Thanks to everybody who will write useful comments. :)

Edited by greg12
Link to comment
Share on other sites

First off, a few of you are being jerks and/or completely unhelpful. He asked specific questions. "Just learn C# and everything will come to you" is a terrible answer because it's both unhelpful and completely false.

There are several entry points for plugin code. You can extend Part or PartModule, which is useful if you're making a part with behavior; you can extend MonoBehaviour and attach a [KSPAddon] attribute, which allows your code to start on a certain scene; and you can extend ScenarioModule, which starts your code at configured scenes but also holds data in the persistence file.

Just to make sure. C# and C++ is the same.

Not at all. Despite the names, these two languages are rather different. C# is more similar to Java at the core, although some of the more advanced features of C# are reminiscent of C++.

Link to comment
Share on other sites

There are several entry points for plugin code. You can extend Part or PartModule, which is useful if you're making a part with behavior; you can extend MonoBehaviour and attach a [KSPAddon] attribute, which allows your code to start on a certain scene; and you can extend ScenarioModule, which starts your code at configured scenes but also holds data in the persistence file.

Maybe because this is my second language or I`m really noob about this or both but I don`t understand a word that you just said. No problem I will work on it and thanks about get me right about the C# and C++ bit.

Off topic: this plugin will have interesting effect on your and others mods too. Don`t worry, it will be no copyright issue. (I Love Kethane)

Link to comment
Share on other sites

Here's a starting point for the code itself:


[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class YourPlugin: MonoBehaviour
{
public void Awake() { }
public void Start() {
print("Holy Sh*t the plugin started correctly!!");
}
}

You create a class library (DLL) that includes subclass of MonoBehaviour, and put that in KSP's GameData directory (In this case, you'd call it "YourPlugin.dll", to match the name of the MonoBehaviour). From there, KSP automatically detects it, and will load it at the appropriate time based upon the settings you put in the attribute (KSPAddon.Startup.MainMenu, in this case. Other choices would include editor windows, in-flight, etc). Awake() is automatically called by KSP when the plugin is loaded, and Start() is called once the appropriate scene has been completely loaded, i.e. on the first execution tick.

You can see the output from this plugin by hitting ALT+F2 at the main menu to bring up the debug console. Scroll up a bit, and you'll find the text it printed.

Check out the source code in other mods to build a sense of how to work with Unity/KSP through scripts. You're welcome to browse through my code (included in the download: http://kerbalspaceprogram.com/dock-align-indicator/)

-NavyFish

Edited by NavyFish
Link to comment
Share on other sites

Here's a starting point for the code itself:


[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class YourPlugin: MonoBehaviour
{
public void Awake() { }
public void Start() {
print("Holy Sh*t the plugin started correctly!!");
}
}

You create a class library (DLL) that includes subclass of MonoBehaviour, and put that in KSP's GameData directory (In this case, you'd call it "YourPlugin.dll", to match the name of the MonoBehaviour). From there, KSP automatically detects it, and will load it at the appropriate time based upon the settings you put in the attribute (KSPAddon.Startup.MainMenu, in this case. Other choices would include editor windows, in-flight, etc). Awake() is automatically called by KSP when the plugin is loaded, and Start() is called once the appropriate scene has been completely loaded, i.e. on the first execution tick.

You can see the output from this plugin by hitting ALT+F2 at the main menu to bring up the debug console. Scroll up a bit, and you'll find the text it printed.

Check out the source code in other mods to build a sense of how to work with Unity/KSP through scripts. You're welcome to browse through my code (included in the download: http://kerbalspaceprogram.com/dock-align-indicator/)

-NavyFish

Thanks you NavyFish for this. I will check out you codes for inspiration and learning purposes. Yes just what I needed. A START point. a little push always good for beginners.

Also I thanks for everybody else who supported me with there knowledge on this thread. Maybe after a week or two, you will see that this project will light up the forum and (I hope) everyone will have a copy in there GameData folder.

For now I will have to take a good research, look through carefully everything and focus on learning C# for this plugin. If you guys have anymore of these good tips or these starting code sequences please let me know. I will keep my eye on this thread and come back about 2 times a day to see what we got. Thank you guys :D

Link to comment
Share on other sites

Here's a bit of an extended example for a simple GUI element that displays in the KSC scene, to help others get started:

Basically everything you need to know about making GUIs for KSP can be found here: Unity Scripting Reference


[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class YourPlugin: MonoBehaviour
{
private static Rect windowPosition = new Rect(0,0,320,240);
private static GUIStyle windowStyle = null;
private static boolean buttonState = false;

public void Awake() {
RenderingManager.AddToPostDrawQueue(0, OnDraw);
}

public void Start() {
windowStyle = new GUIStyle(HighLogic.Skin.window)
}

private void OnDraw(){
windowPosition = GUI.Window(1234, windowPosition, OnWindow, "Your Plugin", windowStyle);
{

private void OnWindow(int windowID){
GUILayout.BeginHorizontal();
GUILayout.Label("ABC-");
GUILayout.Label("123");
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
buttonState = GUILayout.Toggle(buttonState, "Button State: " + buttonState);
GUILayout.EndHorizontal();

GUI.DragWindow();
}
}

Awake() is called when the plugin is loaded, and Start() is called once everything in the scene (including other plugins) have loaded, just before the first execution tick. Inside of Awake() we add the "OnDraw" method to Unity's PostDrawQueue (You could name it whatever you want), which will automatically call it every frame. Inside of OnDraw(), we create the window, which returns its size and position as a Rect, i.e. when the user drags the window. This 'windowPosition' Rect is fed back into the GUI.Window method so that it remains wherever the user dragged it to. GUI.Window points to another method, OnWindow (again, you can name it whatever you want), which contains the window drawing code. From within OnWindow(), we use GUILayout to automatically arrange the various elements (i.e. labels, buttons, sliders, etc). Finally GUI.DragWindow() allows the window to be dragged (and should always come at the end of this function).

Again, check the Unity Scripting Reference linked above for all your GUI concerns!

Link to comment
Share on other sites

  • 4 weeks later...
  • 2 weeks later...

Wow! This really put me in the right direction. Now it'd be helpful if someone could assist in loading images into mods for GUI's. And maybe simple XML download and parsing on a separate thread, and giving the data back to the main thread. Yes, I'd be well on my way if those were solved.

Link to comment
Share on other sites

Don't think there is lot to tell about. Unity should have quite a few such tutorials floating around the web. Be aware through, that a separate thread is not allowed to touch any UnityEngine method. None! Not even a Debug.Log()!

Link to comment
Share on other sites

Wow! This really put me in the right direction. Now it'd be helpful if someone could assist in loading images into mods for GUI's. And maybe simple XML download and parsing on a separate thread, and giving the data back to the main thread. Yes, I'd be well on my way if those were solved.

For GUI images, read up on Unity's GUI textures. For reading from XML, see here.

Link to comment
Share on other sites

Well thanks! Next up, a bit of a big one: a community content repository. Basically an in-game Steam Workshop. Yes, I know, why not just ask the developers for integration? I just did, and there's a chance the Steamworks API couldn't download things from in-game like that. So... I looked around on how to create such a thing, and no one has a clue, it seems. I don't even know what the technical term for such a thing would be. Any ideas?

Link to comment
Share on other sites

Wow! This really put me in the right direction. Now it'd be helpful if someone could assist in loading images into mods for GUI's. And maybe simple XML download and parsing on a separate thread, and giving the data back to the main thread. Yes, I'd be well on my way if those were solved.

Can't help you on the XML thing, never even contemplated something like that myself, but for the images on the GUI, take a look at my post here.

It's got a code snippet in it where I do exactly that for the arrows on my touchscreen mod.

Kerbal Alarm Clock also does it in a slightly different way, but you'd have to look at the source on that one.

(I actually figured it out thanks to KAC and use a slightly modified method for loading images.)

D.

Link to comment
Share on other sites

  • 2 weeks later...
Just to make sure. C# and C++ is the same.

What program do you guys prefer. Microsoft Visual C++ 2012 ?

And how do I edit/ create the DLL files? What program do I need for that?

No. C++ is a different language than C#. A lot different. And KSP uses C#.

Link to comment
Share on other sites

  • 3 weeks later...
Here's a bit of an extended example for a simple GUI element that displays in the KSC scene, to help others get started:

Basically everything you need to know about making GUIs for KSP can be found here: Unity Scripting Reference


[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class YourPlugin: MonoBehaviour
{
private static Rect windowPosition = new Rect(0,0,320,240);
private static GUIStyle windowStyle = null;
private static boolean buttonState = false;

public void Awake() {
RenderingManager.AddToPostDrawQueue(0, OnDraw);
}

public void Start() {
windowStyle = new GUIStyle(HighLogic.Skin.window)
}

private void OnDraw(){
windowPosition = GUI.Window(1234, windowPosition, OnWindow, "Your Plugin", windowStyle);
{

private void OnWindow(int windowID){
GUILayout.BeginHorizontal();
GUILayout.Label("ABC-");
GUILayout.Label("123");
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
buttonState = GUILayout.Toggle(buttonState, "Button State: " + buttonState);
GUILayout.EndHorizontal();

GUI.DragWindow();
}
}

Awake() is called when the plugin is loaded, and Start() is called once everything in the scene (including other plugins) have loaded, just before the first execution tick. Inside of Awake() we add the "OnDraw" method to Unity's PostDrawQueue (You could name it whatever you want), which will automatically call it every frame. Inside of OnDraw(), we create the window, which returns its size and position as a Rect, i.e. when the user drags the window. This 'windowPosition' Rect is fed back into the GUI.Window method so that it remains wherever the user dragged it to. GUI.Window points to another method, OnWindow (again, you can name it whatever you want), which contains the window drawing code. From within OnWindow(), we use GUILayout to automatically arrange the various elements (i.e. labels, buttons, sliders, etc). Finally GUI.DragWindow() allows the window to be dragged (and should always come at the end of this function).

Again, check the Unity Scripting Reference linked above for all your GUI concerns!

For some reason this code ain't working, and it's not because of the typing mistakes. :/ The window is rendered onto the screen but it can't be dragged and the label text isn't there.

Link to comment
Share on other sites

Check the debug log by pressing F2 in-game. Scroll up a bit, you're probably getting a null exception error (it will display in red). When a script fails, it gets left in an awkward, half-finished state, which sounds like your description.

The other likely problem is that your OnWindow function does not have the exact same name as the 3rd parameter in GUI.Window.

Have you imported the unity DLLs and KSP DLL into your project? (ithout those I imagine the C# project wouldn't even compile, so I assume so, but you never know)

Link to comment
Share on other sites

Check the debug log by pressing F2 in-game. Scroll up a bit, you're probably getting a null exception error (it will display in red). When a script fails, it gets left in an awkward, half-finished state, which sounds like your description.

The other likely problem is that your OnWindow function does not have the exact same name as the 3rd parameter in GUI.Window.

Have you imported the unity DLLs and KSP DLL into your project? (ithout those I imagine the C# project wouldn't even compile, so I assume so, but you never know)

Thanks! Now I found the error. My "AddToPostDrawQueue" was replaced with "AddToPreDrawQueue". I had just clicked enter after typing some letters as MonoDevelop suggested something that looked to me like "AddToPostDrawQueue".

And yeah, I have imported the DLLs.

Link to comment
Share on other sites

  • 5 months later...

In case someone is looking for it, here is a afaik comlete list of KSPAddon.Startup-Settings:

[KSPAddon(KSPAddon.Startup.EditorAny, false)]
[KSPAddon(KSPAddon.Startup.Flight, false)]
[KSPAddon(KSPAddon.Startup.Instantly, false)]
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
[KSPAddon(KSPAddon.Startup.TrackingStation, false)]
[KSPAddon(KSPAddon.Startup.Editor, false)]
[KSPAddon(KSPAddon.Startup.SPH, false)]

I thought I just drop this here..

HTH

Edited by philotical
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

Can anyone help me programing a clickable button to do something (at the moment it should open a separate webpage) here is my code so far:

Code:


[KSPAddon(KSPAddon.Startup.EditorAny, false)]

public class Class1 : MonoBehaviour

{

private static Rect windowPosition = new Rect(0,0,300,50);

private static GUIStyle windowStyle = null;

public void Awake()

{

RenderingManager.AddToPostDrawQueue(0, OnDraw);

}

public void Start()

{

windowStyle = new GUIStyle(HighLogic.Skin.window);

}

private void OnDraw()

{

windowPosition = GUI.Window(1234, windowPosition, OnWindow, "Open BBC", windowStyle);

}

private void OnWindow(int windowID)

{

GUILayout.BeginHorizontal();

GUILayout.Label("Open BBC");

if(GUILayout.Button("GO"))

{

System.Diagnostics.Process.Start("http://www.bbc.co.uk");

}

GUILayout.EndHorizontal();

GUI.DragWindow();

}

}


At the moment when I go into ksp the window doesn't appear ;.;
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...