diff --git a/binding_generator.py b/binding_generator.py index 1abc3cc..89a58c9 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -444,6 +444,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl result.append(f"\tconst " + return_type + f" &operator[](int p_index) const;") result.append(f"\t" + return_type + f" &operator[](int p_index);") + if class_name == "Array": + result.append(f"\tconst Variant &operator[](int p_index) const;") + result.append(f"\tVariant &operator[](int p_index);") + result.append("};") if class_name == "String": diff --git a/src/variant/packed_arrays.cpp b/src/variant/packed_arrays.cpp index 10a7399..2081bfa 100644 --- a/src/variant/packed_arrays.cpp +++ b/src/variant/packed_arrays.cpp @@ -32,6 +32,7 @@ #include +#include #include #include #include @@ -124,4 +125,14 @@ Vector3 &PackedVector3Array::operator[](int p_index) { return *vec; } +const Variant &Array::operator[](int p_index) const { + const Variant *var = (const Variant *)internal::gdn_interface->array_operator_index_const((GDNativeTypePtr *)this, p_index); + return *var; +} + +Variant &Array::operator[](int p_index) { + Variant *var = (Variant *)internal::gdn_interface->array_operator_index((GDNativeTypePtr *)this, p_index); + return *var; +} + } // namespace godot diff --git a/test/demo/main.gd b/test/demo/main.gd index f519c09..a489c36 100644 --- a/test/demo/main.gd +++ b/test/demo/main.gd @@ -13,6 +13,7 @@ func _ready(): 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("test array", $Example.test_array()) # Use properties. prints("custom position is", $Example.group_subgroup_custom_position) diff --git a/test/src/example.cpp b/test/src/example.cpp index 4d34d4c..5eaa4bb 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -54,6 +54,7 @@ void Example::_bind_methods() { ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const); ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref); ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks); + ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array); { MethodInfo mi; @@ -81,6 +82,14 @@ void Example::_bind_methods() { BIND_CONSTANT(CONSTANT_WITHOUT_ENUM); } +Example::Example() { + UtilityFunctions::print("Constructor."); +} + +Example::~Example() { + UtilityFunctions::print("Destructor."); +} + // Methods. void Example::simple_func() { UtilityFunctions::print("Simple func called."); @@ -126,6 +135,16 @@ void Example::emit_custom_signal(const String &name, int value) { emit_signal("custom_signal", name, value); } +Array Example::test_array() const { + Array arr; + + arr.resize(2); + arr[0] = Variant(1); + arr[1] = Variant(2); + + return arr; +} + // Properties. void Example::set_custom_position(const Vector2 &pos) { custom_position = pos; diff --git a/test/src/example.h b/test/src/example.h index 1dc6a4e..79bcdd9 100644 --- a/test/src/example.h +++ b/test/src/example.h @@ -76,6 +76,9 @@ public: CONSTANT_WITHOUT_ENUM = 314, }; + Example(); + ~Example(); + // Functions. void simple_func(); void simple_const_func() const; @@ -85,6 +88,7 @@ public: Ref extended_ref_checks(Ref p_ref) const; Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error); void emit_custom_signal(const String &name, int value); + Array test_array() const; // Property. void set_custom_position(const Vector2 &pos);