GameMaker (GML) Cheat Sheet
A one-page printable reference for variables, events, movement, collision and instances the most-used GML snippets in GameMaker. Pin it next to your monitor!
A one-page printable reference for variables, events, movement, collision and instances the most-used GML snippets in GameMaker. Pin it next to your monitor!
// instance variable (Create Event) hp = 100; move_speed = 4; // global variable, accessible anywhere globalvar score; global.score = 0; // local variable, this scope only var dist = point_distance(x, y, target.x, target.y);
Use global. for values shared across all objects, like score or game state.
| Create | Runs once when the instance is spawned |
| Step | Runs every frame logic, input, movement |
| Draw | Runs every frame drawing only |
| Collision | Runs when this instance touches another object |
| Destroy | Runs once when the instance is destroyed |
| Alarm[0] | Runs once when its countdown reaches 0 |
// direct speed/direction direction = 90; speed = 4; // component speeds hspeed = 3; vspeed = -2; // move toward a point smoothly move_towards_point(target.x, target.y, 4); // simple keyboard movement hspeed = (keyboard_check(vk_right) - keyboard_check(vk_left)) * move_speed;
if (place_meeting(x, y, obj_wall)) { // touching a wall at current position move_bounce_solid(true); } var hit = instance_place(x, y, obj_enemy); if (hit != noone) { instance_destroy(hit); } if (collision_rectangle(x-16, y-16, x+16, y+16, obj_spike, false, true)) { hp -= 10; }
// spawn a new instance on a layer instance_create_layer(x, y, "Instances", obj_bullet); // destroy this instance instance_destroy(); // run code on every instance of a type with (obj_enemy) { hp -= 5; }
// Draw Event draw_sprite(spr_player, image_index, x, y); draw_set_color(c_white); draw_text(10, 10, "Score: " + string(global.score)); draw_set_color(c_red); draw_rectangle(x-16, y-16, x+16, y+16, false);
if (hp <= 0) { instance_destroy(); } else if (hp < 20) { show_debug_message("Low HP!"); } else { show_debug_message("OK"); } for (var i = 0; i < 5; i++) { } while (ammo > 0) { ammo--; } switch (state) { case "idle": sprite_index = spr_idle; break; case "run": sprite_index = spr_run; break; default: break; }
// --- Create Event --- move_speed = 4; fire_rate = 20; fire_timer = 0; // --- Step Event --- var mx = (keyboard_check(vk_right) - keyboard_check(vk_left)); var my = (keyboard_check(vk_down) - keyboard_check(vk_up)); hspeed = mx * move_speed; vspeed = my * move_speed; fire_timer -= 1; if (mouse_check_button(mb_left) && fire_timer <= 0) { var ang = point_direction(x, y, mouse_x, mouse_y); var b = instance_create_layer(x, y, "Instances", obj_bullet); b.direction = ang; b.speed = 10; fire_timer = fire_rate; } // --- obj_bullet Collision Event with obj_enemy --- instance_destroy(other); instance_destroy(self);
Follow one of our free step-by-step workshops in the Workshop hub.
๐ง Browse Workshops โ