Tutorial:Understanding Addon Code

From Kerbal Space Program Wiki
Jump to: navigation, search
This page is an orphan, as no other articles link to it.

You're diving into KSP modding head first? A lot of mods do all sorts of amazing things that there are no guides for, so knowing how to copy what you need from someone who knows more than you can be a quick way to learn - and breathe life into dead mods.

This guide's audience is more for people who know how to program - reading APIs, jumping through call graphs and object hierarchy, writing GUIs, modding games - but are new to Unity, KSP modding, and maybe C#. I won't cover the basics of any of these. If you need help with C# syntax, google "c sharp [thing I want to do] example".

Version of Unity

The file version of the KSP executable (at least on Windows, need confirmation for other platforms) match the version of Unity used in that release.

Unity v.s. KSP

Is that class Unity (so you should look through Unity documentation), KSP (so you should look through [KSP's Community API Documentation]), or from a mod? Here's how to tell:

  • Unity classes live in the UnityEngine and UnityEngine.UI DLLs and namespaces
  • KSP classes live in Assembly-CSharp.dll and in the KSP namespace

Main Class

When reading code for standalone programs, starting from the main() method is a really great way to understand what a program does. With a decent IDE you can jump through the index and call trees to find where everything is defined and used.

The equivalents in KSP:

  • Classes starting with "Module". These are Part modules that implement part behavior. You can also find them in the part's .cfg file.
  • The KSPAddon C# attribute. It calls functions when certain game conditions are met, e.g. startup or entering flight. Search for the text "[KSPAddon(". I think the call graphs from these functions are VERY useful
  • Children classes of MonoBehaviour. These implement Unity callbacks.
  • The KSPEvent C# attribute. If you want to find the code that implements a Part right-click menu, search for "[KSPEvent(".