|
4 | 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +/// Extension API for Godot classes, used with `#[godot_api]`. |
| 8 | +/// |
| 9 | +/// Helps with adding custom functionality: |
| 10 | +/// * `init` constructors |
| 11 | +/// * `to_string` method |
| 12 | +/// * Custom register methods (builder style) |
| 13 | +/// * All the lifecycle methods like `ready`, `process` etc. |
| 14 | +/// |
| 15 | +/// These trait are special in that it needs to be used in combination with the `#[godot_api]` |
| 16 | +/// proc-macro attribute to ensure proper registration of its methods. All methods have |
| 17 | +/// default implementations, so you can select precisely which functionality you want to have. |
| 18 | +/// Those default implementations are never called however, the proc-macro detects what you implement. |
| 19 | +/// |
| 20 | +/// Do not call any of these methods directly -- they are an interface to Godot. Functionality |
| 21 | +/// described here is available through other means (e.g. `init` via `Gd::new_default`). |
| 22 | +/// It is not enough to impl `GodotExt` to be registered in Godot, for this you should look at |
| 23 | +/// [ExtensionLibrary](crate::init::ExtensionLibrary). |
| 24 | +/// |
| 25 | +/// If you wish to create a struct deriving GodotClass, you should impl the trait <Base>Virtual, |
| 26 | +/// for your desired Base (i.e. `RefCountedVirtual`, `NodeVirtual`). |
| 27 | +/// |
| 28 | +/// # Examples |
| 29 | +/// |
| 30 | +/// ## Example with `RefCounted` as a base |
| 31 | +/// |
| 32 | +/// ``` |
| 33 | +///# use godot::prelude::*; |
| 34 | +/// |
| 35 | +/// #[derive(GodotClass)] |
| 36 | +/// struct MyRef; |
| 37 | +/// |
| 38 | +/// #[godot_api] |
| 39 | +/// impl MyRef { |
| 40 | +/// #[func] |
| 41 | +/// pub fn hello_world(&mut self) { |
| 42 | +/// godot_print!("Hello World!") |
| 43 | +/// } |
| 44 | +/// } |
| 45 | +/// |
| 46 | +/// #[godot_api] |
| 47 | +/// impl RefCountedVirtual for MyRef { |
| 48 | +/// fn init(_: Base<RefCounted>) -> Self { |
| 49 | +/// MyRef |
| 50 | +/// } |
| 51 | +/// } |
| 52 | +/// ``` |
| 53 | +/// |
| 54 | +/// The following example allows to use MyStruct in GDScript for instance by calling |
| 55 | +/// `MyStruct.new().hello_world()`. |
| 56 | +/// |
| 57 | +/// |
| 58 | +/// Note that you have to implement init otherwise you won't be able to call new or any |
| 59 | +/// other methods from GDScript. |
| 60 | +/// |
| 61 | +/// ## Example with `Node` as a Base |
| 62 | +/// |
| 63 | +/// ``` |
| 64 | +///# use godot::prelude::*; |
| 65 | +/// |
| 66 | +/// #[derive(GodotClass)] |
| 67 | +/// #[class(base=Node)] |
| 68 | +/// pub struct MyNode { |
| 69 | +/// #[base] |
| 70 | +/// base: Base<Node>, |
| 71 | +/// } |
| 72 | +/// |
| 73 | +/// #[godot_api] |
| 74 | +/// impl NodeVirtual for MyNode { |
| 75 | +/// fn init(base: Base<Node>) -> Self { |
| 76 | +/// MyNode { base } |
| 77 | +/// } |
| 78 | +/// fn ready(&mut self) { |
| 79 | +/// godot_print!("Hello World!"); |
| 80 | +/// } |
| 81 | +/// } |
| 82 | +/// ``` |
| 83 | +/// |
7 | 84 | use crate::util::ident;
|
8 | 85 | use proc_macro::TokenStream;
|
9 | 86 | use proc_macro2::TokenStream as TokenStream2;
|
|
0 commit comments