Page 1 of 1

Lua question : how to intercept function call

Posted: Fri Dec 05, 2008 9:40 am
by TsT
Hello

I'm trying to implement the missing love feature to draw on different layers.

I have almost finished but I need to improve my way of intercepting love function.

For now I have something like:

Code: Select all


        realfunc = love.graphics.rectangle
        love.graphics.rectangle = function(...)
                myfunc("something", arg)
        end

        myfunc = function(cmd, a)
                realfunc([i][b]a[1], a[2], a[3], a[4][/b][/i])
        end

Is anyone know how to call a function not with one table argument, but with all item of the table as arguments ?

I have try to use loadstring but it's not running when I have symbol (table, function, ...) in item of the table.

Best Regards,

Re: Lua question : how to intercept function call

Posted: Fri Dec 05, 2008 10:42 am
by hagish
not sure if i understand you correctly but unpack should do it:

Code: Select all

love.graphics.rectangle = function(...)
                myfunc("something", unpack(arg))
end

Re: Lua question : how to intercept function call

Posted: Fri Dec 05, 2008 11:38 am
by bartbes
I'm not sure, but I thought ... returns all the arguments

Code: Select all

function test(...)
print(...)
end
should call print with all arguments.

PS: posting on PS3

Re: Lua question : how to intercept function call

Posted: Fri Dec 05, 2008 1:13 pm
by TsT
Thanks for all, unpack is exactly what I need ^^

Re: Lua question : how to intercept function call

Posted: Sat Dec 06, 2008 4:29 am
by Lord Tim
bartbes wrote:I'm not sure, but I thought ... returns all the arguments

Code: Select all

function test(...)
print(...)
end
should call print with all arguments.

PS: posting on PS3
You just put the "..." in the parenthesis. to access them you use "arg" as a table.

Re: Lua question : how to intercept function call

Posted: Mon Dec 08, 2008 6:48 am
by bartbes
Got that part, but I've recently found a snippet using ... as argument.

However the lua manual lacks some things, and this is one of them. And what about the __index field? (unless they changed it)
I like the LÖVE documentation more (although there are hidden functions too).

Re: Lua question : how to intercept function call

Posted: Wed Dec 10, 2008 3:02 pm
by Kaze
Lord Tim wrote:
bartbes wrote:I'm not sure, but I thought ... returns all the arguments

Code: Select all

function test(...)
print(...)
end
should call print with all arguments.

PS: posting on PS3
You just put the "..." in the parenthesis. to access them you use "arg" as a table.
"..." is the same as "unpack(arg)"

Code: Select all

function dostuff(...)
    stuff1(...)
    stuff2(...)
end