๐ Events
// Core lifecycle events
Create_0 - instance created (init vars)
Step_0 - runs every frame
Draw_0 - runs every frame (draw)
// Alarms (0-11)
Alarm_0 through Alarm_11 - delayed actions
// Collision
Collision_obj_wall - when touching obj_wall
// Keyboard & Mouse
KeyPress_vk_left - key just pressed
KeyRelease_vk_right - key just released
Mouse_*_button - mouse events
// Room
Room Start / Room End
Game Start / Game End
๐ Built-in Variables
| x / y | Instance position in the room |
| speed | Current movement speed |
| direction | Movement direction (0-360 degrees) |
| hspeed / vspeed | Horizontal / vertical speed |
| image_index | Current animation frame |
| image_speed | Animation playback speed |
| image_xscale / image_yscale | Sprite scale |
| image_angle | Sprite rotation angle |
| sprite_index | Current sprite assigned |
| room_width / room_height | Room dimensions |
โ๏ธ Functions
// Instance management
instance_create_layer(x, y, layer, obj);
instance_destroy();
// Place checking
place_free(x, y);
place_meeting(x, y, obj);
// Distance & direction
distance_to_point(x, y);
point_direction(x1, y1, x2, y2);
move_towards_point(x, y, speed);
// Physics
gravity - gravity direction & strength
friction - slows movement on surfaces
// Setting alarms
alarm[0] = 60; // fire in 60 frames
โ๏ธ Drawing
| draw_sprite() | Draw a sprite at position |
| draw_sprite_ext() | Draw with scale, rotation, alpha |
| draw_text() | Draw text at position |
| draw_text_transformed() | Draw text with scale/rotation |
| draw_rectangle() | Draw outline or filled rectangle |
| draw_circle() | Draw outline or filled circle |
| draw_line() | Draw a line between two points |
| draw_set_color() | Set current drawing colour |
| draw_set_font() | Set current font |
| draw_set_halign() | Set horizontal text alignment |
| draw_set_valign() | Set vertical text alignment |
๐ฎ Input
// Keyboard
keyboard_check(vk_left); // held
keyboard_check_pressed(vk_space); // just pressed
// Mouse
mouse_check_button(mb_left); // held
mouse_x / mouse_y // position
// Common keys
vk_left, vk_right, vk_up, vk_down
vk_space, vk_enter, vk_escape
vk_shift, vk_control
ord("A") // letter keys
๐ Sound
| audio_play_sound() | Play a sound asset |
| audio_stop_sound() | Stop a playing sound |
| audio_resume_sound() | Resume a paused sound |
| audio_is_playing() | Check if sound is playing |
| audio_sound_gain() | Set volume (with fade) |
// Play sound effect
audio_play_sound(snd_jump, 1, false);
// Loop music
audio_play_sound(mus_level, 10, true);
// Fade out
audio_sound_gain(mus_level, 0, 2000);
// Check before playing
if (!audio_is_playing(mus_bg)) {
audio_play_sound(mus_bg, 10, true);
}
๐ Data Structures
// Lists (dynamic arrays)
ds_list_create();
ds_list_add(list, value);
ds_list_size(list);
ds_list_destroy(list);
// Maps (key-value pairs)
ds_map_create();
ds_map_add(map, key, value);
ds_map_find_value(map, key);
ds_map_destroy(map);
// Stacks (LIFO)
ds_stack_create();
ds_stack_push(stack, value);
ds_stack_pop(stack);
// Queues (FIFO)
ds_queue_create();
ds_queue_enqueue(queue, value);
ds_queue_dequeue(queue);
๐ฒ Random
| irandom(n) | Random integer 0 to n (inclusive) |
| irandom_range(a, b) | Random integer from a to b |
| random(n) | Random float 0 to n |
| random_range(a, b) | Random float from a to b |
| choose(a, b, ...) | Pick randomly from arguments |
// Examples
var roll = irandom(5); // 0-5
var dmg = irandom_range(10, 20);
var dir = choose(0, 90, 180, 270);
var rnd = random(1.0); // 0.0-1.0
๐ข Math
| abs() | Absolute value |
| min() / max() | Minimum / Maximum of values |
| clamp() | Clamp value between min and max |
| ceil() | Round up to integer |
| floor() | Round down to integer |
| round() | Round to nearest integer |
| lengthdir_x() | X component from length + direction |
| lengthdir_y() | Y component from length + direction |
| point_distance() | Distance between two points |
// Directional movement
x += lengthdir_x(speed, direction);
y += lengthdir_y(speed, direction);
// Distance check
if (point_distance(x, y, target_x, target_y) < 32) {
// close enough
}
// Clamp health
hp = clamp(hp, 0, max_hp);
๐ ๏ธ Common Patterns
โโ PLAYER MOVEMENT โโ
// Step event
if (keyboard_check(vk_left)) x -= 4;
if (keyboard_check(vk_right)) x += 4;
if (keyboard_check(vk_up)) y -= 4;
if (keyboard_check(vk_down)) y += 4;
โโ ENEMY AI โโ
// Step event: chase player
var _dir = point_direction(x, y, obj_player.x, obj_player.y);
x += lengthdir_x(2, _dir);
y += lengthdir_y(2, _dir);
โโ COLLISION CHECK โโ
if (place_meeting(x + hspeed, y, obj_wall)) {
hspeed = 0;
}
if (place_meeting(x, y + vspeed, obj_wall)) {
vspeed = 0;
}
โโ SPAWNING โโ
if (keyboard_check_pressed(vk_space)) {
instance_create_layer(x, y, "Instances", obj_bullet);
}
โโ SCREEN SHAKE โโ
// Create event
shake = 0;
// Call when hit
shake = 6;
// Step event
if (shake > 0) {
camera_set_view_pos(view_camera[0],
random_range(-shake, shake),
random_range(-shake, shake));
shake -= 0.5;
}
โโ CAMERA FOLLOW โโ
// Step event
var _cx = clamp(obj_player.x - 320, 0, room_width - 640);
var _cy = clamp(obj_player.y - 240, 0, room_height - 480);
camera_set_view_pos(view_camera[0], _cx, _cy);