Pages: 1
  Print  
Author Topic: Using C++ and GML in same project. Why?  (Read 1901 times)
Offline (Unknown gender) Darkstar2
Posted on: April 21, 2014, 08:01:25 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
It is said that one of the advantages of using ENIGMA is ability to use C++ in your projects along with GML.
This is a question that I have been curious about for some time.  Now please understand I am a complete novice when it comes to C++, more like total newbie :D  and learning GML progressively.

Could someone give me some examples of how C++ code in my projects would benefit me when I have all the GML functions to use?  Some examples would be great. 

Thanks.
Logged
Offline (Male) Goombert
Reply #1 Posted on: April 21, 2014, 09:02:58 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Structures specifically, if you for instance want the equivelant of a point object or something, you can make a small struct with just the variables, or say you want your own particle object to write a 3D particle effects system, you don't want to make an instance and include all built-in local variables such as x,y,speed,direction,image_angle etc for just a tiny 3D particle, you want only the variables you need. The biggest thing with it being you don't need scripts and DLL calls to wrap extensions and native code.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #2 Posted on: April 22, 2014, 06:20:51 AM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
C++ doesn't really differ much from GML syntax wise. So while techincally you can use pure C++ (which as far as I know you still cannot, because the current parser would go insane) there is really no need. You should use EDL though, which is a combination of GML and C++. EDL supports things like specified types which can greatly increase the speed of a game. For example this code:
Code: [Select]
var counter;
counter = 0;
for (var i=0; i<1000000; ++i){
counter+=1;
}
If I put that in step event I only get 74FPS. But if I change "var" to a real type like "int" by doing this:
Code: [Select]
int counter;
counter = 0;
for (int i=0; i<1000000; ++i){
counter+=1;
}
I get 2250FPS. That is a performance increase of 30x. In GM you cannot do this as it has only generic variant type (which is the default one in ENIGMA as well). But in EDL by assigning a specific type C++ style you can greatly speed up the code. But then you are in C++ territory and can no longer type 'int counter = 0; counter = "Foobar"' as that would be a compilation error (only generic type "var" can be changed from real to string and string to real).
EDL in the future will also things like structs and classes, but I personally don't miss them in ENIGMA, so I don't really care.

