WeaselGames / godot_luaAPI

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

Feature Request: __index includes args for method calls #199

Closed jbromberg closed 4 months ago

jbromberg commented 4 months ago

Is your feature request related to a problem? Please describe. I'm trying to ensure a method call on my GDScript class is always called with a LuaTuple argument. See the code snippet below.

class LuaClass:
    func __index(ref, index):
        if index == "foo":
            return foo

    func foo(args: LuaTuple):
        print(args.size())

var lua := LuaAPI.new()
var lua_class := LuaClass.new()
lua.push_variant("lua_class", lua_class)
lua.do_string("
lua_class.foo()
lua_class.foo(1)
lua_class.foo(1, 2)
")

I'd like for it to be a LuaTuple so I can do custom overloading based on the args and because in some cases I also register these functions as globals using LuaCallableExtra.with_tuple.

Describe the solution you'd like Either the __index func could get called with an args parameter so I can bind it to the callable or some parameter on LuaAPI or the push_variant method that allows me to have methods called with a LuaTuple.

jbromberg commented 4 months ago

I think this is the line where arguments get passed into the function call: https://github.com/WeaselGames/godot_luaAPI/blob/edf4afe7665a03978e8a4a3a15b727a66c4dc03d/src/luaState.cpp#L179

jbromberg commented 4 months ago

Object metatable: https://github.com/WeaselGames/godot_luaAPI/blob/edf4afe7665a03978e8a4a3a15b727a66c4dc03d/src/metatables.cpp#L411

Trey2k commented 4 months ago

Currently the best way to use tuples in class methods is to have a member variable of the type LuaCallableExtra. You can define the var and use a lambda method for the method definition. Another option is to wrap the method in a LuaCallableExtra in __index.

I am open to cleaner solutions though if you find something that could be done.

jbromberg commented 4 months ago

I did this for now. It's working, thanks!

func __index(ref: LuaAPI, index: String):
    if has_method(index):
        return LuaCallableExtra.with_tuple(Callable(self, index), 1)
    return get(index)