Jump to content

Getting started, the basics of writing a plug-in.


Recommended Posts

I'm also trying to start from scratch here. I normally use C, or many years ago, Java (back when it was newish to the world). I'm stumbling a bit on the basics of getting a VS project set up. Anyone have a more in depth tutorial on setting up and creating a project for KSP mods in visual studio? For example, what kind of project do I make? Obviously a new clean application isn't right. :p

I seem to be kind of stuck. I copied the example, complied with (I hope?) the proper references for KSP 1.8, got no compile errors, the KSP.log shows the module loading.. but no hello world anywhere, on launch or on startup. I tried a different tutorial to the same effect. There's no warnings, it just doesn't seem to do anything.

Edited by AmpCat
Link to comment
Share on other sites

2 hours ago, AmpCat said:

I'm also trying to start from scratch here. I normally use C, or many years ago, Java (back when it was newish to the world). I'm stumbling a bit on the basics of getting a VS project set up. Anyone have a more in depth tutorial on setting up and creating a project for KSP mods in visual studio? For example, what kind of project do I make? Obviously a new clean application isn't right. :p

I seem to be kind of stuck. I copied the example, complied with (I hope?) the proper references for KSP 1.8, got no compile errors, the KSP.log shows the module loading.. but no hello world anywhere, on launch or on startup. I tried a different tutorial to the same effect. There's no warnings, it just doesn't seem to do anything.

push it onto github and link it - best way to get help imho.

Link to comment
Share on other sites

  • 3 months later...
On 12/19/2016 at 11:49 PM, Diazo said:

Note that ConfigNodes are very dumb.

