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.
- Download Visual Studio Community (not VS Code) from here: https://visualstudio.microsoft.com/vs/community/
- Run the installer
- During installation, make sure you install
.Net desktop developement
as seen here:
- 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#.
- 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)
- Navigate to https://git-scm.com/downloads and download your appropriate version for your OS
- Run the installer
- 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
- 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 selectingOpen in File Explorer
- Insert the path into Git GUI and select
Open
- Select in the top line
Repository
, thenGit 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
- In Visual Studio, switch back to the
Solution Explorer
, right-click your ModName folder and selectUnload Project
- Right-click your ModName folder again and select
Edit Project File
- Navigate to https://github.com/KSPModdingLibs/KSPBuildTools?tab=readme-ov-file#kspcommonprops and copy the
<Import ...
line. This will most likely be:<Import Project="$(SolutionDir)KSPBuildTools\KSPCommon.targets" />
- Insert the copied line after
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- Delete the entire
<ItemGroup>...
that includesReference
s, as those are replaced by KSP Build Tools.
- Right-click your ModName folder again and select
Reload Project with Dependencies
- If your KSP Folder is not in the default Steam installation directory: In the Solution Explorer double-click
Properties
and navigate toReference Paths
- Select
Browse
and select your KSP installation folder - Select
Add Folder
- Select
(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
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.
- Compiling is simple, the default shortcut to compile your mod is
CTRL + SHIFT + B
- 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 withcd 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:
-
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.
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 issuesFlags
,FlagsAgency
andFlagsOrganization
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/
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:
YT: Patched Conics Modding Journey
KSP Forum: Plugin Modding Guide (outdated)
KSP Forum: Part Modding Guides (possibly outdated, untested):
- https://forum.kerbalspaceprogram.com/topic/160487-official-parttools/
- https://forum.kerbalspaceprogram.com/topic/169578-parts-demo-–-blender-gimp-and-unity-files-dec-7-2019/
- https://wiki.kerbalspaceprogram.com/wiki/Category:Modding_Tutorials
- https://wiki.kerbalspaceprogram.com/wiki/Tutorial:Making_an_asset_from_start_to_finish
- https://forum.kerbalspaceprogram.com/search/?q=Tutorial&quick=1&type=forums_topic&nodes=36&updated_after=any&sortby=relevancy&search_in=titles
Screenshots taken from:
Kind help provided by:
- JonnyOThan on Discord