wesnoth / jessene

A lua module for the Godot engine written in GDNative.
MIT License
14 stars 4 forks source link
godot lua wesnoth

Jessene

A lua module for the Godot engine written in GDNative.

NOTE: This repository only contains the source code for this module and does not include the lua source code or godot-cpp files. Download lua 5.3.5 from: here. Download the godot-cpp files from: here.

Compiling Instructions:

Lua

And now you are done. Attached in the repo is also a copy of a gdns and gdnlib file to import the lua module into godot. When importating the module, make sure that liblua.a for Linux/macOS or liblua.dll for Windows is in the same folder as the compiled LuaScript module.

Using in Godot

In order to use this module in Godot, attach the gdns script to a node (we will call this LuaNode).

Executing lua functions

Let's say you have a simple lua script under res://lua_scripts/utils.lua

function sum(...)
    result = 0
    local arg = {...}
    for i,v in ipairs(arg) do
       result = result + v
    end
    return result
end

To execute a lua function in Godot, you need to provide the name of the function as well as an Array of the arguments.

For instance, to execute the sum(...) function in Godot, do:

onready var Lua = $"LuaNode"

func foo():
    Lua.load("res://lua_scripts/utils.lua")
    var sum = Lua.execute("sum", [1, 2, 3, 4])

Functions will then return with an array of all variants returned, since lua can return multiple items

So in this example sum will now equal [10].

Pushing global variables

You can also push variables to godot that can be accessed globally from all future lua calls.

To push a variable from godot you just need to do:

var data = 5

Lua.pushVariant(data,"data")

Now in all future function calls, data can now be used in lua functions

The Godot to lua parser at this point can recognize most built in types. The following built in types are not supported yet and will be added at a later date: