Skip to content

Collision Result GC Leading to Engine Crash in GodotJS #216

@zwa73

Description

@zwa73

Version

4.1-v0.0.20-20240906

System information

Windows11

Issue description

export default class TestScript extends godot.PhysicsBody2D {
    collink: any;

    _ready() {
        this.set_physics_process(true);  // Enable physics processing
    }

    _physics_process(delta: number): void {
        let velocity = new godot.Vector2();
        if (godot.Input.is_action_pressed("up")) velocity.y -= 1;
        if (godot.Input.is_action_pressed("down")) velocity.y += 1;
        if (godot.Input.is_action_pressed("left")) velocity.x -= 1;
        if (godot.Input.is_action_pressed("right")) velocity.x += 1;

        // Normalize velocity vector and scale by speed and delta time
        if (velocity.length() > 0) {
            velocity.x *= 5;
            velocity.y *= 5;

            // Update position and handle collisions
            // Without storing the collision result, the object is garbage collected (GC)
            // which leads to game crashes on subsequent collision checks
            const res = this.move_and_collide(velocity, false, 0.08, true);
            if (res) this.collink = res;  // Store collision result to prevent GC
            console.log('res', res);
        }
    }
}

Steps to reproduce

Just testing the above code

Minimal reproduction project (MRP)

game.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions