diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..33b46ef4 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,8 @@ +# Code examples +*Note: the examples may contain code that does not work for the latest release build.* + +- (Godot objects)[./godot_objects.lua] +- (Godot utility functions)[./godot_utility_functions.lua] +- (Arrays)[./arrays.lua] +- (Export to editor)[./export_to_editor.lua] - How to replace GDScript's `@export`. More property hints are listed (here)[https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyhint]. +- (Access singletons)[./singleton_access.lua] diff --git a/examples/arrays.lua b/examples/arrays.lua new file mode 100644 index 00000000..2c02e665 --- /dev/null +++ b/examples/arrays.lua @@ -0,0 +1,24 @@ +-- Typed arrays +-- Element type: int +local int_array = Array[int]() +-- Element type: Node +local node_array = Array[Node]() + +-- Construct from table +local array = Array{ "value 0", "value 1" } +-- Godot Arrays are indexed from 0, instead of 1 +print(array[0]) -- "value 0" +print(array[1]) -- "value 1" +print(array[2]) -- nil + +-- Length +print(array:size()) -- 2 +print(#array) -- 2 + +-- Runtime Variant type checking (also works for other variant types) +print(Variant.is(array, Array)) -- true +print(Variant.is(array, "Array")) -- true +-- Method syntax makes this a bit shorter +print(array:is(Array)) -- true +print(array:is(Dictionary)) -- false +print(array:is(RefCounted)) -- false diff --git a/examples/export_to_editor.lua b/examples/export_to_editor.lua new file mode 100644 index 00000000..af61c5a6 --- /dev/null +++ b/examples/export_to_editor.lua @@ -0,0 +1,25 @@ +local MyScript = { extends = Node } + +MyScript.num = export(int) +MyScript.node = export(Node2D) + +-- You can also provide a default value instead +MyScript.boolean = export(true) + +-- Or for full control +MyScript.angle = export({ + type = float, + default = 0, + -- Restrict value to range + hint = PROPERTY_HINT_RANGE, + hint_string = "0,360,degrees", +}) + +MyScript.array = export(Array[Resource]) + +MyScript.dict = export({ + type = Dictionary[String][Node], + default = Dictionary[String][Node](), +}) + +return MyScript diff --git a/examples/godot_objects.lua b/examples/godot_objects.lua new file mode 100644 index 00000000..83f943db --- /dev/null +++ b/examples/godot_objects.lua @@ -0,0 +1,10 @@ +-- Instantiate Godot objects +local v = Vector3(1, 2, 3) +print(v.x) -- 1 + +-- Note: use ":" instead of "." to call methods in Lua +print(v:length()) -- 3.74165749549866 + +local n = Node:new() +print(n:is_inside_tree()) -- false +n:queue_free() diff --git a/examples/godot_utility_functions.lua b/examples/godot_utility_functions.lua new file mode 100644 index 00000000..8c24f9ae --- /dev/null +++ b/examples/godot_utility_functions.lua @@ -0,0 +1,6 @@ +local d1 = Dictionary() +local d2 = Dictionary() +print(is_same(d1, d2)) -- false +print(is_same(d2, d2)) -- true + +print(lerp(5, 10, 0.15)) -- 5.75 diff --git a/examples/projects/README.md b/examples/projects/README.md new file mode 100644 index 00000000..00d7bdd4 --- /dev/null +++ b/examples/projects/README.md @@ -0,0 +1 @@ +WIP diff --git a/examples/singleton_access.lua b/examples/singleton_access.lua new file mode 100644 index 00000000..713b116a --- /dev/null +++ b/examples/singleton_access.lua @@ -0,0 +1 @@ +assert(OS == Engine:get_singleton("OS"))