Jump to content

moding with c++


Boson Higgs

Recommended Posts

Hello, and welcome to the forums!  :)

KSP is based on top of .NET, which means you need to write your mods on a .NET-compatible language.  By far the most common language that folks use for this is C#.

Is there some reason you want to use C++ instead of C#?

Link to comment
Share on other sites

48 minutes ago, Snark said:

Hello, and welcome to the forums!  :)

KSP is based on top of .NET, which means you need to write your mods on a .NET-compatible language.  By far the most common language that folks use for this is C#.

Is there some reason you want to use C++ instead of C#?

tnx
i have more knowledge  on c++ than c#
so if i want to start modding i should know c# , i just wanted to knew without practicing anything can i make a small mod with c++ , if yes what kind of mod
and do i need to be a master at c# to start modding?
Thank you :)

Link to comment
Share on other sites

52 minutes ago, Boson Higgs said:

i just wanted to knew without practicing anything can i make a small mod with c++

Basically no.  The issue is that it's not just a different language (syntax etc.), but different semantics (managed versus unmanaged code).  Yes, I believe it's technically possible to create a thing that's an interop layer with C++ for managed code, but  it's hideously ugly and either you end up needing to write a C# wrapper, or (is this still a thing?) this monstrosity called "managed C++" which might as well be learning a new language and you don't want to go there.

54 minutes ago, Boson Higgs said:

do i need to be a master at c# to start modding?

No, but you do need to know how to write managed code, which is a different paradigm from unmanaged code like C++.  It just works differently, it's a different mindset.

C# syntax is very similar to C++ in a lot of ways, but the semantics are that of managed code, so you need to get your mind around that.

If, for example, you already know how to program in Java, then it's easy.  Why?  Because Java is also a managed language, and the semantics are practically the same.  Basically, C# is just like Java except that it doesn't suck as much.  :)  You can think of C# as having the semantics of Java and most of the syntax of C++, if you like.

C# is a pretty easy language to pick up, especially if you start by grabbing someone else's mod and looking at their source code, so you have a model to work from.

Link to comment
Share on other sites

9 minutes ago, Boson Higgs said:

Tnx @TheRagingIrishman

I think im going to start it tomorrow.

U got any advice for me before I start?

All ksp mods are required to be open source, so if you ever can't figure it out, look at other mods code. Also, 

and https://kerbalspaceprogram.com/api/annotated.html is the link to the api documentation.

 

Link to comment
Share on other sites

@Boson Higgs: One thing to be aware of is that the API you'll be coding against is well-documented for Unity, but in general very sparsely and spottily documented for KSP.  Lots of things either aren't documented, or else may have out-of-date documentation.

My experience is that it tends to be a pretty good API, and the code to do a particular thing is usually not too complex... but figuring out how to do it is the stumbling block, due to the lack of documentation.

So you should expect to have to do a lot of detective work as you go:  i.e. googling, and looking at other mods' source code, and searching the Plugin Development Help & Support forum for people who have posted "how do I...?" questions in the area.

And, most likely, posting some questions there yourself.  :wink:

 

Link to comment
Share on other sites

4 minutes ago, shadoxfilms said:

I could have sworn that C++ was legal C#? or is it only C is legal C++?

No. While similar they are different languages and you cannot copy C++ directly into C sharp

 

You can look at it this way .language base language is C, then came C++, then came Java, then came C sharp.

Edited by linuxgurugamer
Link to comment
Share on other sites

3 hours ago, Boson Higgs said:

tnx
i have more knowledge  on c++ than c#
so if i want to start modding i should know c# , i just wanted to knew without practicing anything can i make a small mod with c++ , if yes what kind of mod
and do i need to be a master at c# to start modding?
Thank you :)

No, in a nutshell the differences between C# and C++ are not incredibly large, it's more about practices.  The basics are the same.  Now of course C# does have some advanced stuff in it and a few keyword differences here and there.  It's not hard to learn them though, it is derived from C++ after all.

The biggest thing C++ developers tend to do is declare at the lowest scope possible.  (I remember back in college they used to drill that into us, declare as low as possible!)  That is advantageous in C++, but because C# is a memory managed/garbage collected language and the world we live in memory is cheap, you should declare high when a variable or object can be reused or worse, falls within a loop.  Just be careful not to forget where you are using it before you reuse it.  That reduces garbage collection, which is CPU intensive.  CPU is more important than memory.  Anytime you use the "new" keyword inside a loop, you are probably creating a lot of garbage to be collected.

To provide an example

for(int i = 0; i < Something.Count; i++)
	for(int j = 0; i < SomethingElse.Count; j++)
		//Do Something

In this case you are creating a new j every time you iterate through i.  If that loop goes 600 times, you've created 600 j's that have to be collected as garbage. On the other hand...

