Gamemaker Studio 2 Gml File
// Create Event enum states { IDLE, WALK, JUMP, ATTACK } state = states.IDLE; // Step Event switch (state) { case states.IDLE: scr_idle(); break; case states.WALK: scr_walk(); break; case states.JUMP: scr_jump(); break; } Can you publish a game using only Drag and Drop? Yes. Hyper Light Drifter used DnD? No. Undertale ? No.
// Creating variables health = 100; player_name = "Hero"; is_alive = true; // Local variables (self-cleaning at event end) var ammo = 30;
// Bad global.hp = 10; global.mp = 5; global.gold = 100; // Good global.game = { hp: 10, mp: 5, gold: 100 }; Macros ( #macro ) are processed before compilation. Use them for game balance values. gamemaker studio 2 gml
So, open GameMaker, create a new Object, open the Create Event, and type:
// Create a struct var player_stats = { level: 5, hp: 100, attack: function(enemy) { enemy.hp -= 10; } }; // Call the function inside the struct player_stats.attack(some_enemy); You can now write true Object-Oriented GML. // Create Event enum states { IDLE, WALK,
#macro GRAVITY 0.5 #macro MAX_SPEED 12 #macro JUMP_FORCE -7 Don't write 500-line Step events. Use an Enum and a state variable.
// If statement if (keyboard_check(vk_space) && jumps > 0) { vspeed = -10; jumps--; } // Switch statement (Great for state machines) switch (state) { case "idle": sprite_index = spr_idle; break; case "run": sprite_index = spr_run; break; } // Creating variables health = 100; player_name =
// For loop for (var i = 0; i < 10; i++) { show_debug_message("Iteration: " + string(i)); } The most confusing aspect of GML for newcomers is understanding scope —which instance is running the code. Dot Syntax (Direct Access) If you know the specific instance ID, you use a dot.