Jump to content

How do I make a plugin that opens a file?


Waseemq1235

Recommended Posts

Greetings,

I've been through the very basics of part making, but have no clue on making a plugin.

The thing that I was most interested in is, well, a plugin, you press a button, and it opens a file (to be specific, an MP3 file, also, I want the MP3 file to run in the background [Basically a music player]).

First of all, I have NO idea what programming language or how to program a plugin.

Second of all, I know no programming languages (yet, heh..).

Third of all, what in the world do you do with a dll file?

Well, I really need the very basics of making a plugin.

Appreciated,

Waseem.

Link to comment
Share on other sites

Programing language - C# is the only one I've seen used (though you can probably use other .Net capable languages)

Learning programing - I would recommend getting your MP3 player going in a standalone app and then adding the MP3 part to a KSP plugin otherwise you'll be tackling programming, Unity(the game framework  KSP uses), and KSP's implementation all at the same time. There are a number of free compilers out there for C# - if you're on Windows there's MS Visual Studio ? edition (forget what it's called - the low end free version). A couple others who's names I'd probably get wrong - you can search the forums (Or look in this next link ->). A useful page is the Mod Development Links Compilation. It has a lot of information including on tools and examples. The other good source of information is the source code that comes with most plugin mods. I think it was TaranisElsu made some example starter projects - sorry I've lost the links - probably google-able.

Once you've made your KSP plugin it compiles to a dll - if you've built it correctly and place the dll in the GameData folder KSP will see it and load it. Be prepared to pull out most of your hair (if any) in getting to this point :D

Link to comment
Share on other sites

10 hours ago, wasml said:

Programing language - C# is the only one I've seen used (though you can probably use other .Net capable languages)

No, KSP (or Unity) does not use .NET as it's CLI.  I think it uses Mono if I am not mistaken.

In any case Unity programming is mostly limited to C#. Though it can also use JavaScript and Boo, KSP doesn't.

Edited by Alshain
Link to comment
Share on other sites

 

You can do everything from the command line, 

 

Assuming you are using windows,  here's a class that does nothing.

namespace DemoCompile
{
	using System;
	using System.Linq;
	
	public class MyPartModule : PartModule
	{
		[KSPField]
		public static string myValue;
		
		public override void OnFixedUpdate(){
			// do stuff
		}
	}
}
@echo off
set kerbal=E:\games\kerbal\KSP_x64_Data\Managed
set outDll=myplugin.dll
%windir%\Microsoft.net\framework\v3.5\csc.exe /t:library /out:%outDll% *.cs %1 /NOLOGO /r:%kerbal%\Assembly-CSharp.dll /r:%kerbal%\Assembly-CSharp-firstPass.dll /r:%kerbal%\UnityEngine.dll /r:%kerbal%\unityEngine.dll /r:%kerbal%\KSPUtil.dll

Create a file called  make.cmd  and paste the command lines above into it.  Changing the path to your managed folder and the name of the dll you want to change.

I do this with a bunch of mods that are dependent on other mods so that I can recompile with the setup I have.

GE.

 

Link to comment
Share on other sites

As far as language selection goes, I advocate that F# is a viable KSP modding language option, despite having no KSP mods to my name. Here is a link to a previous post on the topic:

 

On 9/17/2016 at 8:17 PM, wasml said:

Programing language - C# is the only one I've seen used (though you can probably use other .Net capable languages)

Yup, my point exactly, other .Net capable languages ought to work as well.

 

On 9/18/2016 at 4:34 AM, Van Disaster said:

VS Community Edition - not lacking anything a solo Unity coder is likely to use, I think.

Opening a file is easy. Playing a mp3 file is *way* more complicated than opening a file! you'd probably be better off attempting to control an existing mp3 player.

I believe you are correct on both/all of those statements.

 

On 9/18/2016 at 6:27 AM, Alshain said:

No, KSP (or Unity) does not use .NET as it's CLI.  I think it uses Mono if I am not mistaken.

In any case Unity programming is mostly limited to C#. Though it can also use JavaScript and Boo, KSP doesn't.

You might be technically correct. However, I find it unfortunate that you seem to suggest that C# is the only proper choice.

Link to comment
Share on other sites

On 18.9.2016 at 6:27 AM, Alshain said:

No, KSP (or Unity) does not use .NET as it's CLI.  I think it uses Mono if I am not mistaken.

In any case Unity programming is mostly limited to C#. Though it can also use JavaScript and Boo, KSP doesn't.

Ahem: Mono is an open source implementation of the Microsoft .NET framework using the CLI as base (for a list of compatibility issues see here). So, "Mono" and ".NET" are virtually interchangeable. Programs built under ,NET can run straight under Mono without any recompilation (and should vice versa) as long as they obey compatibility restrictions (well, this is theory, in practise this is not always the case).

