Skip to content

Commit 3b36394

Browse files
committed
Virtual functions now take Option<Gd<T>> instead of Gd<T>
Sometimes inconvenient, but the only way if no nullability information is provided. Exceptions can be worked out over time.
1 parent b8fe7f9 commit 3b36394

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

godot-codegen/src/generator/functions_common.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ pub(crate) fn make_params_exprs<'a>(
322322
let param_name = &param.name;
323323
let param_ty = &param.type_;
324324

325-
// Objects (Gd<T>) use implicit conversions via AsObjectArg. Only use in non-virtual functions.
326325
match &param.type_ {
326+
// Non-virtual functions: Objects (Gd<T>) use implicit conversions via AsObjectArg.
327327
RustTy::EngineClass {
328328
object_arg,
329329
impl_as_object_arg,
@@ -346,6 +346,14 @@ pub(crate) fn make_params_exprs<'a>(
346346
param_types.push(quote! { #object_arg });
347347
}
348348

349+
// Virtual methods accept Option<Gd<T>>, since we don't know whether objects are nullable or required.
350+
RustTy::EngineClass { .. } if is_virtual => {
351+
params.push(quote! { #param_name: Option<#param_ty> });
352+
arg_names.push(quote! { #param_name });
353+
param_types.push(quote! { #param_ty });
354+
}
355+
356+
// All other methods and parameter types: standard handling.
349357
_ => {
350358
params.push(quote! { #param_name: #param_ty });
351359
arg_names.push(quote! { #param_name });

itest/rust/src/object_tests/virtual_methods_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ impl IPrimitiveMesh for VirtualReturnTest {
153153
fn surface_get_format(&self, _index: i32) -> u32 { unreachable!() }
154154
fn surface_get_primitive_type(&self, _index: i32) -> u32 { unreachable!() }
155155
#[cfg(feature = "codegen-full")]
156-
fn surface_set_material(&mut self, _index: i32, _material: Gd<Material>) { unreachable!() }
156+
fn surface_set_material(&mut self, _index: i32, _material: Option<Gd<Material>>) { unreachable!() }
157157
#[cfg(feature = "codegen-full")]
158158
fn surface_get_material(&self, _index: i32) -> Option<Gd<Material>> { unreachable!() }
159159
fn get_blend_shape_count(&self) -> i32 { unreachable!() }
160160
fn get_blend_shape_name(&self, _index: i32) -> StringName { unreachable!() }
161-
fn set_blend_shape_name(&mut self, _index: i32, _namee: StringName) { unreachable!() }
161+
fn set_blend_shape_name(&mut self, _index: i32, _name: StringName) { unreachable!() }
162162
fn get_aabb(&self) -> godot::prelude::Aabb { unreachable!() }
163163
}
164164

0 commit comments

Comments
 (0)