WeaselGames / godot_luaAPI

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

Add LuaObjectMetatable type #149

Closed Trey2k closed 1 year ago

Trey2k commented 1 year ago

This PR adds some new types for better metatable control over objects.

Added:

Removed:

Code Example:

extends Node2D

class TestMetatable:
    extends LuaObjectMetatable

    func __index(obj: Object, lua: LuaAPI, index: String) -> Variant:
        print("__index called")
        return obj.get(index)

class example:
    func lua_fields():
        return ['test']
    func test():
        print("test")

class example2:
    var lua_metatable := TestMetatable.new()
    func test():
        print("test2")

func _ready() -> void:
    var luaAPI = LuaAPI.new()
    luaAPI.object_metatable.permissive = false
    var ex1 = example.new()
    var ex2 = example2.new()
    luaAPI.push_variant("example1", ex1)
    luaAPI.push_variant("example2", ex2)
    var err = luaAPI.do_string("""
    example1.test()
    example2.test()
    """)
    if err is LuaError:
        print(err.message)