Creating a new Plugin Mod - KSPModdingLibs/KSPModdingWiki GitHub Wiki

Written by @HB-Stratos

How to Mod KSP in 2024

I am not a mod developer, I am just starting out. I got a lot of help, and I'm writing this as a mental note for myself, as well as a hopefully correct guide for others who may want to start with KSP mod developement.

1. Installing Visual Studio Community 2022

image

2. Creating the Project

  • Create a new Project with the preset Class Library (.Net Framework). Make sure it says ".NET Framework" and NOT just "Class Library". It must be the exact one shown in the image.

image

  • Click Next and Name your Project
    • Leave .Net Framework at 4.x for KSP 1.12.5 developement (in my case 4.7.2, but any 4.x version of .net framework should do)

image

3. Set up KSP Build Tools

Add the KSP Build Tools Package

  • Right-click on your project in the solution explorer and select "Manage Nuget Packages"

image

  • Select "Browse" and search for KSPBuildTools. Click "Install":

image

Configuring Visual Studio to use KSP Build Tools

  • In Visual Studio, switch back to the Solution Explorer, right-click your ModName folder and select Unload Project

image

  • Right-click your ModName folder again and select Edit Project File

image

  • Delete the entire <ItemGroup>... that includes References, as those are replaced by KSP Build Tools.

image

  • Right-click your ModName folder again and select Reload Project with Dependencies

image

  • If your KSP Folder is not in the default Steam installation directory: In the Solution Explorer double-click Properties and navigate to Reference Paths
    • Select Browse and select your KSP installation folder
    • Select Add Folder
(SKIP THIS, REFERENCE ONLY) 3.1 Setting up References the old-fashioned way
  • Once in the editor with a c# file open for you to edit, go to the top bar of the program

    • Click on Project -> Add Reference...
    • Select Browse on the Left, then click Browse at the bottom. A windows explorer window will open.
    • Navigate to your KSP installation folder, then to KSP_x64_Data/Managed
    • Select the DLLs you want. For the test mod we will write here, these are
      • Assembly-CSharp.dll
      • Assembly-CSharp-firstpass.dll
      • UnityEngine.dll
      • UnityEngine.CoreMOdule.dll
      • UnityEngine.UI.dll
      • UnityEngine.INputLegacyModule.dll

4. Time to write some code

We'll write a quick example mod to verify that we can indeed compile and run a mod. This example mod is taken straight from Linx's youtube walkthrough (which sadly does not feature KSP Build Tools, https://youtu.be/i0I7MhOM7mg).

I would recommend that you type out all this code yourself instead of copy-pasting the below section so you can experience the autocompletion in action.

Here's the final code:

using System;
using System.Collections.Generic;
using UnityEngine;

namespace TestMod2
{
    [KSPAddon(KSPAddon.Startup.Flight, false)]
    public class TestMod : MonoBehaviour
    {
        public void Update()
        {
            bool key = Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.Alpha1);
            if (key)
            {
                List<Part> parts = FlightGlobals.ActiveVessel.parts;
                int index;
                System.Random rnd = new System.Random();
                index = rnd.Next(1, parts.Count); //we ignore the root part by starting at 1
                parts[index].explode();
            }
        }
    }
}

Take note that we define a class of any name, which is prefixed with the [KSPAddon] block, in which we tell KSP to load this class when the game enters the flight state, and that we would like it to be re-loaded every time the flight state is entered (false). Also note that our class must extend MonoBehavior to make use of unity game engine methods, such as Update(), which is called every frame. Also note that we must state using UnityEngine for any of the following code to work.

5. Compiling the Code

  • Compiling is simple, the default shortcut to compile your mod is CTRL + SHIFT + B

6. Symlinking the compiled output into GameData

  • Open CMD, in my case it was required to be an admin shell (right click in windows search -> execute as administrator)
  • Navigate to your KSP installation folder, then into GameData (go up folders with cd .., go down folders with cd FolderName)
  • Go to your Visual Studio Project folder in Windows Explorer. After compiling there should be a GameData folder within in, and with in that a folder with your mod name. Shift-right-click your ModName folder and select Copy as Path
  • In CMD, run mklink /j ModName "Path/To/Your/VS/Projects/GameData/Modname"
  • Your KSP GameData folder should now look like this:

image

  • Whenever you compile your mod now, KSP will automatically get the changed version too as we have created a symbolic link. It's probably best not to compile your mod while KSP is open.
  • You can launch KSP by launching the KSP.exe in your install directory or by pressing CTRL + F5 in Visual Studio if your reference path is set up correctly.
  • If you want to set custom launch arguments for KSP, you can do so by opening Properties -> Debug and defining command line arguments there. You can e.g. use -popupwindow to make KSP open in Window Borderless mode.

image

7. Outlook

This is as far as I've gotten at the time of writing. I'll likely update this (and post it in a more sensible space) as I learn more about modding and it's pitfalls. I hope this guide was helpful so far!

Please see Guides and Resources for additional useful information, including how to attach the debugger to the game so you can step through your mod's code.

Credits

Screenshots taken from:

Kind help provided by:

  • JonnyOThan on Discord
⚠️ **GitHub.com Fallback** ⚠️