Skip to content
Draft
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
8 changes: 8 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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]
24 changes: 24 additions & 0 deletions examples/arrays.lua
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions examples/export_to_editor.lua
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions examples/godot_objects.lua
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions examples/godot_utility_functions.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions examples/projects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WIP
1 change: 1 addition & 0 deletions examples/singleton_access.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert(OS == Engine:get_singleton("OS"))