WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
348 stars 27 forks source link

Simple codegen for lua c library static inclusion #178

Closed Trey2k closed 9 months ago

Trey2k commented 9 months ago

This idea spun up due to https://github.com/WeaselGames/godot_luaAPI/issues/100#issuecomment-1760685532 This is a potential solution for both #100 and #144.

The steps to statically include a lua c library like lpeg, is to download its source code. Place the source in a folder under the lua_libraries folder with the exact library name, for lpeg this would be lpeg. All lower case an no version. The next time the addon is built it will be auto detected by codegen and available to bind_libraries.

Before merging I would like to hear some feedback from @GokuHiki

This has been tested with lpeg on linux using both modules and gdextension so far. I wont be suprised if something breaks along the way.

GokuHiki commented 9 months ago

I just test, the codegen is ok with this code.

extends Node

var lua: LuaAPI = LuaAPI.new()

func _lua_print(message) -> void:
    print(message)

func _ready():
    lua.push_variant("print2", _lua_print)
    lua.bind_libraries(["base", "table", "string", "utf8", "math", "lpeg"])

    var lua_codes: String
    lua_codes = """
    --print(lpeg)
    print2(lpeg)
    """

    var err: LuaError = lua.do_string(lua_codes)
    if err is LuaError:
        print("ERROR %d: %s" % [err.type, err.message])
        return

    var val = lua.pull_variant("lpeg")
    if val is LuaError:
        print("ERROR %d: %s" % [val.type, val.message])
        return

    var lpeg = val
    print(val.version)

Don't know if this relative of not but when I test with lua.push_variant("print", _lua_print) the lua function print won't print anything but it work when change print -> print2?

Thanks and regard!

Trey2k commented 9 months ago

Don't know if this relative of not but when I test with lua.push_variant("print", _lua_print) the lua function print won't print anything but it work when change print -> print2?

This is because the base library has a print method. our module will register its print method when base is bound. You should wait until after binding base to push you custom print method and it should work fine.