Moving_from_EDL_to_C - hpgDesigns/hpgdesigns-dev.io GitHub Wiki
This will be a guide on moving from EDL to C++. Mainly highlighting all the stuff inherited from gml which you can't do in C++. Declarations, statements, strings etc. I think Josh's previous text would actually fit better in this topic, the language will need to be changed to say that these things are compulsory.
- If all you'll ever need my_var to be is an integer, then declare
local int my_var
. - Say what you mean. Don't test
if (health = 0)
. Health = 0 is an assignment; the = symbol assigns. Use == to compare. - Use switch statements correctly. Don't use variables as case labels.
case x > 5
: is not a case label.
GML offers three methods of declaring variables.
// You can declare a variable implicitly at the object scope simply by assigning to it.
my_local_variable = 10;
show_message(string(my_local_variable));
// You can also declare a variable in the scope of the current code
var my_temporary_variable;
my_temporary_variable = 20;
show_message(string(my_temporary_variable));
// GM also allows declaring a global variable in later versions
globalvar my_global_variable = 30;
show_message(string(my_global_variable));
In EDL, each of these translates with types.
// You can still declare a variable implicitly
my_local_variable = 10;
show_message(string(my_local_variable));
// But you can also declare local variables explicitly, with a type.
local int my_local_integer = 10;
show_message(string(my_local_variable));
// You can easily declare typed variables in the scope of the current code
int my_temporary_integer = 20; // EDL does not require you to separate the declaration and initialization
show_message(string(my_temporary_variable));
// To reduce keywords, EDL re-uses global and local for declaring out-of-scope
global int my_global_integer = 30;
show_message(string(my_global_integer));
While EDL does not mandate semicolons, it is designed to be stricter with operators than GML.
Consider this piece of GML:
target = instance_nearest(x,y,obj_target);
if (target = noone) {
standby = true;
round = round++1;
}
The code checks for a nearby target. If it's found, the game continues, otherwise, it sets the instance's standby mode to true and increments the round. The comparison to check if there was no target is done with a single equals-sign operator instead of the standard double. Also, instead of using the += operator to increment the round, it was set to the expression "round++1", which GM interprets as round plus positive 1. By implementing a stricter operator syntax, this code can be simplified in terms of lines used.
Consider this piece of EDL which does the same thing:
if ((target = instance_nearest(x,y,obj_target)) == noone) {
standby = true;
round++;
}
While in this instance, the difference is not particularly impressive, the opportunities to write simpler code grow with each situation, and the amount of code one must type decreases. One of the "crowd favorites" is for loops.
for (var i = 0; i < 10; i++)
show_message(string(i));
I don't feel like typing this part.