Jump to content

Help getting started making my first mod, without Visual Studio


Recommended Posts

I am an experienced programmer but new to C#, Unity, and the KSP modding API. The mod I am envisioning is pretty simple (a window with a few sliders that edits a .cfg file), so I found and downloaded this example mod hoping to get it running and then poke around in its code to learn how the modding system works. I'm currently stuck on the "get it running" step. I strongly prefer working lightweight with a text editor and the command line over IDEs, so I'm trying to avoid using Visual Studio. I download the mod, put it in the directory .../Kerbal Space Program/GameData/TacWindowTest/Plugins, copy a part file into the /Parts directory and edit it per the instructions, and then attempt to build. I have my system path set to include the latest 64-bit version of csc.exe, so I type

C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\GameData\TacWindowTest\Plugins\TacLib-master\Source>csc /target:library /out:TacWindowTest.dll *.cs

per this documentation page, and get output beginning like the following:

Microsoft (R) Visual C# Compiler version 4.6.1038.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

Icon.cs(28,7): error CS0246: The type or namespace name 'KSP' could not be found (are you missing a using
        directive or an assembly reference?)
Icon.cs(33,7): error CS0246: The type or namespace name 'UnityEngine' could not be found (are you missing a
        using directive or an assembly reference?)
PopupWindow.cs(2,7): error CS0246: The type or namespace name 'UnityEngine' could not be found (are you missing        a using directive or an assembly reference?)

And so on for several hundred similar errors. Obviously I haven't told the compiler how to find the Unity/KSP-specific libraries on which the source code for this mod draws. Newbie that I am, I don't know how to do this. How should I proceed and get to a position where I can start teaching myself the coding by experimentation? Just to be sure, is developing mods even possible without Visual Studio? (All the documentation I can find seems to push towards it very heavily)

Link to comment
Share on other sites

I've never been even slightly tempted to try not using Visual Studio myself, so I'm afraid I can't answer your question-- sorry.

Is there a particular reason you don't want to use it?  It's free, and it makes what you're trying to do there trivially easy, and you get syntax highlighting, and the ability to hit F12 to find the definition of what you're working with, and similar nice goodies.

I would also add that if you don't use an IDE, you're going to run into an absolutely excruciating problem:

The KSP API (i.e. the assemblies you need to reference) is practically undocumented.  So you're stumbling around in the dark.  If you're in an IDE, and you're using some class from KSP, you can just hover over it and hit F12 and immediately see the class definition with all the methods and fields and what-not, extracted from the assembly metadata.

For an undocumented API, the ability to see that information is priceless.  I would have rapidly gone insane without such a feature.

Link to comment
Share on other sites

You need to reference the appropriate KSP DLLs from <KSP>/KSP_DATA/Managed (Assembly-CSharp.dll and UnityEngine.dll at a minimum - other DLLs may be needed depending on project type). See the /reference documentation on the page you linked.

But it's much easier to use an IDE as @Snark says. If you don't want to use Visual Studio for whatever reason then  Xamarin Studio is also available in a free version.

Link to comment
Share on other sites

