Module discovery
Expand description
Allows dependent crates to statically discover classes registered by this extension.
§Rationale
The idea is to provide a mechanism that allows tools and integrations to query information about an extension at build time.
For example, this allows validations, data extraction/generation, etc. It is limited to downstream crates depending on the crate declaring
the extension, in particular their build.rs
file. If a crate is used for discovery, then it must declare crate-type = ["cdylib", "rlib"]
,
i.e. both a C dynamic library for GDExtension and a Rust library.
The API is kept deliberately minimal – it does not strive to cover the entire reflection API that Godot provides. Many tools may be better
fitted as a direct integration into the Godot editor, or as runtime code querying Godot’s ClassDB
API. If you believe this API is lacking,
please provide a detailed use case.
§Usage
To make use of discovery, your entry point trait needs to have the discover
attribute:
/// Your tag must be public.
pub struct MyCoolGame;
/// The `discovery` attributes adds an associated `MyExtension::discover()` function to the `MyExtension` tag.
/// It also declares a module `godot_discovery` in the current scope, which re-exports symbols from `godot::init::discovery`.
/// This allows your dependent crate to not depend on `godot` directly.
#[gdextension(discovery)]
unsafe impl ExtensionLibrary for MyCoolGame {}
To access the exposed API in a dependent crate in build.rs
, you can call MyExtension::discover()
:
use my_crate::MyCoolGame;
use my_crate::godot_discovery::DiscoveredExtension;
fn main() {
let api: DiscoveredExtension = MyCoolGame::discover();
for c in api.classes() {
println!("Discovered class {}.", c.name());
}
}