Skip to content

Commit c2b6904

Browse files
committed
Implement index operators for Arrays
1 parent 271e336 commit c2b6904

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

binding_generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
444444
result.append(f"\tconst " + return_type + f" &operator[](int p_index) const;")
445445
result.append(f"\t" + return_type + f" &operator[](int p_index);")
446446

447+
if class_name == "Array":
448+
result.append(f"\tconst Variant &operator[](int p_index) const;")
449+
result.append(f"\tVariant &operator[](int p_index);")
450+
447451
result.append("};")
448452

449453
if class_name == "String":

src/variant/packed_arrays.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <godot_cpp/godot.hpp>
3434

35+
#include <godot_cpp/variant/array.hpp>
3536
#include <godot_cpp/variant/packed_byte_array.hpp>
3637
#include <godot_cpp/variant/packed_color_array.hpp>
3738
#include <godot_cpp/variant/packed_float32_array.hpp>
@@ -124,4 +125,14 @@ Vector3 &PackedVector3Array::operator[](int p_index) {
124125
return *vec;
125126
}
126127

128+
const Variant &Array::operator[](int p_index) const {
129+
const Variant *var = (const Variant *)internal::gdn_interface->array_operator_index_const((GDNativeTypePtr *)this, p_index);
130+
return *var;
131+
}
132+
133+
Variant &Array::operator[](int p_index) {
134+
Variant *var = (Variant *)internal::gdn_interface->array_operator_index((GDNativeTypePtr *)this, p_index);
135+
return *var;
136+
}
137+
127138
} // namespace godot

test/demo/main.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func _ready():
1313
var ref = ExampleRef.new()
1414
prints("sending ref: ", ref.get_instance_id(), "returned ref: ", $Example.extended_ref_checks(ref).get_instance_id())
1515
prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
16+
prints("test array", $Example.test_array())
1617

1718
# Use properties.
1819
prints("custom position is", $Example.group_subgroup_custom_position)

test/src/example.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void Example::_bind_methods() {
5454
ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
5555
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
5656
ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks);
57+
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
5758

5859
{
5960
MethodInfo mi;
@@ -81,6 +82,14 @@ void Example::_bind_methods() {
8182
BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
8283
}
8384

85+
Example::Example() {
86+
UtilityFunctions::print("Constructor.");
87+
}
88+
89+
Example::~Example() {
90+
UtilityFunctions::print("Destructor.");
91+
}
92+
8493
// Methods.
8594
void Example::simple_func() {
8695
UtilityFunctions::print("Simple func called.");
@@ -126,6 +135,16 @@ void Example::emit_custom_signal(const String &name, int value) {
126135
emit_signal("custom_signal", name, value);
127136
}
128137

138+
Array Example::test_array() const {
139+
Array arr;
140+
141+
arr.resize(2);
142+
arr[0] = Variant(1);
143+
arr[1] = Variant(2);
144+
145+
return arr;
146+
}
147+
129148
// Properties.
130149
void Example::set_custom_position(const Vector2 &pos) {
131150
custom_position = pos;

test/src/example.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class Example : public Control {
7676
CONSTANT_WITHOUT_ENUM = 314,
7777
};
7878

79+
Example();
80+
~Example();
81+
7982
// Functions.
8083
void simple_func();
8184
void simple_const_func() const;
@@ -85,6 +88,7 @@ class Example : public Control {
8588
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> p_ref) const;
8689
Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
8790
void emit_custom_signal(const String &name, int value);
91+
Array test_array() const;
8892

8993
// Property.
9094
void set_custom_position(const Vector2 &pos);

0 commit comments

Comments
 (0)