HT Games
Home  >> Games

Implementing Bullet Damage in GameMaker Studio 2 (GML) — Clean, Scalable Approach

Here’s a clean, scalable approach to bullet damage in GameMaker Studio 2.3+ using GML.

1. Bullet Object (obj_bullet)

Create Event:

speed = 16; direction = image_angle; // or set via instance_create_layer
damage = 10; // adjustable per bullet type
owner = id; // optional: track who fired for scoring/team

Collision Event with obj_enemy_parent:

// Apply damage
with (other) {
    hp -= other.damage; // or use a script: event_user(0, damage)
    // Hit feedback
    hit_flash = 5; // frames of white flash
    // Knockback (optional)
    hsp = lengthdir_x(4, other.direction);
    vsp = lengthdir_y(4, other.direction);
}
// Visual/audio feedback
effect_create_above(EF_SMOKE, x, y, 0, c_gray); // or custom particle
audio_play_sound(snd_impact, 1, false);
// Destroy bullet
instance_destroy();

2. Enemy Parent Object (obj_enemy_parent)

Create Event:

hp = 100; max_hp = 100; hit_flash = 0;

Step Event:

if (hit_flash > 0) hit_flash--;
// Death check
if (hp <= 0) {
    // Death effects, drops, score
    instance_create_layer(x, y, "Effects", obj_death_particle);
    audio_play_sound(snd_death, 1, false);
    with (obj_player) score += 100; // example
    instance_destroy();
}

Draw Event (Hit Flash):

if (hit_flash > 0) {
    gpu_set_blendmode(bm_add);
    draw_self();
    gpu_set_blendmode(bm_normal);
} else {
    draw_self();
}

3. Child Enemies

Create obj_enemy_basic, obj_enemy_boss as children of obj_enemy_parent. Override hp, max_hp, death behavior in their Create Events.

4. Advanced: Damage Types / Resistance

// Bullet damage = 10; dmg_type = DMG_PHYSICAL; // enum: PHYSICAL, FIRE, ICE, ELECTRIC
// Enemy fire_resist = 0.5; // takes 50% fire dmg
// ice_weak = 1.5; // takes 150% ice dmg
// In collision:
var mult = 1;
switch (other.dmg_type) {
    case DMG_FIRE: mult = fire_resist; break;
    case DMG_ICE: mult = ice_weak; break;
}
hp -= other.damage * mult;

Key Principles

  • Parent/Child: Use obj_enemy_parent for shared logic; one collision event handles all enemies.
  • Encapsulation: Damage logic lives on enemy (via with(other)), not bullet.
  • Extensibility: Add armor, shields, status effects, multi-hit, piercing via variables/scripts.
  • Performance: Deactivate off-screen bullets; use collision_list for piercing.

https://www.htfbw.com © HT Games