int j;
for(int i = 0; i < Something.Count; i++)
	for(j = 0; i < SomethingElse.Count; j++)
		//Do Something

In this case, j is only created once and re-used for each i.  That's one object going to gc.  Now int probably aren't the biggest cause of garbage, it's really the objects that kill but it's an example and they do create some garbage.

Specific to Unity Only: Avoid foreach loops and LINQ.

Edited by Alshain
Link to comment
Share on other sites

I would say that C sharp is derived from java not C++. C# has more things similar to Java than it does C++ although all three languages are obviously very similar.

Just as a note I started coding in C# about a year-and-a-half ago I knew nothing about the language although I have very strong and see but I didn't know C++ and I didn't know Java either although I've seen them it did not take me long to pick up C sharp the biggest difference is is more a matter of how you're working with the system rather than the language traditional coding it's lock structured top to bottom and here you are pretty much event-driven

Edited by linuxgurugamer
Link to comment
Share on other sites

3 minutes ago, Alshain said:

@linuxgurugamer Java is derived from C++, and C# is derived from Java so C# is derived from C++.... but they are all derived from ANSI C.

You are derived from your Grandmother and Grandfather :wink:

You are arguing over semantics .  Of course a grandchild is derived from the grandparent but I was pointing out each level of the progression

Link to comment
Share on other sites

Just now, linuxgurugamer said:

You are arguing over semantics .  Of course a grandchild is derived from the grandparent but I was pointing out each level of the progression

Well I wasn't because he didn't say anything about Java.  I didn't want to muddy the water, so to speak.

Link to comment
Share on other sites

13 hours ago, Alshain said:

In this case you are creating a new j every time you iterate through i.  If that loop goes 600 times, you've created 600 j's that have to be collected as garbage. On the other hand...

No, no, no and no.

There is not allocation there. Value type are handled on the stack for case like that (most of the time, for a detailed explanation read this)

Don't write ugly code if you don't need to.

And for the relation between lang : C# is Java done right.

Edit : Thanks @Boris-Barboris

Edited by sarbian
Link to comment
Share on other sites

16 hours ago, Alshain said:

Specific to Unity Only: Avoid foreach loops and LINQ.

More accurate would be:

Specific to Mono with Unity 5.4: Avoid foreach loops.  Unity 5.5 uses a newer mono compiler which fixes the boxing bug with foreach loops.  Also, if you're compiling with Visual Studio's compiler (as many mods are), then it's not an issue regardless of Unity version.

Specific to Game Programming: Avoid LINQ.

Link to comment
Share on other sites

3 hours ago, nightingale said:

More accurate would be:

Specific to Mono with Unity 5.4: Avoid foreach loops.  Unity 5.5 uses a newer mono compiler which fixes the boxing bug with foreach loops.  Also, if you're compiling with Visual Studio's compiler (as many mods are), then it's not an issue regardless of Unity version.

Specific to Game Programming: Avoid LINQ.

I see, I was not actually certain of the reason why, I've just been avoiding them because it's not like they have to be used and there was that whole discussion over not using them.

Edited by Alshain
Link to comment
Share on other sites

2 hours ago, Alshain said:

I see, I was not actually certain of the reason why, I've just been avoiding them because it's not like they have to be used and there was that whole discussion over not using them.

To be honest, I'm still oversimplifying it.  Even in compilers without that particular bug, it's only when it has a struct iterator that it doesn't generate garbage.  So iterating with a foreach with Unity 5.5/VS over a List<T> is okay, but for some other containers it's not.  Which is why the common advise is to just not use them, since it's typically easy to avoid them anyway.

Link to comment
Share on other sites

Just now, nightingale said:

To be honest, I'm still oversimplifying it.  Even in compilers without that particular bug, it's only when it has a struct iterator that it doesn't generate garbage.  So iterating with a foreach with Unity 5.5/VS over a List<T> is okay, but for some other containers it's not.  Which is why the common advise is to just not use them, since it's typically easy to avoid them anyway.

Right, as I understood it it was if the enumerator was a class or interface.  But, as you say, it's simpler to keep track of if you don't use them.  But if it's the fault of the Mono compiler, I use VS so sounds like I don't really need to worry about it.

Link to comment
Share on other sites

8 hours ago, Alshain said:

Right, as I understood it it was if the enumerator was a class or interface.  But, as you say, it's simpler to keep track of if you don't use them.  But if it's the fault of the Mono compiler, I use VS so sounds like I don't really need to worry about it.

And just to be sure - it's the boxing of things with struct iterators (List<T>, Dictionary<T, V>, etc.) that are the problem in Mono).  Things without (lots of KSP classes that implement IEnumerable will create garbage in a foreach loop regardless of compiler (example: ConfigNodeList).  In most cases the garbage free way is this:

var enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
    var element = enumerator.Current;
    ...
}

 

Edited by nightingale
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...