|
| 1 | + |
| 2 | + |
| 3 | +# Rust bindings for Godot 4 |
| 4 | + |
| 5 | +_**[Website]** | **[GitHub]** | **[Book]** | **[API Docs]** | [Discord] | [Mastodon] | [Twitter] | [Sponsor]_ |
| 6 | + |
| 7 | +The **godot** crate integrates the Rust language with Godot 4. |
| 8 | + |
| 9 | +[Godot] is an open-source game engine, focusing on a productive and batteries-included 2D and 3D experience. |
| 10 | +Its _GDExtension_ API allows integrating third-party languages and libraries. |
| 11 | + |
| 12 | + |
| 13 | +## Philosophy |
| 14 | + |
| 15 | +The Rust binding is an alternative to GDScript, with a focus on type safety, scalability and performance. |
| 16 | + |
| 17 | +The primary goal of this library is to provide a [**pragmatic Rust API**][philosophy] for game developers. |
| 18 | +Recurring workflows should be simple and require minimal boilerplate. APIs are designed to be safe and idiomatic Rust wherever possible. |
| 19 | +Due to interacting with Godot as a C++ engine, we sometimes follow unconventional approaches to provide a good user experience. |
| 20 | + |
| 21 | + |
| 22 | +## Example |
| 23 | + |
| 24 | +The following code snippet demonstrates writing a simple Godot class `Player` in Rust. |
| 25 | + |
| 26 | +```rust |
| 27 | +use godot::prelude::*; |
| 28 | +use godot::classes::{ISprite2D, Sprite2D}; |
| 29 | + |
| 30 | +// Declare the Player class inheriting Sprite2D. |
| 31 | +#[derive(GodotClass)] |
| 32 | +#[class(base=Sprite2D)] |
| 33 | +struct Player { |
| 34 | + // Inheritance via composition: access to Sprite2D methods. |
| 35 | + base: Base<Sprite2D>, |
| 36 | + |
| 37 | + // Other fields. |
| 38 | + velocity: Vector2, |
| 39 | + hitpoints: i32, |
| 40 | +} |
| 41 | + |
| 42 | +// Implement Godot's virtual methods via predefined trait. |
| 43 | +#[godot_api] |
| 44 | +impl ISprite2D for Player { |
| 45 | + // Default constructor (base object is passed in). |
| 46 | + fn init(base: Base<Sprite2D>) -> Self { |
| 47 | + Player { |
| 48 | + base, |
| 49 | + velocity: Vector2::ZERO, |
| 50 | + hitpoints: 100, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Override the `_ready` method. |
| 55 | + fn ready(&mut self) { |
| 56 | + godot_print!("Player ready!"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Implement custom methods that can be called from GDScript. |
| 61 | +#[godot_api] |
| 62 | +impl Player { |
| 63 | + #[func] |
| 64 | + fn take_damage(&mut self, damage: i32) { |
| 65 | + self.hitpoints -= damage; |
| 66 | + godot_print!("Player hit! HP left: {}", self.hitpoints); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## More |
| 72 | + |
| 73 | +For more information, check out our [Website] or [GitHub] page! |
| 74 | + |
| 75 | + |
| 76 | +[API Docs]: https://godot-rust.github.io/docs/gdext |
| 77 | +[Book]: https://godot-rust.github.io/book |
| 78 | +[Discord]: https://discord.gg/aKUCJ8rJsc |
| 79 | +[GitHub]: https://github.com/godot-rust/gdext |
| 80 | +[Godot]: https://godotengine.org |
| 81 | +[Mastodon]: https://mastodon.gamedev.place/@GodotRust |
| 82 | +[philosophy]: https://godot-rust.github.io/book/contribute/philosophy.html |
| 83 | +[Sponsor]: https://github.com/sponsors/Bromeon |
| 84 | +[Twitter]: https://twitter.com/GodotRust |
| 85 | +[Website]: https://godot-rust.github.io |
0 commit comments