Skip to content

Commit 075c932

Browse files
committed
Implement index operators for Arrays
1 parent 5826fd5 commit 075c932

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
@@ -435,6 +435,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
435435
result.append(f"\tconst " + return_type + f" &operator[](int p_index) const;")
436436
result.append(f"\t" + return_type + f" &operator[](int p_index);")
437437

438+
if class_name == "Array":
439+
result.append(f"\tconst Variant &operator[](int p_index) const;")
440+
result.append(f"\tVariant &operator[](int p_index);")
441+
438442
result.append("};")
439443

440444
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::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::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
@@ -11,6 +11,7 @@ func _ready():
1111
prints("returned const", $Example.return_something_const())
1212
prints("returned ref", $Example.return_extended_ref())
1313
prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
14+
prints("test array", $Example.test_array())
1415

1516
# Use properties.
1617
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
@@ -53,6 +53,7 @@ void Example::_bind_methods() {
5353
ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
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);
56+
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
5657

5758
{
5859
MethodInfo mi;
@@ -80,6 +81,14 @@ void Example::_bind_methods() {
8081
BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
8182
}
8283

84+
Example::Example() {
85+
UtilityFunctions::print("Constructor.");
86+
}
87+
88+
Example::~Example() {
89+
UtilityFunctions::print("Destructor.");
90+
}
91+
8392
// Methods.
8493
void Example::simple_func() {
8594
UtilityFunctions::print("Simple func called.");
@@ -116,6 +125,16 @@ void Example::emit_custom_signal(const String &name, int value) {
116125
emit_signal("custom_signal", name, value);
117126
}
118127

128+
Array Example::test_array() const {
129+
Array arr;
130+
131+
arr.resize(2);
132+
arr[0] = Variant(1);
133+
arr[1] = Variant(2);
134+
135+
return arr;
136+
}
137+
119138
// Properties.
120139
void Example::set_custom_position(const Vector2 &pos) {
121140
custom_position = pos;

test/src/example.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class Example : public Control {
7070
CONSTANT_WITHOUT_ENUM = 314,
7171
};
7272

73+
Example();
74+
~Example();
75+
7376
// Functions.
7477
void simple_func();
7578
void simple_const_func() const;
@@ -78,6 +81,7 @@ class Example : public Control {
7881
ExampleRef *return_extended_ref() const;
7982
Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
8083
void emit_custom_signal(const String &name, int value);
84+
Array test_array() const;
8185

8286
// Property.
8387
void set_custom_position(const Vector2 &pos);

0 commit comments

Comments
 (0)