For the complete documentation index, see llms.txt. This page is also available as Markdown.

Projectiles

CSL does not have a built-in projectile component. Use a prefab with a script for movement and either a trigger collider or a range query for hits.

Trigger-based projectile

Create a prefab with:

  • A sprite or Spine_Animator

  • A Circle_Collider with Is Trigger enabled

  • The component below

Damageable :: class : Component {
    health: float = 100 @ao_serialize;

    take_damage :: method(amount: float) {
        health -= amount;
    }
}

Projectile :: class : Component {
    direction: v2;
    owner: Entity;
    speed: float = 12 @ao_serialize;
    damage: float = 25 @ao_serialize;
    lifetime: float = 3 @ao_serialize;
    spent: bool;

    ao_start :: method() {
        collider := entity.get_component(Circle_Collider);
        collider.is_trigger = true;
        collider.on_trigger_start = proc(self: Collider, other: Collider) {
            projectile := self.entity.get_component(Projectile);
            projectile.hit(other.entity);
        };

        entity.queue_for_destruction(lifetime);
    }

    ao_update :: method(dt: float) {
        entity.add_local_position(direction * speed * dt);
    }

    hit :: method(other: Entity) {
        if spent return;
        if other == owner return;

        target := other.get_component(Damageable);
        if target == null return;

        spent = true;
        target.take_damage(damage);
        entity.destroy();
    }
}

Spawn the prefab with Scene.instantiate, then initialize its component:

Run projectile movement and damage in the normal shared gameplay path. Prediction runs the same code on clients and server, so do not guard it with Game.is_server().

Fast projectiles

Trigger overlap checks can miss a small target when a projectile crosses it between simulation frames. For fast projectiles, query several points along the movement segment with Scene.get_all_components_in_range, or use a larger collision radius. See Navmesh and Collision for a sub-stepping example.

Call SFX.play from the same predicted hit path. The sound system reconciles the predicted sound with the server result.

Last updated