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

New syntax for #[export] methods and #[base] parameters #872

Merged
merged 12 commits into from
May 10, 2022
16 changes: 8 additions & 8 deletions examples/dodge-the-creeps/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl Hud {
Hud
}

#[export]
pub fn show_message(&self, owner: &CanvasLayer, text: String) {
#[godot]
pub fn show_message(&self, #[base] owner: &CanvasLayer, text: String) {
let message_label = unsafe { owner.get_node_as::<Label>("message_label").unwrap() };
message_label.set_text(text);
message_label.show();
Expand All @@ -37,21 +37,21 @@ impl Hud {
button.show();
}

#[export]
pub fn update_score(&self, owner: &CanvasLayer, score: i64) {
#[godot]
pub fn update_score(&self, #[base] owner: &CanvasLayer, score: i64) {
let label = unsafe { owner.get_node_as::<Label>("score_label").unwrap() };
label.set_text(score.to_string());
}

#[export]
fn on_start_button_pressed(&self, owner: &CanvasLayer) {
#[godot]
fn on_start_button_pressed(&self, #[base] owner: &CanvasLayer) {
let button = unsafe { owner.get_node_as::<Button>("start_button").unwrap() };
button.hide();
owner.emit_signal("start_game", &[]);
}

#[export]
fn on_message_timer_timeout(&self, owner: &CanvasLayer) {
#[godot]
fn on_message_timer_timeout(&self, #[base] owner: &CanvasLayer) {
let message_label = unsafe { owner.get_node_as::<Label>("message_label").unwrap() };
message_label.hide()
}
Expand Down
20 changes: 10 additions & 10 deletions examples/dodge-the-creeps/src/main_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl Main {
}
}

#[export]
fn game_over(&self, owner: &Node) {
#[godot]
fn game_over(&self, #[base] owner: &Node) {
let score_timer = unsafe { owner.get_node_as::<Timer>("score_timer").unwrap() };
let mob_timer = unsafe { owner.get_node_as::<Timer>("mob_timer").unwrap() };

Expand All @@ -38,8 +38,8 @@ impl Main {
.unwrap_or_else(|| godot_print!("Unable to get hud"));
}

#[export]
fn new_game(&mut self, owner: &Node) {
#[godot]
fn new_game(&mut self, #[base] owner: &Node) {
let start_position = unsafe { owner.get_node_as::<Position2D>("start_position").unwrap() };
let player = unsafe {
owner
Expand All @@ -66,16 +66,16 @@ impl Main {
.unwrap_or_else(|| godot_print!("Unable to get hud"));
}

#[export]
fn on_start_timer_timeout(&self, owner: &Node) {
#[godot]
fn on_start_timer_timeout(&self, #[base] owner: &Node) {
let mob_timer = unsafe { owner.get_node_as::<Timer>("mob_timer").unwrap() };
let score_timer = unsafe { owner.get_node_as::<Timer>("score_timer").unwrap() };
mob_timer.start(0.0);
score_timer.start(0.0);
}

#[export]
fn on_score_timer_timeout(&mut self, owner: &Node) {
#[godot]
fn on_score_timer_timeout(&mut self, #[base] owner: &Node) {
self.score += 1;

let hud = unsafe { owner.get_node_as_instance::<hud::Hud>("hud").unwrap() };
Expand All @@ -84,8 +84,8 @@ impl Main {
.unwrap_or_else(|| godot_print!("Unable to get hud"));
}

#[export]
fn on_mob_timer_timeout(&self, owner: &Node) {
#[godot]
fn on_mob_timer_timeout(&self, #[base] owner: &Node) {
let mob_spawn_location = unsafe {
owner
.get_node_as::<PathFollow2D>("mob_path/mob_spawn_locations")
Expand Down
12 changes: 6 additions & 6 deletions examples/dodge-the-creeps/src/mob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Mob {
}
}

#[export]
fn _ready(&mut self, owner: &RigidBody2D) {
#[godot]
fn _ready(&mut self, #[base] owner: &RigidBody2D) {
let mut rng = rand::thread_rng();
let animated_sprite = unsafe {
owner
Expand All @@ -51,15 +51,15 @@ impl Mob {
animated_sprite.set_animation(MOB_TYPES.choose(&mut rng).unwrap().to_str())
}

#[export]
fn on_visibility_screen_exited(&self, owner: &RigidBody2D) {
#[godot]
fn on_visibility_screen_exited(&self, #[base] owner: &RigidBody2D) {
unsafe {
owner.assume_unique().queue_free();
}
}

#[export]
fn on_start_game(&self, owner: &RigidBody2D) {
#[godot]
fn on_start_game(&self, #[base] owner: &RigidBody2D) {
unsafe {
owner.assume_unique().queue_free();
}
Expand Down
16 changes: 8 additions & 8 deletions examples/dodge-the-creeps/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ impl Player {
}
}

#[export]
fn _ready(&mut self, owner: &Area2D) {
#[godot]
fn _ready(&mut self, #[base] owner: &Area2D) {
let viewport = owner.get_viewport_rect();
self.screen_size = viewport.size;
owner.hide();
}

#[export]
fn _process(&mut self, owner: &Area2D, delta: f32) {
#[godot]
fn _process(&mut self, #[base] owner: &Area2D, delta: f32) {
let animated_sprite = unsafe {
owner
.get_node_as::<AnimatedSprite>("animated_sprite")
Expand Down Expand Up @@ -88,8 +88,8 @@ impl Player {
owner.set_global_position(position);
}

#[export]
fn on_player_body_entered(&self, owner: &Area2D, _body: Ref<PhysicsBody2D>) {
#[godot]
fn on_player_body_entered(&self, #[base] owner: &Area2D, _body: Ref<PhysicsBody2D>) {
owner.hide();
owner.emit_signal("hit", &[]);

Expand All @@ -102,8 +102,8 @@ impl Player {
collision_shape.set_deferred("disabled", true);
}

#[export]
pub fn start(&self, owner: &Area2D, pos: Vector2) {
#[godot]
pub fn start(&self, #[base] owner: &Area2D, pos: Vector2) {
owner.set_global_position(pos);
owner.show();

Expand Down
4 changes: 2 additions & 2 deletions examples/hello-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ impl HelloWorld {
HelloWorld
}

#[export]
fn _ready(&self, _owner: &Node) {
#[godot]
fn _ready(&self) {
godot_print!("hello, world.")
}
}
Expand Down
16 changes: 8 additions & 8 deletions examples/native-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl CustomNode {
CustomNode
}

#[export]
fn _enter_tree(&self, owner: TRef<EditorPlugin>) {
#[godot]
fn _enter_tree(&self, #[base] owner: TRef<EditorPlugin>) {
// Initialization of the plugin goes here.
// Add the new type with a name, a parent type, a script and an icon.
let script = unsafe { load::<Script>("res://my_button.gdns", "Script").unwrap() };
Expand All @@ -25,8 +25,8 @@ impl CustomNode {
owner.add_custom_type("MyButton", "Button", script, texture);
}

#[export]
fn _exit_tree(&self, owner: TRef<EditorPlugin>) {
#[godot]
fn _exit_tree(&self, #[base] owner: TRef<EditorPlugin>) {
// Clean-up of the plugin goes here.
// Always remember to remove it from the engine when deactivated.
owner.remove_custom_type("MyButton");
Expand All @@ -43,15 +43,15 @@ impl MyButton {
MyButton
}

#[export]
fn _enter_tree(&self, owner: TRef<Button>) {
#[godot]
fn _enter_tree(&self, #[base] owner: TRef<Button>) {
owner
.connect("pressed", owner, "clicked", VariantArray::new_shared(), 0)
.unwrap();
}

#[export]
fn clicked(&self, _owner: TRef<Button>) {
#[godot]
fn clicked(&self) {
godot_print!("You clicked me!");
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl Greeter {
}
}

#[export]
fn _ready(&self, _owner: &Node) {
#[godot]
fn _ready(&self) {
if let Some(greeting_resource) = self.greeting_resource.as_ref() {
let greeting_resource = unsafe { greeting_resource.assume_safe() };
greeting_resource.map(|s, o| s.say_hello(&*o)).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions examples/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl ServerPuppet {
Self
}

#[export]
fn _ready(&mut self, owner: TRef<Node>) {
#[godot]
fn _ready(&mut self, #[base] owner: TRef<Node>) {
let peer = NetworkedMultiplayerENet::new();
peer.create_client(
GodotString::from(ADDRESS),
Expand All @@ -44,13 +44,13 @@ impl ServerPuppet {
.unwrap();
}

#[export]
fn on_connected_to_server(&mut self, owner: TRef<Node>) {
#[godot]
fn on_connected_to_server(&mut self, #[base] owner: TRef<Node>) {
owner.rpc("greet_server", &[Variant::new("hello")]);
}

#[export(rpc = "puppet")]
fn return_greeting(&mut self, _owner: &Node, msg: GodotString) {
#[godot(rpc = "puppet")]
fn return_greeting(&mut self, msg: GodotString) {
godot_print!("Server says: {}", msg);
}
}
8 changes: 4 additions & 4 deletions examples/rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl Server {
Self
}

#[export]
fn _ready(&mut self, owner: &Node) {
#[godot]
fn _ready(&mut self, #[base] owner: &Node) {
let peer = NetworkedMultiplayerENet::new();
peer.create_server(PORT, MAX_CLIENTS, IN_BANDWIDTH, OUT_BANDWIDTH)
.unwrap();
Expand All @@ -28,8 +28,8 @@ impl Server {
tree.set_network_peer(peer);
}

#[export(rpc = "master")]
fn greet_server(&mut self, owner: &Node, msg: GodotString) {
#[godot(rpc = "master")]
fn greet_server(&mut self, #[base] owner: &Node, msg: GodotString) {
godot_print!("Client says: {}", msg);

let tree = owner.get_tree().expect("could not retreive Scene Tree");
Expand Down
12 changes: 6 additions & 6 deletions examples/scene-create/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ impl SceneCreate {
}
}

#[gdnative::derive::export]
fn _ready(&mut self, _owner: &Spatial) {
#[gdnative::derive::godot]
fn _ready(&mut self) {
self.template = load_scene("res://Child_scene.tscn");
match &self.template {
Some(_scene) => godot_print!("Loaded child scene successfully!"),
None => godot_print!("Could not load child scene. Check name."),
}
}

#[gdnative::derive::export]
fn spawn_one(&mut self, owner: &Spatial, message: GodotString) {
#[gdnative::derive::godot]
fn spawn_one(&mut self, #[base] owner: &Spatial, message: GodotString) {
godot_print!("Called spawn_one({})", message.to_string());

let template = if let Some(template) = &self.template {
Expand Down Expand Up @@ -77,8 +77,8 @@ impl SceneCreate {
update_panel(owner, num_children);
}

#[gdnative::derive::export]
fn remove_one(&mut self, owner: &Spatial, str: GodotString) {
#[gdnative::derive::godot]
fn remove_one(&mut self, #[base] owner: &Spatial, str: GodotString) {
godot_print!("Called remove_one({})", str);
let num_children = owner.get_child_count();
if num_children <= 0 {
Expand Down
16 changes: 8 additions & 8 deletions examples/signals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl SignalEmitter {
}
}

#[export]
fn _process(&mut self, owner: &Node, delta: f64) {
#[godot]
fn _process(&mut self, #[base] owner: &Node, delta: f64) {
if self.timer < 1.0 {
self.timer += delta;
return;
Expand Down Expand Up @@ -57,8 +57,8 @@ impl SignalSubscriber {
SignalSubscriber { times_received: 0 }
}

#[export]
fn _ready(&mut self, owner: TRef<Label>) {
#[godot]
fn _ready(&mut self, #[base] owner: TRef<Label>) {
let emitter = &mut owner.get_node("../SignalEmitter").unwrap();
let emitter = unsafe { emitter.assume_safe() };

Expand All @@ -76,16 +76,16 @@ impl SignalSubscriber {
.unwrap();
}

#[export]
fn notify(&mut self, owner: &Label) {
#[godot]
fn notify(&mut self, #[base] owner: &Label) {
self.times_received += 1;
let msg = format!("Received signal \"tick\" {} times", self.times_received);

owner.set_text(msg);
}

#[export]
fn notify_with_data(&mut self, owner: &Label, data: Variant) {
#[godot]
fn notify_with_data(&mut self, #[base] owner: &Label, data: Variant) {
let msg = format!(
"Received signal \"tick_with_data\" with data {}",
data.try_to::<u64>().unwrap()
Expand Down
8 changes: 4 additions & 4 deletions examples/spinning-cube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl RustTest {
}
}

#[export]
fn _ready(&mut self, owner: &MeshInstance) {
#[godot]
fn _ready(&mut self, #[base] owner: &MeshInstance) {
owner.set_physics_process(true);
}

#[export]
fn _physics_process(&mut self, owner: &MeshInstance, delta: f64) {
#[godot]
fn _physics_process(&mut self, #[base] owner: &MeshInstance, delta: f64) {
use gdnative::api::SpatialMaterial;

self.time += delta as f32;
Expand Down
Loading