Difference between revisions of "Tutorial:Creating your first module"
(Created page with "So you've got your IDE all set up. Now what? The next step involves coding your first part module. Quit shaking, it's actually very simple to add things to KSP, and won't t...") |
(No difference)
|
Revision as of 19:48, 28 February 2012
So you've got your IDE all set up. Now what?
The next step involves coding your first part module. Quit shaking, it's actually very simple to add things to KSP, and won't take you very long to complete.
This tutorial assumes you're using Visual Studio 2010, but any IDE will do; Adapt accordingly!
First Things First
You've managed to slog through getting your IDE set up, and you've got an empty class open in front of you with a pretty uncreative name.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyKSPProject { public class Class1 { } }
Because Unity doesn't like namespaces very much, we're going to get rid of the namespace around the class.
We're also going to rename the class to something nice and creative, like TestModule. As a matter of good practices, remember to rename the file the class is in! (In VS2010, right-click the file and choose Rename.)
After compulsively re-indenting (VS2010: CTRL+E, D) it looks like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class TestModule { }
Awesome, now we can move on to the good stuff.
The Good Stuff
Extending Part
Part Modules all have one thing in common: They extend the Part class. The Part class defines a bunch of easy to use and intuitive methods and properties which you must override to do things with your module.
So, let's go ahead and extend Part. You don't need to add any using statements; It's in the global namespace as long as you have your KSP DLLs referenced correctly. Note that, again, compulsive cleanup was done.
DO NOT ADD A CONSTRUCTOR, KSP DOESN'T LIKE CONSTRUCTORS!
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class TestModule : Part { }
Showing Up to Class
When a part module is first loaded, Part.onPartStart() is called. Here, we can set up low-level things like icons.
So, let's go ahead and override onPartStart. You don't need the base.onPartStart() stuff, so let's toss that and add some documentation like good programmers do. The override prefix isn't necessary, but it's good practice and will throw compile errors in case HarvesteR moves things around.
using System; using System.Collections.Generic; using System.Linq; using System.Text; /// <summary> /// My first part! /// </summary> public class TestModule : Part { /// <summary> /// Called when the part is started by Unity. /// </summary> protected override void onPartStart() { } }
So we've got a method now, but it doesn't really do anything. So, let's just print something silly to the log.
using System; using System.Collections.Generic; using System.Linq; using System.Text; /// <summary> /// My first part! /// </summary> public class TestModule : Part { /// <summary> /// Called when the part is started by Unity. /// </summary> protected override void onPartStart() { // Add stuff to the log print("something silly to the log"); } }
Cool! Now when we compile the DLL and toss it into Plugins/ and load the game, we can see something silly in our KSP_Data/output_log.txt!