Skip to content

Type-safe signals for engine classes #1111

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

Merged
merged 4 commits into from
Mar 30, 2025
Merged

Type-safe signals for engine classes #1111

merged 4 commits into from
Mar 30, 2025

Conversation

Bromeon
Copy link
Member

@Bromeon Bromeon commented Mar 30, 2025

Following up #1000, this pull request exposes all signals declared in Godot engine classes.
Closes #1108.

For example, Node has the child_entered_tree signal:

image

To connect such a signal in Rust, we can simply do:

let mut node: Gd<Node> = /* some node in tree */;

node.signals()
    .child_entered_tree()
    .connect(|child_node| {
        println!("A child node entered the tree: {child_node}");
    });

In other words, it's the exact same API as before.

This PR also comes with other small changes:

  1. WithSignals trait is split into WithSignals (all signal-enabled classes) and WithUserSignals (only #[signal] user-defined ones).
  2. Gd::signals() now takes &mut instead of &. This is mostly for consistency with self.signals(), as well as Godot's own APIs Object::connect, Object::emit etc. all operating on &mut self. Technically it makes not much difference, but signal manipulation being linked to mutation has a higher chance of detecting unintended changes.

@Bromeon Bromeon added feature Adds functionality to the library c: engine Godot classes (nodes, resources, ...) labels Mar 30, 2025
@Bromeon Bromeon changed the title Type-safe signal for engine classes Type-safe signals for engine classes Mar 30, 2025
@GodotRust
Copy link

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

@Bromeon Bromeon added this pull request to the merge queue Mar 30, 2025
Merged via the queue into master with commit 8340843 Mar 30, 2025
46 checks passed
@Bromeon Bromeon deleted the feature/engine-signals branch March 30, 2025 21:18
@Bromeon
Copy link
Member Author

Bromeon commented Mar 30, 2025

Next plan: making signals in base classes accessible. At the moment, Gd<T>::signals() only returns signals from the class T itself, but it should be possible to connect/emit those in superclasses as well.

Potential ideas:

  • Duplicating signals (preferably avoided)
  • Deref/DerefMut of the collection type to its base class' type
  • Explicit selection à la obj.signals_of::<BaseClass>().some_signal()

ColinWttt added a commit to ColinWttt/godot_await that referenced this pull request Mar 31, 2025
@Bromeon Bromeon mentioned this pull request Jan 7, 2025
@Bromeon Bromeon mentioned this pull request Apr 21, 2025
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.

Type-safe signals from Godot-defined structs
2 participants