You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
234: Fix cast always succeeding due to Godot bug. r=Bromeon a=joshua-maros
See #158 .
Currently, casting always succeeds due to a bug in Godot. This PR adds an explicit (slow) check to make sure that the cast is valid before performing it. This should mirror the behavior that Godot will have when the bug is fixed upstream. This is better than the fix mentioned in #158 as that fix only works when the object you are casting is *exactly* the class you are casting to, you could not cast a derived class to a parent class. This PR has no such limitation.
I tested it with the following code:
```rust
#[derive(GodotClass)]
#[class(init)]
pub struct TestClass {}
#[godot_api]
impl TestClass {}
#[derive(GodotClass)]
#[class(init)]
pub struct OtherRefCountedClass {}
#[godot_api]
impl OtherRefCountedClass {}
#[derive(GodotClass)]
#[class(init)]
pub struct TestClassRunner {
#[export]
#[init(default = Gd::new(TestClass {}))]
pub tc: Gd<TestClass>,
}
#[godot_api]
impl TestClassRunner {
#[func]
pub fn tests(&self) {
godot_print!("{:?}", self.tc.share().upcast::<Object>());
godot_print!("{:?}", self.tc.share().upcast::<RefCounted>());
godot_print!(
"{:?}",
self.tc.share().upcast::<Object>().cast::<TestClass>()
);
godot_print!(
"{:?}",
self.tc.share().upcast::<Object>().try_cast::<Node>()
);
godot_print!(
"{:?}",
self.tc
.share()
.upcast::<Object>()
.try_cast::<OtherRefCountedClass>()
);
}
}
```
And constructing the runner and running the function in Godot produced the following output:
```
Gd { id: -9223372008501280223, class: TestClass }
Gd { id: -9223372008501280223, class: TestClass }
Gd { id: -9223372008501280223, class: TestClass }
None
None
```
Co-authored-by: joshua-maros <60271685+joshua-maros@users.noreply.github.com>
0 commit comments