The tutorials on the official Kerbal Space Program wiki for setting up Visual Studio and making a simple mod are a bit vague in some areas. Here’s my take on making a mod in Kerbal Space Program. Anyone that knows how to edit the official wiki tutorial is more than welcome to steal anything below.
Acquire and Install Visual Studio 2012 Express
If you’re running Windows 7 and don’t have Visual Studio installed yet, go here: http://www.microsoft.com/visualstudio/eng/downloads#d-express-windows-desktop
Click on “Install Now”. It will install and ask for a reboot. You can run it in trial mode for some amount of time (30 days?) but it’s super easy to just register an account and get a license key. The Express version of Visual Studio is free for personal/student use. It has a few restrictions, like you can’t compile 64-bit applications, so catch up on that if you care to. There have been past versions of Visual Studio Express, but not all of them had the ability to create programs in C#, so if you’re using an existing install, check to make sure you can create C# programs.
Create a New Visual C# Class Library Project
Open Visual Studio and go to File->New Project and create a new Visual C# project. I named mine HelloKerbal.
If all goes well, you’ll have a new project ready with a default class called class1.
Add References to the Project
This mod requires us to be able to reference the Kerbal API (and some parts of the Unity API, too, I think). To do this, you need to add two .dll files as references in Visual Studio. There’s a pane called “Solution Explorer” that’s (by default) on the right. Right-click on “HelloKerbal” and click on “Add Reference”.
When the Reference Manager pops up, click on “Browse” on the bottom.
Navigate to your Kerbal Space Program install directory, and go to the “KSP_Data” directory, and then the “Managed” directory. I bought Kerbal Space Program through Steam, so the .dlls are in “C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP_Data\Managed”.
Add “Assembly-CSharp.dll” and “UnityEngine.dll” as references. You can control-right-click both .dll files to add them both at the same time.
Click “Add”, verify in the Reference Manager they’re both there, then click “Ok”.
Create a Subclass of PartModule and Say Hello
Alight, it’s time to modify the default code that Visual Studio created for us. Change the default Class1 to HelloKerbal, and set it to be a subclass of the PartModule class that’s defined in one of the .dll files that we’re referencing.
It’s a good idea (sometimes but not always) to rename the source code file to the name of the class that’s being defined in it. In the “Solution Explorer”, right click on “Class1.cs” and rename it to “HelloKerbal.cs”.
This is a convenient thing to do, since it will result in the .dll being built to be called HelloKerbal.dll instead of Class1.dll. You should see something like this after renaming.
The HelloKerbal class inherits from the PartModule class, which has a method called OnStart that we’ll redefine (override) to suite our nefarious purposes. As far as I can tell, OnStart is a method that’s called when a part is added to your spacecraft. So, we want to redefine the OnStart method to include a print statement that will output “Hello, Kerbal!” to the debug log.
I’m not super familiar with C#, but I think that this is the correct way to go about this.
Compile the Class Library and Copy to Plugins Directory
Hit F7, or go to “Build->Build Solution”. The “Output” pane will show that you’ve built a .dll in the “Debug” folder under your project folder.
Navigate to this directory in Windows Explorer, copy the HelloKerbal.dll file, and place it in the Kerbal Space Program plugins directory. Mine is here: “C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\Plugins”.
Create a Copy of an Existing Part to Use New Behavior
This mod creates a simple behavior for a rocket part. It’s beyond the scope of this tutorial to create a brand new part, so we’re going to make a copy of an existing one and point it towards this mod as its defined behavior (just like the Wiki tutorial does). Hopefully the original creator of this part won’t mind.
Go to the “Parts” directory in your Kerbal Space Program install location. Mine is in “C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\Parts”.
Scroll down to the “RCS block” directory, select it, and hit Ctrl-C, and then Ctrl-V. You should see a copy of the “RCS block” directory called “RCS block – Copy”.
Go into the “RCS block – Copy” directory and open “part.cfg” in Notepad. There is really only one part of the config file that needs to be edited so that it points to the function in HelloKerbal.dll, but for convenience we’ll edit the title of the part, too, so that it’s obvious which one it is in the rocket building interface.
Edit the “rcs module parameters” section at the bottom of the config file to say “name = HelloKerbal”.
Edit the “editor parameters” section of the config file to say “title = RV-105 COPY”.
Save the config file.
Activating and Observing the Mod Behavior
Start Kerbal Space Program, and get into the Vehicle Assembly Building. Pop open the debug console by pressing alt-F2. This is where we’ll see the mod behavior in a moment.
The copied part is under the “Control” category of parts, so you’ll need to plop down a cockpit or load an existing rocket to get access to it. Go to the Control category and observe that there are two identical looking parts, and that one of them named “RV-105 COPY”.
All that’s needed to initiate the behavior we defined is to click on the modified part. Click it, and you’ll see “Hello, Kerbal!” printed out in the debug console.
Discussion and Some Questions
I’m not at all familiar with the Unity framework, so I don’t know if the OnStart function is a Unity thing, or something unique to Kerbal Space Program. I could only get the OnStart function to fire off when I clicked on the part in the editor. There is some documentation on the class methods here: http://wiki.kerbalspaceprogram.com/wiki/API:PartModule
Much of my confusion with the tutorial on the wiki stemmed from not understanding that the mod would be tied to a part. Is this always the case that a mod has to be tied to a part?
There seems to be a lot of discussion on the official forums, so I guess I’ll have to keep digging for some answers. I’d like to also try creating my own part model so that I don’t have to borrow someone else’s existing work, but I’m not sure if I can do that with just Blender. I keep seeing references to having to do things in Unity (which is a framework AND an editor, I guess?).
When I edited the config file’s Module name field to say “HelloKerbal”, did that point it to the .dll file itself, or the class inside the .dll? I assume the latter. What implication does this have for mod code organization?
Well, whatever I learn I’ll try to write about. I can see myself taking a break from Kerbal Space Program after I inevitably burn out, so I’d like to bang out some tutorials so that my future self won’t have a tough time remembering how to do things once I pick it back up!