Skip to content

Commit

Permalink
Implement index operators for Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Sep 28, 2021
1 parent 5826fd5 commit 075c932
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,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":
Expand Down
11 changes: 11 additions & 0 deletions src/variant/packed_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <godot_cpp/godot.hpp>

#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/packed_byte_array.hpp>
#include <godot_cpp/variant/packed_color_array.hpp>
#include <godot_cpp/variant/packed_float32_array.hpp>
Expand Down Expand Up @@ -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::interface->array_operator_index_const((GDNativeTypePtr *)this, p_index);
return *var;
}

Variant &Array::operator[](int p_index) {
Variant *var = (Variant *)internal::interface->array_operator_index((GDNativeTypePtr *)this, p_index);
return *var;
}

} // namespace godot
1 change: 1 addition & 0 deletions test/demo/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func _ready():
prints("returned const", $Example.return_something_const())
prints("returned ref", $Example.return_extended_ref())
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)
Expand Down
19 changes: 19 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
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("test_array"), &Example::test_array);

{
MethodInfo mi;
Expand Down Expand Up @@ -80,6 +81,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.");
Expand Down Expand Up @@ -116,6 +125,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;
Expand Down
4 changes: 4 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Example : public Control {
CONSTANT_WITHOUT_ENUM = 314,
};

Example();
~Example();

// Functions.
void simple_func();
void simple_const_func() const;
Expand All @@ -78,6 +81,7 @@ class Example : public Control {
ExampleRef *return_extended_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);
Expand Down

0 comments on commit 075c932

Please sign in to comment.