Implement typed dictionaries

This commit is contained in:
Thaddeus Crews
2023-07-03 12:51:54 -05:00
parent 730ccaae39
commit 2ffff669f5
9 changed files with 546 additions and 2 deletions

View File

@@ -81,10 +81,13 @@ func _ready():
# Array and Dictionary
assert_equal(example.test_array(), [1, 2])
assert_equal(example.test_tarray(), [ Vector2(1, 2), Vector2(2, 3) ])
assert_equal(example.test_dictionary(), {"hello": "world", "foo": "bar"})
assert_equal(example.test_tarray(), [Vector2(1, 2), Vector2(2, 3)])
var array: Array[int] = [1, 2, 3]
assert_equal(example.test_tarray_arg(array), 6)
assert_equal(example.test_dictionary(), { "hello": "world", "foo": "bar" })
assert_equal(example.test_tdictionary(), { Vector2(1, 2): Vector2i(2, 3) })
var dictionary: Dictionary[String, int] = { "1": 1, "2": 2, "3": 3 }
assert_equal(example.test_tdictionary_arg(dictionary), 6)
example.callable_bind()
assert_equal(custom_signal_emitted, ["bound", 11])

View File

@@ -199,6 +199,8 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
ClassDB::bind_method(D_METHOD("test_tdictionary_arg", "dictionary"), &Example::test_tdictionary_arg);
ClassDB::bind_method(D_METHOD("test_tdictionary"), &Example::test_tdictionary);
ClassDB::bind_method(D_METHOD("test_node_argument"), &Example::test_node_argument);
ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
@@ -551,6 +553,23 @@ Dictionary Example::test_dictionary() const {
return dict;
}
int Example::test_tdictionary_arg(const TypedDictionary<String, int64_t> &p_dictionary) {
int sum = 0;
TypedArray<int64_t> values = p_dictionary.values();
for (int i = 0; i < p_dictionary.size(); i++) {
sum += (int)values[i];
}
return sum;
}
TypedDictionary<Vector2, Vector2i> Example::test_tdictionary() const {
TypedDictionary<Vector2, Vector2i> dict;
dict[Vector2(1, 2)] = Vector2i(2, 3);
return dict;
}
Example *Example::test_node_argument(Example *p_node) const {
return p_node;
}

View File

@@ -129,6 +129,8 @@ public:
int test_tarray_arg(const TypedArray<int64_t> &p_array);
TypedArray<Vector2> test_tarray() const;
Dictionary test_dictionary() const;
int test_tdictionary_arg(const TypedDictionary<String, int64_t> &p_dictionary);
TypedDictionary<Vector2, Vector2i> test_tdictionary() const;
Example *test_node_argument(Example *p_node) const;
String test_string_ops() const;
String test_str_utility() const;