|
Post by Dragonite on Sept 27, 2016 6:21:08 GMT -6
Got any neat code tricks or tips that a lot of people might not know about in GameMaker? Know of any useful obscure functions that aren't as well known as you think they should be? Share them here! 1. script_executeIt sounds funny, but I probably wouldn't be able to use GameMaker if it wasn't for the ability to execute variables as scripts. script_execute(variable, argument0, argument1 .. argumentn); It doesn't seem like much at first but it lets you pull off some pretty complicated polymorphism stuff that you might be used to in other programming languages in GML, which otherwise lacks object orientation features like class methods or functions. 2. Data structure accessorsDo you do a lot of lookups in ds_lists or grids and wish there was an easier way than typing out ds_list_find_value over and over? Instead of this variable = ds_list_find_value(list, value); you can do this list[| value]; // that's the symbol you get from shift+backslash, the "broken pipe," you might recognize it from the logical "or" You can also use a # for grids, a ? for maps and @ to access the original copy of an array if you passed it as a function argument. Useful, right? 3. dsin, dcos, dtan, etc.Do you do a lot of trigonometry? Do you wish you didn't have to keep translating between degrees (used for most angle measurements in GameMaker) and radians (used for the trigonometry functions and that's about it)? x = a * cos(degtorad(angle)); A (somewhat) recent update to GM introduced degree forms of those functions, which do the exact same trigonometry but expect degree arguments instead of radians. Saves a bit of typing! x = a * dcos(angle);
|
|
KurtBlissZ
GM Intermediate
"Just Another Waffle"
Posts: 55
|
Post by KurtBlissZ on Sept 27, 2016 12:39:56 GMT -6
So here’s two things I do just for convenience. 4. Macros (yal.cc) Macros can be used as constants but they are not a read only variable or even a variable. All it does it replace wherever your placed the macro with its value. So, for instance you could use it for controls, you can make a macro called CON_UP set its value to keyboard_check(vk_up). You can also just make the macro a variable to. So for example you can make a macro called… Player_Name and set it to global._Player_Name_. Then you can define global._Player_Name_ by typing Player_Name = “Kurt”. Then of course you could draw you player name to, draw_text(10, 10, Player_Name); 5. Enums I actually google searched this for the docs link, but I also happened to find Dragonite's video about it too. So I shall just cheat and put it here, Though when using enums I prefer having a prefix just so I don’t have to remember everything. I would do this enum Player { State_Idle, State_Running, State_Attacking }
|
|
|
Post by nujumkey on Sept 27, 2016 14:04:57 GMT -6
If this is a tricks and tips game then my best one is
If you get an error, 95% its because you typed something wrong. wrong expression ( > instead of >= ) wrong variable ( gravity instead of grav ) wrong calculations ( Average = X * Y / 2 instead of (X * Y)/2 )
Often when i encounter logic bugs in my games it's one of these three. They are extremely hard to pin down, so i recommend watching for these if you're frustrated.
|
|
|
Post by GMLWaffle on Sept 27, 2016 14:20:52 GMT -6
90% of my errors are misused functions
|
|
|
Post by Dragonite on Sept 27, 2016 15:13:50 GMT -6
90% of mine are typing "vraible" instead of "variable." Too bad GM doesn't support syntax highlighting on variable names.
|
|
KurtBlissZ
GM Intermediate
"Just Another Waffle"
Posts: 55
|
Post by KurtBlissZ on Sept 29, 2016 17:19:20 GMT -6
Here's another thing that makes things a little easier.
var k_left = keyboard_check(vk_left); k_left |= keyboard_check(ord("A"));
if k_left x--;
Now if you press the left arrow key or the A key k_left returns true.
If you didn't know already you could do this too
var k_left = keyboard_check(vk_left) || keyboard_check(ord("A")); if k_left x--;
I found doing |= really helpful when adding in custom controls for my asteroids game like this,
var k_left = keyboard_check(vk_left) || keyboard_check(ord("A")); var k_right = keyboard_check(vk_right) || keyboard_check(ord("D")); k_up = keyboard_check(vk_up) || keyboard_check(ord("W")); var k_shoot = keyboard_check(vk_space);
var g = 0; do if gamepad_is_connected(g) { k_left |= (gamepad_axis_value(g, gp_axislh) < -0.5); k_right |= (gamepad_axis_value(g, gp_axislh) > 0.5);
k_left |= gamepad_button_check(g, gp_padl); k_right |= gamepad_button_check(g, gp_padr);
k_up |= gamepad_button_check(g, gp_face1); k_shoot |= gamepad_button_check(g, gp_face2);
k_up |= gamepad_button_check(g, gp_face4); k_shoot |= gamepad_button_check(g, gp_face3);
g++; } until (!gamepad_is_connected(g))
|
|
|
Post by illdiewithoutpi on Sept 29, 2016 20:52:52 GMT -6
Man, I didn't even know about |=. I assume from the way you're using it that this:
var1 |= var2; is the same as this:
var1 = var1 || var2;
Is that right?
|
|
|
Post by Dragonite on Sept 29, 2016 22:12:33 GMT -6
Close. It's a bitwise or, so it would be more like a = a | b; Which is designed to work on the individual bits of a number rather than just true/false values (I would totally anchor link these if hyperlinks were underlined . . . wonder if I can fix that): en.wikipedia.org/wiki/Bitwise_operation
|
|