Skip to content

match_class! macro to dispatch subclasses #1225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Bromeon
Copy link
Member

@Bromeon Bromeon commented Jul 3, 2025

This declarative macro allows to dispatch a class to a variety of different subclasses, without tedious application of Gd::try_cast().

Basic usage:

let event: Gd<InputEvent> = some_input();

let simple_dispatch: i32 = match_class!(event, {
   InputEventMouseButton(btn) => 1,
   InputEventMouseMotion(motion) => 2,
   InputEventAction(action) => 3,
   _ => 0, // Fallback.
});

More diverse dispatch patterns are also supported:

let fancy_dispatch: i32 = match_class!(some_input(), {
    InputEventMouseButton(btn) => 1,

    // Block syntax for multiple statements:
    InputEventMouseMotion(motion) => {
        godot_print!("motion");
        2
    },

    // Qualified types supported:
    godot::classes::InputEventAction(action) => 3,

    // Fallback with variable -- retrieves original Gd<InputEvent>.
    // This pattern is equivalent to `InputEvent(original)`.
    _(original) => 0,

    // Alternatively, you can use `_` without a variable to discard the value.
});

@Bromeon Bromeon added feature Adds functionality to the library c: engine Godot classes (nodes, resources, ...) labels Jul 3, 2025
@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1225

@Bromeon Bromeon force-pushed the feature/match-class branch from 4afc54e to f95cac9 Compare July 3, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: engine Godot classes (nodes, resources, ...) feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants