Implicit binds in virtual calls cause borrow errors · Issue #338 · godot-rust/gdext (original) (raw)
Edit bromeon -- title was "Cannot addchild
in a &mut self
function if Self
overrides onnotification
"
#[derive(GodotClass)] #[class(init, base = Node)] pub struct Bar { #[base] base: Base, }
#[godot_api] impl NodeVirtual for Bar { fn ready(&mut self) { if Engine::singleton().is_editor_hint() { return; }
let foo = Node::new_alloc();
self.add_child(foo.share().upcast());
}
fn on_notification(&mut self, what: NodeNotification) {}
}
add_child
will trigger a notification (https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-constant-notification-child-order-changed), and that will in turn trigger a call to on_notification
, which will try to get a mutable borrow of self
. This causes a crash.
It seems like this is a big issue tbh because it basically makes self.add_child
almost unusable whenever on_notification
is overridden.
I'm not sure what a good solution to this is, but it's related to the common issue of calling a signal that will trigger on self.