The easiest way, by far, is to go download SharpDevelop (that also include the c# compiler/assembler).

Then create a new empty project and add the reference assemblies for KSP and Unity:
- Assembly-CSharp
- Assembly-CSharp-firstpass
- KSPUtil
- UnityEngine
- UnityEngine.UI

Then take the default .cs file the project comes with, remove all the content and put this in its place:

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


namespace YourStuff {


[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class MyMod : MonoBehaviour
{
  // keep it alive
  MyMod() { DontDestroyOnLoad(this); }
  
  // called at every simulation step
  public void FixedUpdate()
  {
    print("hello world");
  }
}
  
public class MyPartModule : PartModule
{
  // this is loaded from .cfg
  [KSPField] public float my_value;
  
  // this is an event
  [KSPEvent(guiActive = true, guiName = "Click me!", active = true)]
  public void ClickMe()
  {
    Monobehaviour.print("stop clicking me!");
  }

  // shown on the part tooltip in editor
  public override string GetInfo()
  {
    return "hello world!";
  }
}
  
} // YourStuff

 

To get started with the UI, have a look at this simple, self-contained Notepad

Link to comment
Share on other sites

5 hours ago, dpitch40 said:

I am an experienced programmer but new to C#, Unity, and the KSP modding API. The mod I am envisioning is pretty simple (a window with a few sliders that edits a .cfg file), so I found and downloaded this example mod hoping to get it running and then poke around in its code to learn how the modding system works. I'm currently stuck on the "get it running" step. I strongly prefer working lightweight with a text editor and the command line over IDEs, so I'm trying to avoid using Visual Studio. I download the mod, put it in the directory .../Kerbal Space Program/GameData/TacWindowTest/Plugins, copy a part file into the /Parts directory and edit it per the instructions, and then attempt to build. I have my system path set to include the latest 64-bit version of csc.exe, so I type


C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\GameData\TacWindowTest\Plugins\TacLib-master\Source>csc /target:library /out:TacWindowTest.dll *.cs

per this documentation page, and get output beginning like the following:

First I ll concur with the others and say that not using an ide will cripple you for KSP coding since you will miss auto-completion and you will have more problem finding what you need.

For your cmd line trouble you need to add a /nostdlib  and  /lib:<dir of KSP dll> or a list of  /reference:<dll>.

Here is the build line for MJ for reference (I should use /ib...) :

/home/roslyn/csc.exe /noconfig /target:library /checked- /nowarn:1701,1702,2008 /langversion:6 /nostdlib+ /platform:AnyCPU /warn:4 /errorendlocation /preferreduilang:en-US /highentropyva- /optimize+ /debug- /filealign:512 /reference:/home/ksp/KSP_Data/Managed/Assembly-CSharp.dll /reference:/home/ksp/KSP_Data/Managed/Assembly-CSharp-firstpass.dll /reference:/home/ksp/KSP_Data/Managed/KSPUtil.dll /reference:/home/ksp/KSP_Data/Managed/mscorlib.dll /reference:/home/ksp/KSP_Data/Managed/System.Core.dll /reference:/home/ksp/KSP_Data/Managed/System.dll /reference:/home/ksp/KSP_Data/Managed/UnityEngine.dll /reference:/home/ksp/KSP_Data/Managed/UnityEngine.UI.dll /out:/home/jenkins/jobs/MechJeb2-Dev/workspace/build/MechJeb2/Plugins/MechJeb2.dll /resource:Resources.resources,MuMech.Properties.Resources.resources '/recurse:*.cs'

 

Link to comment
Share on other sites

I guess I don't like the barrier IDEs put between you, your code, and its execution. I like the simplicity, intuitiveness, and hands-on experience of building with the command line and a (high-quality) text editor, and automating where necessary as I go. It's what I've done in my job for four years, and I feel like anything that is easy in an IDE should still at least be possible without. Anyway, it turns out the /lib and /reference options for csc were what I needed for my initial question. I managed to get the super-basic tutorial on the wiki working perfectly. For the TacWindowTest mod, I am making significant progress by building with the following batch script:

@echo off

set LIB=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP_x64_Data\Managed
set PLUGINS=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\GameData\TacWindowTest\Plugins

csc /lib:"%LIB%" /r:Assembly-CSharp.dll /r:UnityEngine.dll /r:UnityEngine.UI.dll /r:KSPUtil.dll /out:"%PLUGINS%\TacWindowTest.dll" /target:library Icon.cs PopupWindow.cs TacWindowTest.cs Utilities.cs Window.cs

For some reason just using the /lib argument as suggested doesn't help; I have to include a reference to each needed library. But anyway, after updating a few usages that were outdated due to the example being written in 2013, it compiles!

However, it doesn't do anything in game, and I find the following line in the debug log on starting KSP: "[Error]: Cannot find a PartModule of typename 'TacWindowTest'". Someone has asked a prior question on this message, and the solution was to make sure the name of the MODULE added to the modded part matches the name of the main class in the compiled DLL. The name of the MODULE I added to my modded  RCS thruster in the TacWindowTest\Parts directory is "TacWindowTest", which is also the name of the top-level class in the TacWindowTest.cs. The .dll I am compiling is getting placed in TacWindowTest\Plugins\TacWindowTest.dll. Where is the disconnect occurring here?

Link to comment
Share on other sites

I've been a developer for about 45 years.  When developing for Unix/Linux, I use a text editor (gVim), whether C/C++ or Assembler for apps or device drivers.  When creating mods, I use the MS Visual Studio.  As others have stated, it provides the functionality you need and doesn't get in the way.

I don't know your development methods, but I prefer efficiency and expediency.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...