Languages supported by Mono are C#, F#, Java (for Mono), Scala, Boo, Nemerle, Visual Basic .NET, Python, Javascript, Oberon, PHP, Object Pascal, Lua, Cobra, Synergy, Component Pascal, and a Forth, Lisp and Smalltalk dialect.

A project may contain source files written in different languages, methods/functions from a class in one language can make calls to methods/function written in another language, as long as both are using Mono/.NET. In theory, even class member function of the same class could be written in different languages.

So no, Unity programming is not restricted to C#. A developer may use any of the .NET/Mono langugaes cited above. Unity class documentation is done only for C# though...

 

 

Link to comment
Share on other sites

4 minutes ago, Carraux said:

Ahem: Mono is an open source implementation of the Microsoft .NET framework using the CLI as base (for a list of compatibility issues see here). So, "Mono" and ".NET" are virtually interchangeable. Programs built under ,NET can run straight under Mono without any recompilation (and should vice versa) as long as they obey compatibility restrictions (well, this is theory, in practise this is not always the case).

Languages supported by Mono are C#, F#, Java (for Mono), Scala, Boo, Nemerle, Visual Basic .NET, Python, Javascript, Oberon, PHP, Object Pascal, Lua, Cobra, Synergy, Component Pascal, and a Forth, Lisp and Smalltalk dialect.

A project may contain source files written in different languages, methods/functions from a class in one language can make calls to methods/function written in another language, as long as both are using Mono/.NET. In theory, even class member function of the same class could be written in different languages.

So no, Unity programming is not restricted to C#. A developer may use any of the .NET/Mono langugaes cited above. Unity class documentation is done only for C# though...

 

 

All of that is great but a plugin doesn't use Mono or .NET, it uses Unity.

Link to comment
Share on other sites

On 17.9.2016 at 5:47 PM, Waseemq1235 said:

First of all, I have NO idea what programming language or how to program a plugin.

Second of all, I know no programming languages (yet, heh..).

Third of all, what in the world do you do with a dll file?

The third one comes to you if you have mastered the first and second point and is the least interesting here. The first point gets interesting when you have mastered the second part of first point at least on a minimum level.

So: Learn programming! :) It's fun! :)

C# is a good language. And it's the one KSP is written in. Maybe this may help you:
https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23 beginner tutorial

Link to comment
Share on other sites

I feel like there may be some confusion over compiler vs. CLI.  You can use the Microsoft Visual Studio to develop C# plugins for KSP.  However that is not in itself using .NET.  You are only using Microsoft.NET if you include and use Microsoft's references.  For example here is what a default .NET Class library looks like in Visual studio.  Pay attention to the references, you have Microsoft.CSharp and System.

usBcHUg.png

Now lets take a look at my tiny little plugin.  Notice something different?  No Microsoft, No System, No Mono, nothing but Assembly-CSharp (which is the core of KSP) and two Unity references.  There are a few others that can exist in plugins, but they all come from Unity and KSP and can be found in your KSP_Data\Managed folder.  To my knowledge no plugin ever uses .NET or Mono.

QDVR5mH.png

 

Edited by Alshain
Link to comment
Share on other sites

1 hour ago, Carraux said:

The third one comes to you if you have mastered the first and second point and is the least interesting here. The first point gets interesting when you have mastered the second part of first point at least on a minimum level.

So: Learn programming! :) It's fun! :)

C# is a good language. And it's the one KSP is written in. Maybe this may help you:
https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23 beginner tutorial

Oh wow, just clicked on a basic tutorial, 2nd tutorial, doesn't get anything... Not sure how that's fun..

Seriously.. It's impossibly hard to learn...

I have a question, is a plugin like a GUI?

Also, what are source codes? (That crap that I always hear of)

P.S: For the ones that think that I am not looking at the new posts, you are wrong, I am keeping track of this.

 

Edited by Waseemq1235
Link to comment
Share on other sites

11 hours ago, Waseemq1235 said:

Oh wow, just clicked on a basic tutorial, 2nd tutorial, doesn't get anything... Not sure how that's fun..

Seriously.. It's impossibly hard to learn...

I have a question, is a plugin like a GUI?

Also, what are source codes? (That crap that I always hear of)

P.S: For the ones that think that I am not looking at the new posts, you are wrong, I am keeping track of this.

 

I've always recommended books over online resources for initial learning.  Online resources are great for reference.   Easy beginner books IMO are the Teach Yourself series from Sams Publishing.  You can get them digitally or paper.  The latest appears to be C# 5.0 which is actually not current (2015 introduced 6.0) but given the beginner nature of it, 99.9% of it will be the same.  C# 6.0 (for that matter every version since 2.0) introduces all high level stuff that won't be covered by the book.

