Skip to content

Instantly share code, notes, and snippets.

@HB-Stratos
Last active August 15, 2024 19:24
Show Gist options
  • Save HB-Stratos/f95fd86264013f82c66e6cd344452dbf to your computer and use it in GitHub Desktop.
Save HB-Stratos/f95fd86264013f82c66e6cd344452dbf to your computer and use it in GitHub Desktop.
A guide on how to set up Visual Studio for Kerbal Space Program mod development.

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 is that exact name. It must be the exact one shown in the image, meant for dll creation with C#.

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 Submodule

Installing Git

Set up Visual Studio to use Git

  • In Visual Studio (which may need to be restarted), navigate to Git Changes and choose Create Git Repository...
  • You can log in with github here if you wish, or create a local only repository
  • You can also give your repository a description here
  • Select Create and Push

image

Adding KSP Build Tools as a Git Submodule

  • Open the previously installed Git GUI and select Open existing Repository
  • Browse to your project folder and copy its Path. You can find its path by clicking on the 3 dots next to the master branch name and selecting Open in File Explorer
  • Insert the path into Git GUI and select Open
  • Select in the top line Repository, then Git Bash
  • You can apparently also right click in windows explorer and select Git Bash to shortcut this process.
  • Navigate to https://github.com/KSPModdingLibs/KSPBuildTools and copy the git submodule command. This will most likely be git submodule add https://github.com/KSPModdingLibs/KSPBuildTools.git
  • Paste this command into the Git Bash shell and execute it

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

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. Speeding up KSP launching for faster iteration.

You can strip out a lot of unnecessary features to make KSP load faster for mod developement. Some folders you can usually delete (unless your mod will need them):

  • KSPedia
  • Missions
  • Spaces (IVAs)
  • Props (Objects used in IVAs)
  • zDeprecated (Old parts, some old vessels may not load without)
  • Some content of Parts, whatever you don't need for testing
  • Agencies May cause minor issues
  • Flags, FlagsAgency and FlagsOrganization May also cause minor issues

You can also install the QuickStart mod to have the game automatically return to the scene you left it in: https://spacedock.info/mod/110/QuickStart

Additionally, installing KSP Community fixes will speed up game loading through its caching functionality. Keep in mind though that it changes a few stock behaviors, I would recommend reading the list of features: https://forum.kerbalspaceprogram.com/topic/204002-18-112-kspcommunityfixes-bugfixes-and-qol-tweaks/

8. 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!

I'll leave you with some links towards other resources you may find useful while creating mods:

Debugging KSP Plugins

Unity Explorer for KSP

YT: Patched Conics Modding Journey

KSP Forum: Plugin Modding Guide (outdated)

KSP Forum: Part Modding Guides (possibly outdated, untested):

Credits

Screenshots taken from:

Kind help provided by:

  • JonnyOThan on Discord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment