Skip to content
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

adds doc tests to Godot Ext #139

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions godot-core/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

/// Pushes a warning message to Godot's built-in debugger and to the OS terminal.
///
/// _Godot equivalent: @GlobalScope.push_warning()_
#[macro_export]
macro_rules! godot_warn {
($fmt:literal $(, $args:expr)* $(,)?) => {
Expand All @@ -21,6 +24,9 @@ macro_rules! godot_warn {
};
}

/// Pushes an error message to Godot's built-in debugger and to the OS terminal.
///
/// _Godot equivalent: @GlobalScope.push_error()_
#[macro_export]
macro_rules! godot_error {
// FIXME expr needs to be parenthesised, see usages
Expand Down Expand Up @@ -57,6 +63,9 @@ macro_rules! godot_script_error {
};
}

/// Prints to the Godot console.
///
/// _Godot equivalent: @GlobalScope.print()_
#[macro_export]
macro_rules! godot_print {
($fmt:literal $(, $args:expr)* $(,)?) => {
Expand All @@ -75,6 +84,7 @@ pub use crate::{godot_error, godot_print, godot_script_error, godot_warn};
use crate::builtin::{StringName, Variant};
use crate::sys::{self, GodotFfi};

/// Prints to the Godot console, used by the godot_print! macro.
pub fn print(varargs: &[Variant]) {
unsafe {
let method_name = StringName::from("print");
Expand Down
77 changes: 77 additions & 0 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,83 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

/// Extension API for Godot classes, used with `#[godot_api]`.
///
/// Helps with adding custom functionality:
/// * `init` constructors
/// * `to_string` method
/// * Custom register methods (builder style)
/// * All the lifecycle methods like `ready`, `process` etc.
///
/// These trait are special in that it needs to be used in combination with the `#[godot_api]`
/// proc-macro attribute to ensure proper registration of its methods. All methods have
/// default implementations, so you can select precisely which functionality you want to have.
/// Those default implementations are never called however, the proc-macro detects what you implement.
///
/// Do not call any of these methods directly -- they are an interface to Godot. Functionality
/// described here is available through other means (e.g. `init` via `Gd::new_default`).
/// It is not enough to impl `GodotExt` to be registered in Godot, for this you should look at
/// [ExtensionLibrary](crate::init::ExtensionLibrary).
///
/// If you wish to create a struct deriving GodotClass, you should impl the trait <Base>Virtual,
/// for your desired Base (i.e. `RefCountedVirtual`, `NodeVirtual`).
///
/// # Examples
///
/// ## Example with `RefCounted` as a base
///
/// ```
///# use godot::prelude::*;
///
/// #[derive(GodotClass)]
/// struct MyRef;
///
/// #[godot_api]
/// impl MyRef {
/// #[func]
/// pub fn hello_world(&mut self) {
/// godot_print!("Hello World!")
/// }
/// }
///
/// #[godot_api]
/// impl RefCountedVirtual for MyRef {
/// fn init(_: Base<RefCounted>) -> Self {
/// MyRef
/// }
/// }
/// ```
///
/// The following example allows to use MyStruct in GDScript for instance by calling
/// `MyStruct.new().hello_world()`.
///
///
/// Note that you have to implement init otherwise you won't be able to call new or any
/// other methods from GDScript.
///
/// ## Example with `Node` as a Base
///
/// ```
///# use godot::prelude::*;
///
/// #[derive(GodotClass)]
/// #[class(base=Node)]
/// pub struct MyNode {
/// #[base]
/// base: Base<Node>,
/// }
///
/// #[godot_api]
/// impl NodeVirtual for MyNode {
/// fn init(base: Base<Node>) -> Self {
/// MyNode { base }
/// }
/// fn ready(&mut self) {
/// godot_print!("Hello World!");
/// }
/// }
/// ```
///
use crate::util::ident;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
Expand Down