Stay away from Head First books.  They use this method they call "Brain Friendly" and it is anything but.  It is confusing and hard to follow and it's as if it were written by someone who suffers from ADD because it jumps all over the freaking page trying to explain one concept.

To answer the question, a plugin can have a GUI, or not.  A GUI (Graphical User Interface) is just something the user interacts with.  You use them every day, everything you typically 'see' in Windows is a GUI, what isn't a GUI is the stuff behind the scenes.  In KSP the VAB part menu is a GUI, the buttons, those are part of the GUI.  In a mod like KER, the display screen is a GUI.

Source Code is the uncompiled program.  That is, it is human readable and not machine readable.  So anything you write will be your source code.  The compiler will transform that into machine readable programs.

Edited by Alshain
Link to comment
Share on other sites

13 hours ago, Waseemq1235 said:

Also, what are source codes? (That crap that I always hear of)

Ok, here we go:

Your computer has a microprocessor capable of performing simple commands like adding two numbers or comparing two numbers or do other simple logical things. This set of commands is called "machine language". And a collection of those commands is a program. Programs are stored in digital/binary format on your hard disk, and not in clear text.

Machine language is not really easy readable for human eyes, it's not ment to be a language for humans. It is designed to be easy implemented as a bunch of circuits in your micro processor. So to program your computer, to write a computer program, computer scientists created programming languages designed to be used by humans. Unfortunately your PC/micro processor doesn't understand all these new computer languages, it is capable of running machine language programs only.

So to get stuff done with a such a programming language, a new program is needed. A program which is capable to take a text which contains a program/piece of program and translate this into an aquivalent set of machine language commands and store it on your hard disk.

This program is called "compiler".
The resulting compiled program is usualy called executable (or short exe) or binary. Or... program.

And the text file which contains the program, that is the source file, or the source code. Because it is the source from which the computer program will be generated.

Link to comment
Share on other sites

17 hours ago, Alshain said:

I feel like there may be some confusion over compiler vs. CLI [...]

[...] To my knowledge no plugin ever uses .NET or Mono.

The CLI you mention is the "Common Language Infrastructure". But CLI is a specification only, not an implementation. This specification is standardized by ISO and ECMA.

Three groups have taken this specification and have implemented it: These different implementations are called ".NET", "Mono" and "Portable.NET".
(https://en.wikipedia.org/wiki/Common_Language_Infrastructure)

Unity itself is written in C/C++ but uses C# as scripting language. And in the very moment, C# is used, Mono (or .NET) comes into play. And you can't use C# without Mono or .NET because the C# compiler-compiles not into machine language directly, but into the Common Language Specifcation (CLS), a part of the CLI (and therefore .NET/Mono). And the internal implementations of all aspects of the C# language are realized using the CLI. In fact, the current implemantation of both C#-compilers from Microsoft and from the Mono Group are written in C# so the compilers are capable of compiling themselves.

I think what you mean is the .NET framework.

But his one is standardized too (at least most part of it) and includes all implementations of the basic classes like collections, lists, dynamic arrays, file io, match etc. and can be used on any system supporting the CLI. Anytime you see a "using System.blablabla;" in the source file, you see a reference to the standardized classes of the Mono/.NET world. And the KSP mods and Unity uses them too..

Have a look at some example sources from Unity itself for a beginner's tutorial: https://unity3d.com/learn/tutorials/topics/scripting/coding-unity-absolute-beginner (Note the "using System.Collections,").

If you look at MechJeb's sources, you'll find that too

Or KerbalEngineer:

namespace KerbalEngineer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
[...]

(from the KerbalEngineer source file CelestialBodies.cs)

Edited by Carraux
Link to comment
Share on other sites

8 hours ago, severedsolo said:

I'll agree with Alshain that books are better than online... but I can't send you a book.

This website: http://www.learncs.org/

taught me all the basics of C#. I was struggling with syntax etc beforehand, this explained it nice and simply for me. Worth a look I think.

Exactly what I needed, I needed something that has some text, and then, I have to write it. Much better than watching or reading, oh and yes, I do agree that books are helpful, but reading is sometimes boring.

Edit: Still impossibly hard, YOU HAVE TO MEMORIZE EVERY SINGLE LITTLE MARK OR YOUR PROGRAM IS DEAD.. Without those () [] <> ; it would be so much easier, but no...

Edited by Waseemq1235
Link to comment
Share on other sites

15 hours ago, Waseemq1235 said:

Edit: Still impossibly hard, YOU HAVE TO MEMORIZE EVERY SINGLE LITTLE MARK OR YOUR PROGRAM IS DEAD.. Without those () [] <> ; it would be so much easier, but no...

In a "Live environment" (that is, using an IDE like Visual Studio) that's much easier, because it auto-fills or tells you what you've done wrong 99% of the time.

Unfortunately this is how computers work. If it's not exactly how they expect it to be, then your program won't run. Computers have no imagination.

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