From aa28782be391902e55131524f8d242b70888eb2c Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Fri, 5 Jul 2024 09:40:46 +0200 Subject: [PATCH] [GDScript] Fix `get_argument_count` for lambda `Callable`s --- modules/gdscript/gdscript_lambda_callable.cpp | 4 ++-- .../features/lambda_bind_argument_count.gd | 18 ++++++++++++++++++ .../features/lambda_bind_argument_count.out | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.out diff --git a/modules/gdscript/gdscript_lambda_callable.cpp b/modules/gdscript/gdscript_lambda_callable.cpp index 626ef6ccb01e..42b0b066e124 100644 --- a/modules/gdscript/gdscript_lambda_callable.cpp +++ b/modules/gdscript/gdscript_lambda_callable.cpp @@ -84,7 +84,7 @@ int GDScriptLambdaCallable::get_argument_count(bool &r_is_valid) const { return 0; } r_is_valid = true; - return function->get_argument_count(); + return function->get_argument_count() - captures.size(); } void GDScriptLambdaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { @@ -204,7 +204,7 @@ int GDScriptLambdaSelfCallable::get_argument_count(bool &r_is_valid) const { return 0; } r_is_valid = true; - return function->get_argument_count(); + return function->get_argument_count() - captures.size(); } void GDScriptLambdaSelfCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { diff --git a/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.gd b/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.gd new file mode 100644 index 000000000000..67225cad6add --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.gd @@ -0,0 +1,18 @@ +# https://github.com/godotengine/godot/issues/93952 + +func foo(): + pass + +func test(): + var a: int + + var lambda_self := func (x: int) -> void: + foo() + print(a, x) + + print(lambda_self.get_argument_count()) # Should print 1. + + var lambda_non_self := func (x: int) -> void: + print(a, x) + + print(lambda_non_self.get_argument_count()) # Should print 1. diff --git a/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.out b/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.out new file mode 100644 index 000000000000..04b4638adf6a --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/lambda_bind_argument_count.out @@ -0,0 +1,3 @@ +GDTEST_OK +1 +1