If you GetValue('Key5") when Key5 doesn't exist, KSP throws a Null Reference Exception and stop running your code.

If you AddValue("Key1","Value1") when the node already has a Key1 present it will add a second Key1 value to the node instead of updating the existing value.

You can SetValue("Key1","Value1") instead to update an existing Key1 value, but if Key1 doesn't exist, it will Null Reference and stop running your code.

Any chance this will be fixed someday?

Yeah... on further reflection, its probably better this way

Edited by mattssheep4
Link to comment
Share on other sites

34 minutes ago, mattssheep4 said:

Any chance this will be fixed someday?

There are tryGetXXX calls that fixes the first "problem". The second one is by design and having multiple value with the same name is perfectly valid and is used for curves configuration. The last one is nothing out of the ordinary and is no different than accessing a null value. You have to check first.

You can write you own ConfigNodes extension if this make your life hard.

Link to comment
Share on other sites

  • 2 months later...
On 12/19/2016 at 11:49 PM, Diazo said:

In your project, add a reference to UnityEngine.dll, UnityEngine.UI.dll

I'm getting an error saying that these two files are already referenced (presumably from Unity already). Should I remove them and replace with the ones from the KSP folder?

EDIT:
I've got things mostly working (I think?), but there is no logging happening in the console. In ksp.log, there's this line which is confusing me:

Quote

[LOG 10:28:46.210] [AddonLoader]: Instantiating addon 'ContractDefs' from assembly 'MyMod'

There's no reference to ContractDefs in my code, anyone know what gives?

Edit 2:
Started from scratch a few times... oddly enough, the last iteration showed a similar problem as what I've seen consistently in my attempts to mod KSP, where I can access KSP assets but can't reference MonoBehaviour. I was trawling through many threads on the forums to figure it out, and when I looked back at my IDE, there were no more errors. I'm flabbergasted, but pleased. I will try again, to try to hammer down the exact process, and share when I have it.

Edit 3:
As promised, a detailed walk-through of a repeatable mod dev setup. I'm using KSP 1.9, but I'm pretty sure 1.8 used the same unity version, so it should work for 1.8.x as well. I'm also using Visual Studio 2017 on Windows 10, though afaik the VS version matters less.

Moving on:
Start Visual Studio. Close any open projects, and create a new project as shown in the image:

(NOTE: I tested a few different options here, including .NET Standard and .NET Core class libraries, which did NOT work. Though I admit, I wasn't keeping very good track of what I had and hadn't tried when I was testing those options, so feel free to try them at your own risk.)

85lxdu7.png

I suggest the location to be somewhere near you KSP dev folder, but it doesn't need to be. 

.NET 4.0 is required, and .NET 4.5 is recommended for 1.8+, per the wiki

Hit OK, and VS will create the project. For some reason, I had to close VS and open the solution (.sln) file by double click because VS kept saying it was still building the project, but it obviously had finished. Regardless, once you have your project open and set up, you then need to select references. I've seen a few different variations on what dll's need referenced. After a lot of experimentation, it seems that the *minimum* to reference are:

- UnityEngine.CoreModule.dll

- Assembly-CSharp.dll

Side note: I was shocked to find that UnityEngine.dll is *not* a requirement for the basic script in the OP, but you will need it to do a lot of things.

To add them, click on the Project tab, then Add Reference, then browse. Navigate to your KSP directory/KSP_x64_data/Managed folder, and select the above files:

4hpIMlT.png

(You can see how much testing I did in that pic with different versions, etc... to find that only those two little dlls are necessary)

Once you have your references, you should be good to go from the OP and other posts in the Add-On subforum.

Hope this helps other beginner modders! 

As an aside, learning Unity outside of a mod dev environment is really helpful for modding. I've been using Unity for a couple years now, mostly just for messing around with building my own small games, but because of that I'm very confident in actually coding KSP mods - it was just setting up the environment that was tripping me up for so long.

 

Edit 4: I guess I should mention a bit about compiling - once you're ready to compile and test your code, it's useful (imo) to point your build directory to your mod's folder. Then hit build, and go to your mod folder. You'll have to delete all the other files the IDE generates, you only need YourMod.dll for the game to do its thing. Unfortunately that's something you''ll have to do every time you build it. I haven't tested it, but its possible they won't hurt anything being there for testing, but imo its best to test as closely to release as possible.

Edited by mattssheep4
Figured things out
Link to comment
Share on other sites

  • 2 weeks later...

Hi guys, i want to writte a plug-in and i'm glad to find this course. But i don't know so much coding in c# or c++. can you explain me how to add reference to  dll files in a project? I use code::blocks

Edited by axe_bra
Link to comment
Share on other sites

4 hours ago, axe_bra said:

Hi guys, i want to writte a plug-in and i'm glad to find this course. But i don't know so much coding in c# or c++. can you explain me how to add reference to  dll files in a project? I use code::blocks

I would maybe back you up a step or three and ask,  "Are you familiar at all with game development in the Unity engine?"

If not, I think a valid suggestion would be to start by downloading Unity and setting up your C# script editor with appropriate Unity Debug extensions.  Now is REALLY a great time to get started in Unity because (due to COVID) Unity is allowing free access to certain premium tutorial/learning tools that normally would cost you $40/month.  Not to mention, we're all a bit homebound anyway so what else are you going to do with your time?  

Then spend a day or two working through the tutorial game examples... and allow yourself some exploration to branch off into areas of interest and maybe make yourself a simple little game like Asteroids or Pac Man or something.  In the process you'll come across more than one tutorial video on "Getting started with C# Scripting" and after watching several other videos on various other Unity development areas of interest to you...  ask a few questions, search out some answers for stuff like:  "How do I make my player follow a mouse pointer?" and so forth...  it will begin to make sense. 

With that experience under your belt, THEN you can come back to this thread and the C# scripting notes above will make perfect sense.  I would budget yourself at least a week or two for this.

As it turns out, I'm about my second week into this process and would be happy to provide tips on how to get your copy of Unity up and running with a C# scripting editor and various tools.  I'm cheap, so naturally all of my tool choices and extensions are freebies, but I don't feel hampered by that at all.  The tools I have to work with are excellent!

Edited by XLjedi
Link to comment
Share on other sites

6 hours ago, XLjedi said:

I would maybe back you up a step or three and ask,  "Are you familiar at all with game development in the Unity engine?"

If not, I think a valid suggestion would be to start by downloading Unity and setting up your C# script editor with appropriate Unity Debug extensions.  Now is REALLY a great time to get started in Unity because (due to COVID) Unity is allowing free access to certain premium tutorial/learning tools that normally would cost you $40/month.  Not to mention, we're all a bit homebound anyway so what else are you going to do with your time?  

Then spend a day or two working through the tutorial game examples... and allow yourself some exploration to branch off into areas of interest and maybe make yourself a simple little game like Asteroids or Pac Man or something.  In the process you'll come across more than one tutorial video on "Getting started with C# Scripting" and after watching several other videos on various other Unity development areas of interest to you...  ask a few questions, search out some answers for stuff like:  "How do I make my player follow a mouse pointer?" and so forth...  it will begin to make sense. 

With that experience under your belt, THEN you can come back to this thread and the C# scripting notes above will make perfect sense.  I would budget yourself at least a week or two for this.

As it turns out, I'm about my second week into this process and would be happy to provide tips on how to get your copy of Unity up and running with a C# scripting editor and various tools.  I'm cheap, so naturally all of my tool choices and extensions are freebies, but I don't feel hampered by that at all.  The tools I have to work with are excellent!

Thank you XLJedi. 

i'm not familiar at all with game development in the unity engine. So i will follow your suggestion.

See you later :)

 

Link to comment
Share on other sites

  • 1 year later...

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.

 Share

×
×
  • Create New...