-
Notifications
You must be signed in to change notification settings - Fork 0
Creating a new Plugin Mod
Written by @HB-Stratos
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 says ".NET Framework" and NOT just "Class Library". It must be the exact one shown in the 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)
- Right-click on your project in the solution explorer and select "Manage Nuget Packages"
- Select "Browse" and search for KSPBuildTools. Click "Install":
- 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
- 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.
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.
Screenshots taken from:
Kind help provided by:
- JonnyOThan on Discord