### on_stage_init() Logic Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/06-initialization.md Provides an example of custom logic to execute when different initialization stages are loaded. This includes registering classes and setting up autoloads. ```rust fn on_stage_init(stage: InitStage) { match stage { InitStage::Core => { godot_print!("Core stage initialized"); } InitStage::Servers => { godot_print!("Servers stage initialized"); } InitStage::Scene => { godot_print!("Scene stage initialized"); // Register classes, setup autoloads, etc. } InitStage::Editor => { godot_print!("Editor stage initialized"); } #[cfg(since_api = "4.5")] InitStage::MainLoop => { godot_print!("MainLoop stage initialized - fully ready"); } } } ``` -------------------------------- ### Example: Custom GameConfig Resource Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/10-virtual-methods-callbacks.md Example of a custom `Resource` named `GameConfig` implementing the `IResource` trait. The `setup` method is overridden to print a message. ```rust #[derive(GodotClass)] #[class(init, base=Resource)] struct GameConfig { base: Base, #[export] level_count: i32, } #[godot_api] impl IResource for GameConfig { fn setup(&mut self) { godot_print!("Initializing config"); } } ``` -------------------------------- ### Minimal Setup for a Simple Class Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/09-prelude-and-common-imports.md Use this minimal setup for defining a simple class with basic functionality. It imports the prelude and derives `GodotClass`. ```rust use godot::prelude::*; #[derive(GodotClass)] #[class(init)] struct MyClass { base: Base, } #[godot_api] impl MyClass { #[func] fn my_method(&self) { godot_print!("Hello!"); } } ``` -------------------------------- ### Full ExtensionLibrary Implementation Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/06-initialization.md A complete example demonstrating how to implement the `ExtensionLibrary` trait for a custom GDExtension. It shows how to set initialization levels, editor behavior, and stage-specific callbacks. ```rust use godot::init::* struct MyExtension; #[gdextension] unsafe impl ExtensionLibrary for MyExtension { fn min_level() -> InitLevel { InitLevel::Scene } fn editor_run_behavior() -> EditorRunBehavior { EditorRunBehavior::ToolClassesOnly } fn on_stage_init(stage: InitStage) { match stage { InitStage::Scene => { godot_print!("Initializing game extension"); // Setup singletons, cache frequently-used nodes, etc. } InitStage::Editor => { godot_print!("Editor tools ready"); } _ => {} } } fn on_stage_deinit(stage: InitStage) { if stage == InitStage::Scene { godot_print!("Cleaning up"); } } #[cfg(since_api = "4.5")] fn on_main_loop_frame() { // Per-frame update logic } } ``` -------------------------------- ### Complete Player Class Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/04-registration-macros.md A comprehensive example demonstrating the registration of a `Player` struct, including initialization, exported properties with ranges, and on-ready fields. ```rust #[derive(GodotClass)] #[class(init, base=Node2D)] struct Player { base: Base, #[init(val = 100)] health: i32, #[var] name: GString, #[export] #[export(range = (0.0, 500.0))] speed: f32, #[init(node = "HealthBar")] health_bar: OnReady>, } ``` -------------------------------- ### on_stage_deinit() Logic Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/06-initialization.md Provides an example of custom logic to execute when different initialization stages are unloaded. This includes cleaning up resources. ```rust fn on_stage_deinit(stage: InitStage) { match stage { InitStage::Editor => { godot_print!("Cleaning up editor resources"); } InitStage::Scene => { godot_print!("Cleaning up scene resources"); } // ... } } ``` -------------------------------- ### Async Signal Reception Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/05-signals.md Demonstrates how to use SignalReceiver to asynchronously wait for and process signals in a loop. ```rust async fn wait_for_event() { let obj: Gd = MyClass::new_gd(); while let Some(value) = obj.signals().value_changed().recv().await { godot_print!("Value: {}", value); } } ``` -------------------------------- ### on_main_loop_frame() Example (Godot 4.5+) Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/06-initialization.md An example of a function called every frame after initialization is complete, for Godot versions 4.5 and later. It runs after all `Node::process()` calls. ```rust #[cfg(since_api = "4.5")] fn on_main_loop_frame() { // Per-frame logic, called after all process() methods } ``` -------------------------------- ### Color Example: HSV Conversion and Darkening Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/03-builtin-types.md Demonstrates creating a color from HSV values and then darkening it. ```rust let color = Color::from_hsv(0.5, 0.8, 1.0, 1.0); // Cyan let darkened = color.darkened(0.2); ``` -------------------------------- ### Player Node Implementation with Callbacks Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/10-virtual-methods-callbacks.md Example of implementing Node lifecycle callbacks for a Player node, including ready, process, and input handling. ```rust #[derive(GodotClass)] #[class(init, base=Node2D)] struct Player { base: Base, speed: f32, } #[godot_api] impl INode2D for Player { fn ready(&mut self) { godot_print!("Player ready! Setting up..."); } fn process(&mut self, delta: f64) { // Called every frame let velocity = Vector2::new(0.0, 0.0); // Update position based on input... self.base_mut().set_position( self.base().get_position() + velocity * delta as f32 ); } fn physics_process(&mut self, delta: f64) { // Physics updates (0.016s interval) } fn input(&mut self, event: Gd) { // Handle input if let Some(key_event) = event.clone().try_cast::() { let key = key_event.bind().get_keycode(); godot_print!("Key pressed: {:?}", key); } } } ``` -------------------------------- ### Connecting to Node Tree Signals Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/05-signals.md Example of connecting to 'tree_entered' and 'tree_exited' signals on a Node using Callable::from_fn. ```rust impl INode for MyNode { fn ready(&mut self) { let base = self.base_mut(); // tree_entered: emitted when node enters scene tree base.connect("tree_entered".into(), Callable::from_fn(|| { godot_print!("In tree!"); })); // tree_exited: emitted when node leaves scene tree base.connect("tree_exited".into(), Callable::from_fn(|| { godot_print!("Out of tree!"); })); } } ``` -------------------------------- ### FromGodot Type Conversion Example Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/07-type-conversions.md Demonstrates converting Godot integer and string types to their Rust equivalents using the `from_godot` method. ```rust let godot_int: i32 = 42; let rust_int: i32 = i32::from_godot(godot_int); let godot_str: GString = GString::from("hello"); let rust_str: String = String::from_godot(godot_str); ``` -------------------------------- ### Get Frame Setup Time CPU Source: https://github.com/godot-rust/gdext/blob/master/godot-codegen/src/formatter/test-cases/rendering_server.rs.txt Retrieves the CPU time taken for the current frame's setup. This method is part of the RenderingServer module. ```rust pub fn get_frame_setup_time_cpu (& self ,) -> f64 { unsafe { let __class_name = StringName :: from ("RenderingServer") ; let __method_name = StringName :: from ("get_frame_setup_time_cpu") ; let __method_bind = sys :: interface_fn ! (classdb_get_method_bind) (__class_name . string_sys () , __method_name . string_sys () , 1740695150i64) ; assert ! (! __method_bind . is_null () , "failed to load method {}::{} (hash {}) -- possible Godot/gdext version mismatch" , "RenderingServer" , "get_frame_setup_time_cpu" , 1740695150i64) ; let __call_fn = sys :: interface_fn ! (object_method_bind_ptrcall) ; let __args = [] ; let __args_ptr = __args . as_ptr () ; < f64 as sys :: GodotFfi > :: from_sys_init_default (| return_ptr | { __call_fn (__method_bind , self . object_ptr , __args_ptr , return_ptr) } ) } } ``` -------------------------------- ### Get CPU Frame Setup Time Source: https://github.com/godot-rust/gdext/blob/master/godot-codegen/src/formatter/test-cases/rendering_server.rs.txt Retrieves the time taken on the CPU to set up the current frame. This metric is valuable for performance analysis and identifying rendering bottlenecks. ```rust pub fn get_frame_setup_time_cpu (& self ,) -> f64 { unsafe { let __class_name = StringName :: from ("RenderingServer") ; let __method_name = StringName :: from ("get_frame_setup_time_cpu") ; let __method_bind = sys :: interface_fn ! (classdb_get_method_bind) (__class_name . string_sys () , __method_name . string_sys () , 1740695150i64) ; assert ! (! __method_bind . is_null () , "failed to load method {}::{} (hash {}) -- possible Godot/gdext version mismatch" , "RenderingServer" , "get_frame_setup_time_cpu" , 1740695150i64) ; let __call_fn = sys :: interface_fn ! (object_method_bind_ptrcall) ; let __args = [] ; let __args_ptr = __args . as_ptr () ; < f64 as sys :: GodotFfi > :: from_sys_init_default (| return_ptr | { __call_fn (__method_bind , self . object_ptr , __args_ptr , return_ptr) }) } } ``` -------------------------------- ### Implement IButton Lifecycle Source: https://github.com/godot-rust/gdext/blob/master/_autodocs/10-virtual-methods-callbacks.md Implement the `ready` method for a custom Button node. ```rust #[derive(GodotClass)] #[class(init, base=Button)] struct MyButton { base: Base