Skip to content

Commit 2e75eb7

Browse files
authored
Added Lua global variable example (#144)
* lua global variable example * add check * cleaned up comment * fixed capitalization * fix package name * removed unnecessary parenthesis
1 parent a7488c2 commit 2e75eb7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

.github/workflows/check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ jobs:
6262
odin check thread/sync_mutex $FLAGS
6363
odin check thread/pool/basic $FLAGS
6464
65+
odin check lua/global_variables $FLAGS
66+
6567
odin check math/noise/draw_texture $FLAGS
6668
odin check math/rand/markov $FLAGS
6769
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package global_variables
2+
3+
import lua "vendor:lua/5.4"
4+
import "core:fmt"
5+
6+
// The code the Lua VM will run
7+
CODE :: "print(Answer)"
8+
9+
main :: proc() {
10+
// Create new Lua state
11+
state := lua.L_newstate()
12+
13+
// Open the base libraries (print, etc...)
14+
lua.open_base(state)
15+
16+
// Set a new global integer (a Lua 5.3+ feature!) called "Answer" to 42
17+
// First push the integer to the stack, essentially a "postboard" to talk with the Lua VM
18+
lua.pushinteger(state, 42)
19+
// Pops the top value on the stack and creates a global with it's value
20+
lua.setglobal(state, "Answer")
21+
22+
// Run code and check if it succeeded
23+
if lua.L_dostring(state, CODE) != 0 {
24+
// Get the error string from the top of the stack and print it
25+
error := lua.tostring(state, -1)
26+
fmt.println(error)
27+
// Pop the error off of the stack
28+
lua.pop(state, 1)
29+
}
30+
31+
// Closes the Lua VM, deallocating all memory
32+
lua.close(state)
33+
}

0 commit comments

Comments
 (0)