Quote
Could someone give me some examples of how C++ code in my projects would benefit me when I have all the GML functions to use?
Also, C++ isn't really about "functions". It doesn't have any. It is a programming language, so all it provides is you with syntax and semantic to write something. It does come with some basic functionality (called STL or Standart Template Library) which include data structures like lists, maps, vectors and so on. But usually you either write everything by yourself or you use something others have written (including API's). So imagine C++ basically as ENIGMA without any built-in functions, but only support for Extensions.

That is why I also often say how you can do whatever you want in ENIGMA and lacking of some single feature doesn't make it unusable or worse. Because in C++ you either write your own or find someone who already has done so, but you don't go to C++ standard committee and tell them how you really want a shader library. So we would like users to write extensions that implement a lot of functionality, not directly needed in the engine. That also allows different implementations by different people. Like the particle system is an extension right now. So it would be possible for another person to write a particle system that uses only GPU and then you would just Enable it and Disable the old one. And whola, without changing anything in your code (or code of the base engine) you got a particle system working in a totally different way.
« Last Edit: April 22, 2014, 06:35:24 AM by TheExDeus » Logged
Offline (Unknown gender) Darkstar2
Reply #3 Posted on: April 22, 2014, 12:17:53 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
Thanks for the explanations, so in this example if I use the for / loop to read bytes from a big file, it would be much faster ?

BTW

Quote
int counter;
counter = 0;
for (int i=0; i<1000000; ++i){
   counter+=1;
}

I thought that was the usual way of using FOR with int, it's even documented that way in GM.
Logged
Offline (Male) Goombert
Reply #4 Posted on: April 22, 2014, 12:58:20 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Actually you want unsigned int, because it is never negative, or size_t.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #5 Posted on: April 22, 2014, 01:42:09 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
Looking at  code above I am wondering in this case why not do this?

for (int counter=0; counter<1000000; ++counter)

Logged
Offline (Unknown gender) TheExDeus
Reply #6 Posted on: April 22, 2014, 02:30:34 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Quote
Thanks for the explanations, so in this example if I use the for / loop to read bytes from a big file, it would be much faster ?
Yes. There the I/O could be a bottleneck, but you should still see some kind of improvement. So you should declare types whenever you can. The general purpose "var" is very big (can be any size - from 1byte to n bytes, but maybe Josh could say more precisely) and slow (I guess because the compiler cannot easily optimize it like it would basic data types).

Quote
I thought that was the usual way of using FOR with int, it's even documented that way in GM.
Where in the GM documentation? I think you missed the point on what I did there. I changed "var" into "int". GM doesn't allow that. It only has "var". I could of changed it into "double" or "char" or "size_t" or anything else if I wanted to. But those types are what makes it so fast.

Quote
Actually you want unsigned int, because it is never negative, or size_t.
I personally think using unsigned variables are becoming a bad practice quite rapidly. They generate A LOT of errors, especially for newbies, and so they are not encouraged. They were required when you actually needed the extra "size" (like when int was only 65536), but now when int's are +-2,147,483,647 which is usually "enough". Soon it will be better to just use double everywhere. Like if you wanted the loop to work in reverse:
Code: [Select]
int counter;
counter = 0;
for (unsigned int i=1000000; i>=0; --i){
   counter+=1;
}
This is would end up as infinite loop. And most newbies wouldn't even figure out why. I guess this is why there are no unsigned types in Java. Speed wise unsigned and signed is more complex. unsigned can be optimized in some interesting ways (like http://stackoverflow.com/a/12225693) but they can be slower to cast to float (http://plog.sesse.net/blog/tech/2011-07-27-10-37_the_micro_optimization_corner_unsigned_to_float.html).

But I guess if you make a forward going loop you should use size_t, as it usually also gives you a good indication on what the loop is doing (that it accesses arrays usually).

Another reason is because you would have to explicitly cast unsigned to signed for comparison. Like if you run ENIGMA the first time (when engine is compiled) the majority of warnings (and there are A LOT of them.. we should probably get on that) is because we compare signed and unsigned variables. If we used signed everywhere we wouldn't have that problem and yet the code would usually run the same (as we don't really go over 2.1 billion mark in resources, instance count, text glyphs (even with Unicode ranges) and so on).

edit:
Quote
for (int counter=0; counter<1000000; ++counter)
You could. The reason why I didn't is because an empty for loop sometimes could be just optimized out. Read this: http://punchlet.wordpress.com/2011/07/01/letter-the-eighth-bedevilling-benchmarks/
So while it probably wouldn't do it in Run mode (which isn't optimized) it maybe would do it Compile mode. So for illustrating a point I made it so the loop wouldn't be optimized out.
« Last Edit: April 22, 2014, 02:48:39 PM by TheExDeus » Logged
Offline (Male) Goombert
Reply #7 Posted on: April 22, 2014, 02:47:49 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Harri, there are unsigned types in Java, C# added them, now Java 8 added them.
https://blogs.oracle.com/darcy/entry/unsigned_api
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Male) Josh @ Dreamland
Reply #8 Posted on: April 27, 2014, 10:36:21 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2958

View Profile Email
There are a lot of benefits to a more authentic object-oriented paradigm. I'll write up some clear and useful documentation for them after they are actually implemented. Right now, our C++ feature set is pretty limited.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) Darkstar2
Reply #9 Posted on: April 27, 2014, 10:55:07 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
Ok thanks, I was just curious.   At first when it was advertized as support C++ and GML, I thought you
could actually do full C++ set.  In any case I am quite far from the stage to do anything significant in C++ other than guses my number games :D  But eventually it is something I want to learn more of.

Logged
Offline (Male) Goombert
Reply #10 Posted on: April 27, 2014, 11:10:40 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
It's not that far off really, you can already use proper data types, pointers, macros, and small structs.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Pages: 1
  Print