Skip to content

Commit adbc0bf

Browse files
authored
Merge pull request #656 from BastiaanOlij/dictionary_index
2 parents 5cacce7 + b008810 commit adbc0bf

File tree

7 files changed

+46
-9
lines changed

7 files changed

+46
-9
lines changed

binding_generator.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
448448
result.append(f"\tconst Variant &operator[](int p_index) const;")
449449
result.append(f"\tVariant &operator[](int p_index);")
450450

451+
if class_name == "Dictionary":
452+
result.append(f"\tconst Variant &operator[](const Variant &p_key) const;")
453+
result.append(f"\tVariant &operator[](const Variant &p_key);")
454+
451455
result.append("};")
452456

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

608612
# Move constructor.
609613
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
610-
result.append("\tstd::swap(opaque, other.opaque);")
614+
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
615+
result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);")
616+
else:
617+
result.append("\tstd::swap(opaque, other.opaque);")
611618
result.append("}")
612619
result.append("")
613620

@@ -722,7 +729,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
722729

723730
# Move assignment.
724731
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
725-
result.append("\tstd::swap(opaque, other.opaque);")
732+
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
733+
result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);")
734+
else:
735+
result.append("\tstd::swap(opaque, other.opaque);")
726736
result.append("\treturn *this;")
727737
result.append("}")
728738

@@ -1560,6 +1570,13 @@ def is_packed_array(type_name):
15601570
"PackedVector3Array",
15611571
]
15621572

1573+
def needs_copy_instead_of_move(type_name):
1574+
"""
1575+
Those are types which need initialised data or we'll get warning spam so need a copy instead of move.
1576+
"""
1577+
return type_name in [
1578+
"Dictionary",
1579+
]
15631580

15641581
def is_enum(type_name):
15651582
return type_name.startswith("enum::")

src/variant/packed_arrays.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <godot_cpp/godot.hpp>
3434

3535
#include <godot_cpp/variant/array.hpp>
36+
#include <godot_cpp/variant/dictionary.hpp>
3637
#include <godot_cpp/variant/packed_byte_array.hpp>
3738
#include <godot_cpp/variant/packed_color_array.hpp>
3839
#include <godot_cpp/variant/packed_float32_array.hpp>
@@ -135,4 +136,14 @@ Variant &Array::operator[](int p_index) {
135136
return *var;
136137
}
137138

139+
const Variant &Dictionary::operator[](const Variant &p_key) const {
140+
const Variant *var = (const Variant *)internal::gdn_interface->dictionary_operator_index_const((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key);
141+
return *var;
142+
}
143+
144+
Variant &Dictionary::operator[](const Variant &p_key) {
145+
Variant *var = (Variant *)internal::gdn_interface->dictionary_operator_index((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key);
146+
return *var;
147+
}
148+
138149
} // namespace godot

test/demo/main.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ 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+
1617
prints("test array", $Example.test_array())
18+
prints("test dictionary", $Example.test_dictionary())
1719

1820
# Use properties.
1921
prints("custom position is", $Example.group_subgroup_custom_position)

test/demo/main.tscn

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
script = ExtResource( "1_c326s" )
77

88
[node name="Example" type="Example" parent="."]
9-
script = null
109

1110
[node name="Label" type="Label" parent="Example"]
1211
offset_left = 194.0
1312
offset_top = -2.0
1413
offset_right = 234.0
1514
offset_bottom = 21.0
16-
structured_text_bidi_override_options = []
17-
script = null
1815
__meta__ = {
1916
"_edit_use_anchors_": false
2017
}
@@ -23,9 +20,6 @@ __meta__ = {
2320
offset_right = 79.0
2421
offset_bottom = 29.0
2522
text = "Click me!"
26-
script = null
2723
__meta__ = {
2824
"_edit_use_anchors_": false
2925
}
30-
31-
[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"]

test/src/example.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ 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+
5758
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
59+
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
5860

5961
{
6062
MethodInfo mi;
@@ -145,6 +147,15 @@ Array Example::test_array() const {
145147
return arr;
146148
}
147149

150+
Dictionary Example::test_dictionary() const {
151+
Dictionary dict;
152+
153+
dict["hello"] = "world";
154+
dict["foo"] = "bar";
155+
156+
return dict;
157+
}
158+
148159
// Properties.
149160
void Example::set_custom_position(const Vector2 &pos) {
150161
custom_position = pos;

test/src/example.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ class Example : public Control {
8888
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> p_ref) const;
8989
Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
9090
void emit_custom_signal(const String &name, int value);
91+
9192
Array test_array() const;
93+
Dictionary test_dictionary() const;
9294

9395
// Property.
9496
void set_custom_position(const Vector2 &pos);

0 commit comments

Comments
 (0)