Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity to use C++?

Discussion in 'General Discussion' started by eshonbel, May 19, 2014.

  1. eshonbel

    eshonbel

    Joined:
    Mar 15, 2014
    Posts:
    7
    Hey guys, I was learning C++ and I have found it to be an extremely efficient language. I know that Unity is coded in C++, but is there any way to implement C++ in the future? plus no one uses booscript
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    C# is one of the big reasons I love Unity... would hate to have to use C++. It being an option? Well, if that's plausible at all then that would make sense to have.

    What about that C#/.NET Native thing I keep hearing about? It's supposedly C# but with native performance. I wonder if that could ever make it into Unity... there have been enough discussions about it, though I haven't read through all of them.
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Keep learning C++, until you find out that it is not, in fact, an extremely efficient language. Then come back and delete this thread and involuntarily shudder every time you remember saying you wanted to script in C++.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    It is efficient, just not in terms of QAD
     
  5. raybarrera

    raybarrera

    Joined:
    Nov 20, 2010
    Posts:
    207
    Yeah, you really have to be more precise when you make a statement like that.

    For experienced users, nothing beats C++ in terms of speed.

    For people who are not masochists, C# is a thing of beauty.
     
  6. sandboxgod

    sandboxgod

    Joined:
    Sep 27, 2013
    Posts:
    366
    Oh no I smell a C# vs C++ war.

    Hey OP (eshonel?), C++ is available to Pro users right now in Unity.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,615
    It's worth pointing out that it's available for use in making native plugins. C++ doesn't get added as a built-in scripting language for Pro users.

    If you're worried about using a language other than the one you're formally trained in, don't. The important part of the formal training isn't the language itself.

    If you train as a carpenter the specifics of what hammers, saws and sandpaper you use aren't the important part of your education. Likewise, when you're learning to program the specific language isn't the key feature. There'll be a little bit of a learning curve whenever you change tools or languages, but that'll get less each time you do it.
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Also available to free users in the form of C++ .NET.
     
  9. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    AFAIK, you could program in any .NET language and export a DLL and use it in Unity.
     
  10. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    C# is better than C++.
     
  11. raybarrera

    raybarrera

    Joined:
    Nov 20, 2010
    Posts:
    207
    Troll alert.
     
  12. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    Just an thoughtful opinion!
     
  13. JovanD

    JovanD

    Joined:
    Jan 5, 2014
    Posts:
    205
    You could always import DLLs
     
  14. JonathanBolten

    JonathanBolten

    Joined:
    Dec 15, 2012
    Posts:
    50
    If you're willing to write a C++ and C# interface, you can code your entire game in a C++ DLL and just call C# functions to make Unity do things. I've recently done this and it's awesome, much less garbage. :)
     
  15. Cygon4

    Cygon4

    Joined:
    Sep 17, 2012
    Posts:
    382
    I've done C++ since 1997 and C# since 2002. Anywhere between 8 and 16 hours a day.

    I'd use C++ if I had a choice. Much better standard library (for the things it covers) and way more consistent upon the already beaten path. Faster compile times, too, if the project is large and your team doesn't consist of lazy guys creating #include dependencies all over the place. The only thing I'd miss would be dependency injection frameworks like Ninject (but these don't integrate into Unity anyway, at least not with constructor injection).

    DLLs are a non-option for normal scripting. You'd have to wrap each of your C++ methods in plain C and write another layer of code on the C# side to P/Invoke it. And then you still have no means of using Unity classes (eg. just calling a simple method on the GameObject class) in C++ without wrapping them in callbacks, too.
     
  16. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    For example you want to use neural network powered image recognition in a game, so you are implementing a neocognitron. What scale of performance improvement C++ going to offer over C#, assuming that you are an programming genius implementing things just perfect way in both languages? What that difference would be if there will be some GPGPU tools available and involved?
     
  17. Cygon4

    Cygon4

    Joined:
    Sep 17, 2012
    Posts:
    382
    From my subjective point of view, I'd have much a much easier time programming this in C++ because its container and iterator concepts are leaps and bounds ahead of .NET's. The library could also be tested outside of Unity whereas I would have to take great care in C# to not access any of Unity's "WrapperCall" classes.

    If you must bring performance into consideration, I'd guess doing on the CPU might give C++ something around a 20%-50% speedup (number crunching being where C++ actually beats C# by a noticeable margin).

    Doing it on the GPU would probably lead to equal results - but C++ would be much easier to program again, thanks to C++11 futures, promises and threading primitives. .NET 4.0 System.Threading.Task is but a small fraction of that, and not even available in Unity's Mono version yet.
     
  18. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    I learned the basics of C++ at school but got used to C# now so I'm totally fine with it ^^' Of course, I don't do super complicated stuff and rarely use C# specific classes/functions, I'm an artist after all ;)
     
  19. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    Opinion: "I think that C# is better than C++"
    Opinion (Better): "I prefer C# to C++"
    Erroneous Statement of Opinion as Fact: "C# is better than C++"

    People need to stop falling back on their right to have an opinion when they're expressing their opinions as absolute truths.

    Pet peeve of mine, carry on.
     
  20. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    I've got astigmatism, so I need to do everything I can in order to C# ;)

    Anyway, I think choice is good, but C++ is a gun specifically designed to shoot yourself at the foot (they even give out ammo for it!) and C# is more like machine gun (submachine if you feel like using non-.NET dlls), as you can produce code fast and you'll shoot your target most of the time.
     
  21. violinbg

    violinbg

    Joined:
    Mar 24, 2014
    Posts:
    79
    You install Unity and start coding right away. You change a line of code in your script - the IDE complies it by the time you switch back from MonoDevelop. If you are on a strict budget and deadline, you are better of with a faster development cycle than a slightly more efficient execution time.

    I don't see how development can be efficient if you had to use C++; I have not seen it yet - UE4 is a total failure on that point. Compilation time is ridiculous and intelli-sence doesn't work...QT Framework is a nice approach to a RAD in C++ but it can be slow when your project gets big.

    Plus in .NET debugging is easier. You can modify values, execute script and in Visual Studio you can even change your code while in debug mode and it will recompile on the fly. So you can see what impact a change may have, not having to stop, change, build and debug. The garbage collection makes your life easier and list goes on.

    I like C++, but I prefer C# because it takes less time to do more.
     
  22. sandboxgod

    sandboxgod

    Joined:
    Sep 27, 2013
    Posts:
    366
    ^^ why did you have to bother bringing up UE4? I am seeing compile times around 20-30 secs on average on my non-SSD. Your average ue4 dev will be using Blueprints anyways which compile incredibly fast and has a better intellisense than probably C# since it is completely context sensitive. Anyway! It is always best to discover the best workflow for yourself

    I bet the original poster is getting way more input than he cared for. It is turning into a C++ vs C# which is ironic- seeing as how Unity was built in C++


    [edit] Here is your official answer from Unity. See blog post on IL2CPP

    I assume Ralph is a Unity employee
     
    Last edited: May 20, 2014
  23. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    But I must say... that thing about being able to write code in C++, drop the .cpp file into Unity, and access it from C# scripts sounds pretty neat. Even if that's not directly scripting with Unity's API/MonoBehaviour in C++, could still be quite useful.

    Hoping that happens.
     
  24. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    It is possible right now. Compile your C++ code into a native dll and call it from your C# scripts.
     
  25. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    ...except it's not even nearly as convenient as just having .cpp files within your project, automatically recompiled when needed. Oh, did I mentioned having to compile for Windows, Linux, MacOS, both 32 bit and 64 bit versions of them, and for all other platforms?
     
  26. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,964
    Supposedly Visual Assist is a good alternative to IntelliSense. Though it will not work with Visual Studio Express.
     
  27. violinbg

    violinbg

    Joined:
    Mar 24, 2014
    Posts:
    79
    My point was that Unity strength is that it works out of the box. Simply install and start working. There are so many things they have hidden away from the programmer yet it still very flexible.

    There are way too many C++ solutions for whoever wants one and very few multiplatform C# once. (or perhaps it's just Unity)
     
  28. eshonbel

    eshonbel

    Joined:
    Mar 15, 2014
    Posts:
    7
    Wow, so many answers ;0
     
  29. Cogent

    Cogent

    Joined:
    May 14, 2013
    Posts:
    356
    Wow, so many opinions!

    ;)
     
  30. im

    im

    Joined:
    Jan 17, 2013
    Posts:
    1,408
    one of the main reasons i choose to use unity for my first game was because i prefer to use c# over c/c++ you get more done quicker. i would really not like for them to drop c# and to go to c/c++ that does not mean they could not release a separate c/c++ api for those who would like to use c/c++
     
  31. drawcode

    drawcode

    Joined:
    Jul 21, 2008
    Posts:
    72
    There was a time when Unity was going to go with a C++ api during that Apple ToS 3.3.1 clause that banned lots of platforms like Flash in 2010: http://blogs.unity3d.com/2010/07/02/unity-and-ios-4-0-update-iii/

    I wish this was an option or it would have continued. I wonder if some of the IL2CPP is actually readable and similar or optimized and unusable due to that. Or if eventually we can choose the direct cpp route or managed to cpp with very similar classes for gameplay like in the blog link.

    I like both C++ and C#, but I would love to use a better IDE in XCode and Visual Studio only for developing C++ based Unity apps like other engines.
     
    Last edited: May 26, 2014
  32. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    I've tried to c++ a little couple of days ago, because of ue4.

    Gosh, who the hell still write in it.
    It reminds me university time. I've initially learned programming with Turbo Pascal, then moved to Delphi - OOP-pimped functional programming Pascal, looking not so bad, but outdated. So, C++ is just a Delphi of C. Just another C, you know, a language developed almost half a century ago, pimped with OOP and other modern stuff. With all that fanciest __s, **s, and ::s.

    No thank you.
    Better learn modern way to write programs, not the one from last century.
     
  33. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    C# or C++... hmm...

    BASIC! :D
     
  34. thxfoo

    thxfoo

    Joined:
    Apr 4, 2014
    Posts:
    515
    Name one reasonably popular application that is not written in C/C++ (not Visual Studio, everybody hates it since it is made in .NET after VS2008 because of resource usage and speed). OK, I would know some, but not many.
    The problem is that many people have not idea how to write modern C++ code. You can code like 20 years ago, but you should not, you do it wrong if you do.
     
  35. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,261
    Styles of coding are never right or wrong. They are specific to the individual. I have a friend who refuses to code in anything other than C. Not C++, original C. I, on the other hand, am quite happy to code in several different languages. I personally prefer more strictly typed languages like Java and C#, but I've coded in C, C++, Actionscript 3, and javascript.

    None of these are right or wrong, just different.

    Personally, I rather favor Unity's way of doing things. The projects I'm working on don't require cutting-edge performance or extreme optimization. I don't need to get my hands dirty with low-level memory management. I can understand and respect those developer who do, but I personally don't. For me the efficiency of Unity's scripting system and how quickly it allows me to test is a big plus.
     
  36. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    I think it is just a question of legacy.

    In 2000 there was only Delphi and C++ to write mainstream software. Well, at least you will probably not write a game engine with Java. And as Pascal was considered as language for students and Delphi as a way to yesterday's students to jump in 'mature' programming, C/C++ was probably the only choice for 'serious', 'professional' development. Not to mention that previous software was written mostly with C, not with Pascal or any other language. Even some of modern game engines are the Quake engine's descendants. Again, the performance question. All of that abstract vs direct. So too many reasons for today's software to be C/C++ based.

    C# is already provides rich instruments to implement logic. Don't know if you can actually write an OS with it. At least now you'll need some another OS to run .NET platform to run your C#-written OS. So that makes C# not to be self-sufficient, I assume. But, from the other side, what actually stops you from compiling your C#-code directly into native code, if you already know what platform will run it? I think, when there will be the ability to create a native code from C# and performance difference with C++ will be considered not critical, things could change. But even then - legacy, legacy always will be there.
     
  37. Wild-Factor

    Wild-Factor

    Joined:
    Oct 11, 2010
    Posts:
    607
    There is probably more things code in php, java etc.. than in C++. Should we use it ?
    Should we make websites in C++ ?
    Spatial Shuttle software in C++, instead of ADA ?
    NO. NO and NO
    Every langage has its strengh and weakness and people should learn to use the right tool for the right task.

    C++ was made to be low level and close to the assembly. For perf reason.
    C# is actually a fixed version of C++. It supress all the productivity killer, and have his own container lib, so programmer don't have their crazy idea "I can make my own math lib and container, which will be 10 times faster than the previous without bugs in no time". We all know how it ends :)
    20-30% of increase in productivity, cost you a fraction of modern processor power. Is there going to be a performance in using C++ or C# has a scripting langage on the perf ? For players not a visible one. Is it going to make you have a lot more time to polish your game. Yes. => better for the player

    For the player using C# is a better choice. Now you have to ask yourself: do you make a game for the technical challenge or to give joy to people ?
     
    Last edited: May 27, 2014
  38. thxfoo

    thxfoo

    Joined:
    Apr 4, 2014
    Posts:
    515
    I disagree. There are coding styles that are very error prone, that would survive no serious code review. So in my eyes these are wrong. C++ got many improvements, if you use them your code is safer, faster and easier to read. Not using them without a good reason (not knowing them is no reason) is an error.

    There can be good reasons not to use unique pointers or smart pointers or references, but if the reason is that you want to code like you always coded, then you are the legacy. Modern C++ is very different from legacy C++ and for good reasons.
     
  39. LightSource

    LightSource

    Joined:
    Sep 29, 2012
    Posts:
    249
    Pssh, ya'll are missing out.
    Boo 4 life!
     
  40. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Does he mean coding style or coding practise? Style (as in how many spaces or where you stick your braces) isn't so important as practise and methodology.
     
  41. Cogent

    Cogent

    Joined:
    May 14, 2013
    Posts:
    356
    We want Erlang.NET.

    Everyone knows Erlang is better than C, C++, C#, E#, F#, J# java, HTML5, Boo, Ada, javascript, AS3, asm, cobol, fortran, sql, dhtml, pascal, QuakeC, Haxe...


    Well... either Erlang or Logo...


    :evil:
     
  42. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    You may use any .Net language - just compile your assemblies in Visual Studio or in whatever IDE you use.

    C++ CLI
    Code (csharp):
    1. #using <UnityEngine.dll>
    2. using namespace UnityEngine;
    3. using namespace System;
    4.  
    5. public ref class CPlusPlusTest : MonoBehaviour
    6. {
    7. public:
    8.   void OnGUI()
    9.   {
    10.     GUI::Label(Rect(0, 60, 400, 30), "Hello from C++ CLI");
    11.   }
    12.  
    13.   void Update()
    14.   {
    15.     transform->Rotate(Vector3::right, 30 * Time::deltaTime);
    16.   }
    17. };
    Visual Basic
    Code (csharp):
    1. Imports UnityEngine
    2.  
    3. Public Class VisualBasicTest Inherits MonoBehaviour
    4.  
    5.   Public Sub OnGUI()
    6.     GUI.Label(New Rect(0, 30, 400, 30), "Hello from Visual Basic")
    7.   End Sub
    8.  
    9.   Public Sub Update()
    10.     transform.Rotate(Vector3.right, 30 * Time.deltaTime)
    11.   End Sub
    12.  
    13. End Class
     
  43. eshonbel

    eshonbel

    Joined:
    Mar 15, 2014
    Posts:
    7
    The only thing I know that was created with Erlang is Wings 3D. It's not really meant for games.
    This is Erlang:
    %% qsort:qsort(List)
    %% Sort a list of items
    -module(qsort). % This is the file 'qsort.erl'
    -export([qsort/1]). % A function 'qsort' with 1 parameter is exported (no type, no name)

    qsort([]) -> []; % If the list [] is empty, return an empty list (nothing to sort)
    qsort([Pivot|Rest]) ->
    % Compose recursively a list with 'Front' for all elements that should be before 'Pivot'
    % then 'Pivot' then 'Back' for all elements that should be after 'Pivot'
    qsort([Front || Front <- Rest, Front < Pivot])
    ++ [Pivot] ++
    qsort([Back || Back <- Rest, Back >= Pivot]).

    It doesn't look friendly to me.

    Plus, this is turning out into a code war. I'm a noob at the moment with most code, so I don't know what half of you are talking about.. Thank you for your opinions, but is there a chance that Unity will implement C++?
     
  44. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    That is not representative of Erlang. Since you're a newbie I suggest you go and learn it - it's not easy compared to some of the more mainstream languages, but has some great concepts that are worth knowing.


    Unity supports C++.