Skip to content

Commit fd70a43

Browse files
authored
Merge pull request #130 from gilzoide/feature/typed-dictionary
Add support for defining typed dictionaries in Lua
2 parents c1c7f8c + da04e4a commit fd70a43

File tree

10 files changed

+362
-258
lines changed

10 files changed

+362
-258
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
## [Unreleased](https://github.com/gilzoide/lua-gdextension/compare/0.5.0...HEAD)
33
### Added
44
- Support for constructing typed arrays in Lua using the idiom `Array[some_type]()`
5-
- Support for typed arrays and classes in exported properties:
5+
- Support for constructing typed dictionaries in Lua using the idiom `Dictionary[key_type][value_type]()`
6+
- Support for typed arrays, typed dictionaries and classes in exported properties:
67
```lua
78
MyScript.exported_node_array = export(Array[Node])
9+
MyScript.exported_int_valued_dict = export(Dictionary[Variant][int])
810
MyScript.exported_texture_property = export(Texture)
911
-- or
1012
MyScript.exported_node_array = export({ type = Array[Node] })
13+
MyScript.exported_int_valued_dict = export({ type = Dictionary[Variant][int] })
1114
MyScript.exported_texture_property = export({ type = Texture })
1215
```
1316

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ This plugin is available in the Asset Library as:
2525
+ Create and manipulate Variant values
2626
+ Instantiate objects and access class constants
2727
+ Access singleton objects by name
28-
+ Utility functions, like `print_rich`, `lerp` and `is_same`
28+
+ Global utility functions, like Godot's `print_rich`, `lerp` and `is_same`.
29+
Also adds an `await` function that mimics GDScript's `await` keyword (only supported when running in a coroutine).
2930
+ Global enums, like `OK`, `TYPE_STRING` and `SIDE_LEFT`
3031
+ Patch Lua `package.searchers`, `require`, `loadfile` and `dofile` to accept paths relative to `res://` and `user://`
3132
- Editor plugin with Lua REPL for testing out Lua snippets
@@ -134,6 +135,18 @@ lua.do_string("""
134135
print(n:is_inside_tree()) -- false
135136
n:queue_free()
136137
```
138+
- Typed Arrays and Dictionaries are also supported
139+
```lua
140+
-- Element type: int
141+
local int_array = Array[int]()
142+
-- Element type: Node
143+
local node_array = Array[Node]()
144+
145+
-- Key: int, Value: bool
146+
local int_to_bool_dict = Dictionary[int][bool]()
147+
-- Key: Variant, Value: Node
148+
local node_valued_dict = Dictionary[Variant][Node]()
149+
```
137150
- Call Godot utility functions.
138151
```lua
139152
local d1 = Dictionary()

src/luaopen/variant.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "../utils/IndexedIterator.hpp"
3232
#include "../utils/ObjectIterator.hpp"
3333
#include "../utils/VariantType.hpp"
34-
#include "../utils/VariantTypedArray.hpp"
3534
#include "../utils/MethodBindByName.hpp"
3635
#include "../utils/convert_godot_lua.hpp"
3736
#include "../utils/convert_godot_std.hpp"
@@ -227,7 +226,6 @@ extern "C" int luaopen_godot_variant(lua_State *L) {
227226

228227
MethodBindByName::register_usertype(state);
229228
VariantType::register_usertype(state);
230-
VariantTypedArray::register_usertype(state);
231229

232230
state.set("typeof", &variant_get_type);
233231

src/script-language/LuaScriptProperty.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "../utils/Class.hpp"
2828
#include "../utils/VariantArguments.hpp"
2929
#include "../utils/VariantType.hpp"
30-
#include "../utils/VariantTypedArray.hpp"
3130
#include "../utils/convert_godot_lua.hpp"
3231

3332
#include <godot_cpp/classes/node.hpp>
@@ -53,14 +52,12 @@ static LuaScriptProperty lua_property(sol::this_state L, sol::stack_object value
5352
// index 1: either a Variant type or the default value
5453
if (auto type = table.get<sol::optional<VariantType>>(1)) {
5554
property.type = type->get_type();
55+
if (type->has_type_hints()) {
56+
property.hint = type->get_property_hint();
57+
property.hint_string = type->get_property_hint_string();
58+
}
5659
property.default_value = type->construct_default();
5760
}
58-
else if (auto typed_array = table.get<sol::optional<VariantTypedArray>>(1)) {
59-
property.type = Variant::Type::ARRAY;
60-
property.hint = PROPERTY_HINT_ARRAY_TYPE;
61-
property.hint_string = typed_array->get_hint_string();
62-
property.default_value = typed_array->construct_default();
63-
}
6461
else if (auto cls = table.get<sol::optional<Class>>(1)) {
6562
property.type = Variant::Type::OBJECT;
6663
property.class_name = cls->get_name();
@@ -80,16 +77,12 @@ static LuaScriptProperty lua_property(sol::this_state L, sol::stack_object value
8077
// PropertyInfo fields
8178
if (auto type = table.get<sol::optional<VariantType>>("type")) {
8279
property.type = type->get_type();
83-
if (property.default_value.get_type() == Variant::Type::NIL) {
84-
property.default_value = type->construct_default();
80+
if (type->has_type_hints()) {
81+
property.hint = type->get_property_hint();
82+
property.hint_string = type->get_property_hint_string();
8583
}
86-
}
87-
else if (auto typed_array = table.get<sol::optional<VariantTypedArray>>("type")) {
88-
property.type = Variant::Type::ARRAY;
89-
property.hint = PROPERTY_HINT_ARRAY_TYPE;
90-
property.hint_string = typed_array->get_hint_string();
91-
if (property.default_value.get_type() == Variant::Type::NIL) {
92-
property.default_value = typed_array->construct_default();
84+
if (property.default_value == Variant()) {
85+
property.default_value = type->construct_default();
9386
}
9487
}
9588
if (auto hint = table.get<sol::optional<uint32_t>>("hint")) {
@@ -125,10 +118,11 @@ static LuaScriptProperty lua_property(sol::this_state L, sol::stack_object value
125118
}
126119

127120
if (property.type == 0) {
128-
property.type = property.default_value.get_type();
129-
if (property.default_value.get_type() == Variant::Type::ARRAY && Array(property.default_value).is_typed()) {
130-
property.hint = PROPERTY_HINT_ARRAY_TYPE;
131-
property.hint_string = VariantTypedArray::of(property.default_value).get_hint_string();
121+
VariantType type = VariantType::from_variant(property.default_value);
122+
property.type = type.get_type();
123+
if (type.has_type_hints()) {
124+
property.hint = type.get_property_hint();
125+
property.hint_string = type.get_property_hint_string();
132126
}
133127
}
134128
property.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;

0 commit comments

Comments
 (0)