godot-rust / gdext

Rust bindings for Godot 4
https://mastodon.gamedev.place/@GodotRust
Mozilla Public License 2.0
3.06k stars 190 forks source link

Godot passes an Object Variant with a null value when clearing Object subclass properties #77

Closed mhoff12358 closed 1 year ago

mhoff12358 commented 1 year ago

Given an exported property like this:

#[export(
    getter = "get_building_pattern",
    setter = "set_building_pattern",
    variant_type = "::godot::sys::VariantType::Object"
)]
building_pattern: Option<Gd<TileMapPattern>>,

with a setter like this:

#[func]
pub fn set_building_pattern(&mut self, value: Variant) {
    let converted: Result<Gd<TileMapPattern>, VariantConversionError> = value.try_to();
}

This will crash during try_to because it creates a Gd with the null pointer inside it and then it calls ready on it.

mhoff12358 commented 1 year ago

This is related to https://github.com/godot-rust/gdnative/pull/1002 from gdnative. That was solved by making Variants check for this case and say that they're of type nil rather than object. That is probably a good solution for this, but we'll also need a way to have the setter take in an Option<Gd<T>> rather than a Gd<T>.