Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append(f"\tconst Variant &operator[](int p_index) const;")
result.append(f"\tVariant &operator[](int p_index);")

if class_name == "Dictionary":
result.append(f"\tconst Variant &operator[](const Variant &p_key) const;")
result.append(f"\tVariant &operator[](const Variant &p_key);")

result.append("};")

if class_name == "String":
Expand Down Expand Up @@ -607,7 +611,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl

# Move constructor.
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
result.append("\tstd::swap(opaque, other.opaque);")
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);")
else:
result.append("\tstd::swap(opaque, other.opaque);")
result.append("}")
result.append("")

Expand Down Expand Up @@ -722,7 +729,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl

# Move assignment.
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
result.append("\tstd::swap(opaque, other.opaque);")
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);")
else:
result.append("\tstd::swap(opaque, other.opaque);")
result.append("\treturn *this;")
result.append("}")

Expand Down Expand Up @@ -1560,6 +1570,13 @@ def is_packed_array(type_name):
"PackedVector3Array",
]

def needs_copy_instead_of_move(type_name):
"""
Those are types which need initialised data or we'll get warning spam so need a copy instead of move.
"""
return type_name in [
"Dictionary",
]

def is_enum(type_name):
return type_name.startswith("enum::")
Expand Down
2 changes: 1 addition & 1 deletion godot-headers
11 changes: 11 additions & 0 deletions src/variant/packed_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <godot_cpp/godot.hpp>

#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/dictionary.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 @@ -135,4 +136,14 @@ Variant &Array::operator[](int p_index) {
return *var;
}

const Variant &Dictionary::operator[](const Variant &p_key) const {
const Variant *var = (const Variant *)internal::gdn_interface->dictionary_operator_index_const((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key);
return *var;
}

Variant &Dictionary::operator[](const Variant &p_key) {
Variant *var = (Variant *)internal::gdn_interface->dictionary_operator_index((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key);
return *var;
}

} // namespace godot
2 changes: 2 additions & 0 deletions test/demo/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ 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())
prints("test dictionary", $Example.test_dictionary())

# Use properties.
prints("custom position is", $Example.group_subgroup_custom_position)
Expand Down
6 changes: 0 additions & 6 deletions test/demo/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
script = ExtResource( "1_c326s" )

[node name="Example" type="Example" parent="."]
script = null

[node name="Label" type="Label" parent="Example"]
offset_left = 194.0
offset_top = -2.0
offset_right = 234.0
offset_bottom = 21.0
structured_text_bidi_override_options = []
script = null
__meta__ = {
"_edit_use_anchors_": false
}
Expand All @@ -23,9 +20,6 @@ __meta__ = {
offset_right = 79.0
offset_bottom = 29.0
text = "Click me!"
script = null
__meta__ = {
"_edit_use_anchors_": false
}

[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"]
11 changes: 11 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ 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);
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);

{
MethodInfo mi;
Expand Down Expand Up @@ -145,6 +147,15 @@ Array Example::test_array() const {
return arr;
}

Dictionary Example::test_dictionary() const {
Dictionary dict;

dict["hello"] = "world";
dict["foo"] = "bar";

return dict;
}

// Properties.
void Example::set_custom_position(const Vector2 &pos) {
custom_position = pos;
Expand Down
2 changes: 2 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class Example : public Control {
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> 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;
Dictionary test_dictionary() const;

// Property.
void set_custom_position(const Vector2 &pos);
Expand Down