Registering constants
Constants can be used to share fixed values from Rust code to the Godot engine.
See also GDScript reference for constants.
Constant declaration
Constants are declared as const
items in Rust, inside the inherent impl
block of a class.
The attribute #[constant]
makes it available to Godot.
#![allow(unused)] fn main() { #[godot_api] impl Monster { #[constant] const DEFAULT_HP: i32 = 100; #[func] fn from_name_hp(name: GString, hitpoints: i32) -> Gd<Self> { ... } } }
Usage in GDScript would look as follows:
var nom = Monster.from_name_hp("Nomster", Monster.DEFAULT_HP)
var orc = Monster.from_name_hp("Orc", 200)
(This particular example might be better suited for default parameters once they are implemented, but it illustrates the point.)
Statics
static
fields can currently not be registered as constants.