A small scripting engine written in C that embeds Lua 5.4. The C host creates a
Lua state, exposes its own functions to Lua through an Engine table, and can either
run a .lua script or start an interactive REPL.
This project demonstrates how a native application (for example a game engine) lets gameplay logic be written in a scripting language while core functionality stays in C.
- Embeds the Lua 5.4 interpreter through the C API
- Exposes C functions to Lua as
Engine.log,Engine.getTime,Engine.addandEngine.readFile - Runs a script file, or starts a REPL when no file is given
- Reports Lua runtime and syntax errors without crashing the host
- Includes an example text-based farming game written entirely in Lua
main.c
│ engine_init()
▼
engine.c ── luaL_newstate() + luaL_openlibs()
│ engine_register_bindings()
▼
bindings.c ── creates global table "Engine" with C functions
│
▼
scripts/*.lua ── call Engine.log(), Engine.add(), ... like normal Lua functions
Each binding follows the Lua C API convention: read arguments from the Lua stack, push results back onto the stack, and return the number of results.
static int binding_get_time(lua_State *L)
{
lua_pushinteger(L, (lua_Integer)time(NULL));
return 1; // one value returned to Lua
}| Function | Returns | Description |
|---|---|---|
Engine.log(message) |
nothing | Prints message with a [LOG] prefix |
Engine.getTime() |
integer | Current Unix timestamp |
Engine.add(a, b) |
number | Adds two numbers; integer + integer stays an integer |
Engine.readFile(path) |
string or nil |
Whole file contents, or nil if the file cannot be opened |
The full Lua standard library (print, math, string, io, ...) is also available.
- GCC
- Lua 5.4 development headers and library
make
On Ubuntu/Debian (or WSL on Windows):
sudo apt install build-essential liblua5.4-devgit clone https://github.com/Shevanka/lua-engine.git
cd lua-engine
makeRun a script:
./lua-engine scripts/hello.lua
# or
make run SCRIPT=scripts/hello.luaStart the interactive REPL (type exit to quit):
./lua-engine
lua> print(Engine.add(10, 32))
42
lua> exitRemove the binary:
make cleanCalls every binding once. Expected output:
[ENGINE] Bindings berhasil didaftarkan.
[ENGINE] Lua engine berhasil diinisialisasi.
[MAIN] Menjalankan script: scripts/hello.lua
[LOG] Halo dari Lua script!
[LOG] Unix timestamp sekarang: 1795525227
[LOG] Hasil 10 + 32 = 42
[LOG] Isi file berhasil dibaca.
Ini print bawaan Lua: 12.0
[ENGINE] Lua engine dimatikan.
A menu-driven mini farming game that shows Lua tables, functions and loops:
- view and move the player position
- view the inventory and check item amounts
- print the 5×5 world map and hit tiles (dirt / tree)
=== MINI FARM WORLD ===
1. Lihat posisi player
2. Jalan ke koordinat
3. Lihat inventory
4. Cek jumlah item
5. Lihat map
6. Pukul tile
7. Keluar
lua-engine/
├── src/
│ ├── main.c # Entry point: script mode or REPL
│ ├── engine.c/.h # Lua state lifecycle and script execution
│ └── bindings.c/.h # C functions exposed to Lua
├── scripts/
│ ├── hello.lua # Binding demo
│ └── farm_world.lua# Mini farming game
├── Makefile
└── LICENSE
-
Write a function with the signature
static int name(lua_State *L)insrc/bindings.c. -
Register it inside
engine_register_bindings():lua_pushcfunction(L, binding_name); lua_setfield(L, -2, "name"); // Engine.name
-
Rebuild with
makeand callEngine.name(...)from any script.