[Method Bind] Add support for default argument values and static method binding. Sync headers.

This commit is contained in:
bruvzg
2022-05-04 14:27:08 +03:00
parent 24e4aeb2c6
commit 031a83b0ea
11 changed files with 794 additions and 115 deletions

View File

@@ -2,32 +2,49 @@ extends Node
func _ready():
# Bind signals
prints("Signal bind")
$Button.button_up.connect($Example.emit_custom_signal.bind("Button", 42))
prints("")
# Call static methods.
prints("Static method calls")
prints(" static (109)", Example.test_static(9, 100));
Example.test_static2();
# Call methods.
prints("Instance method calls")
$Example.simple_func()
($Example as Example).simple_const_func() # Force use of ptrcall
prints("returned", $Example.return_something("some string"))
prints("returned const", $Example.return_something_const())
prints("returned ref", $Example.return_extended_ref())
prints(" returned", $Example.return_something("some string"))
prints(" returned const", $Example.return_something_const())
prints(" returned ref", $Example.return_extended_ref())
prints("VarArg method calls")
var ref = ExampleRef.new()
prints("sending ref: ", ref.get_instance_id(), "returned ref: ", $Example.extended_ref_checks(ref).get_instance_id())
prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
prints("vararg_nv ret", $Example.varargs_func_nv("some", "arguments", "to", "test"))
prints(" sending ref: ", ref.get_instance_id(), "returned ref: ", $Example.extended_ref_checks(ref).get_instance_id())
prints(" vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
prints(" vararg_nv ret", $Example.varargs_func_nv("some", "arguments", "to", "test"))
$Example.varargs_func_void("some", "arguments", "to", "test")
prints("test array", $Example.test_array())
prints("test dictionary", $Example.test_dictionary())
prints("Method calls with default values")
prints(" defval (300)", $Example.def_args())
prints(" defval (250)", $Example.def_args(50))
prints(" defval (150)", $Example.def_args(50, 100))
# Use properties.
prints("custom position is", $Example.group_subgroup_custom_position)
prints("Array and Dictionary")
prints(" test array", $Example.test_array())
prints(" test dictionary", $Example.test_dictionary())
prints("Properties")
prints(" custom position is", $Example.group_subgroup_custom_position)
$Example.group_subgroup_custom_position = Vector2(50, 50)
prints("custom position now is", $Example.group_subgroup_custom_position)
prints(" custom position now is", $Example.group_subgroup_custom_position)
# Get constants
prints("FIRST", $Example.FIRST)
prints("ANSWER_TO_EVERYTHING", $Example.ANSWER_TO_EVERYTHING)
prints("CONSTANT_WITHOUT_ENUM", $Example.CONSTANT_WITHOUT_ENUM)
prints("Constnts")
prints(" FIRST", $Example.FIRST)
prints(" ANSWER_TO_EVERYTHING", $Example.ANSWER_TO_EVERYTHING)
prints(" CONSTANT_WITHOUT_ENUM", $Example.CONSTANT_WITHOUT_ENUM)
func _on_Example_custom_signal(name, value):
prints("Example emitted:", name, value)

View File

@@ -46,6 +46,18 @@ ExampleRef::~ExampleRef() {
UtilityFunctions::print("ExampleRef destroyed.");
}
int Example::test_static(int p_a, int p_b) {
return p_a + p_b;
}
void Example::test_static2() {
UtilityFunctions::print(" void static");
}
int Example::def_args(int p_a, int p_b) {
return p_a + p_b;
}
void Example::_bind_methods() {
// Methods.
ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
@@ -58,6 +70,11 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
{
MethodInfo mi;
mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
@@ -108,20 +125,20 @@ Example::~Example() {
// Methods.
void Example::simple_func() {
UtilityFunctions::print("Simple func called.");
UtilityFunctions::print(" Simple func called.");
}
void Example::simple_const_func() const {
UtilityFunctions::print("Simple const func called.");
UtilityFunctions::print(" Simple const func called.");
}
String Example::return_something(const String &base) {
UtilityFunctions::print("Return something called.");
UtilityFunctions::print(" Return something called.");
return base;
}
Viewport *Example::return_something_const() const {
UtilityFunctions::print("Return something const called.");
UtilityFunctions::print(" Return something const called.");
if (is_inside_tree()) {
Viewport *result = get_viewport();
return result;
@@ -138,22 +155,22 @@ Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
ref.instantiate();
// TODO the returned value gets dereferenced too early and return a null object otherwise.
ref->reference();
UtilityFunctions::print("Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
UtilityFunctions::print(" Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
return ref;
}
Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
UtilityFunctions::print("Varargs (Variant return) called with ", String::num((double)arg_count), " arguments");
UtilityFunctions::print(" Varargs (Variant return) called with ", String::num((double)arg_count), " arguments");
return arg_count;
}
int Example::varargs_func_nv(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
UtilityFunctions::print("Varargs (int return) called with ", String::num((double)arg_count), " arguments");
UtilityFunctions::print(" Varargs (int return) called with ", String::num((double)arg_count), " arguments");
return 42;
}
void Example::varargs_func_void(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
UtilityFunctions::print("Varargs (no return) called with ", String::num((double)arg_count), " arguments");
UtilityFunctions::print(" Varargs (no return) called with ", String::num((double)arg_count), " arguments");
}
void Example::emit_custom_signal(const String &name, int value) {

View File

@@ -90,6 +90,7 @@ public:
int varargs_func_nv(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
void varargs_func_void(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
void emit_custom_signal(const String &name, int value);
int def_args(int p_a = 100, int p_b = 200);
Array test_array() const;
Dictionary test_dictionary() const;
@@ -98,6 +99,10 @@ public:
void set_custom_position(const Vector2 &pos);
Vector2 get_custom_position() const;
// Static method.
static int test_static(int p_a, int p_b);
static void test_static2();
// Virtual function override (no need to bind manually).
virtual bool _has_point(const Vector2 &point) const override;
};