Page 1 of 1

[solved] unpack() and arbitrary length arguments

PostPosted: Thu Sep 15, 2016 16:40
by taikedz
Hello

I am trying to write a generalized way to override functions on Lua entities. I have boiled down the following code, on a dmobs:panda which uses mobs_redo (this should not be relevant, but mentioning it, just in case it is)

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- get necessary elements
local themob = minetest.registered_entities["dmobs:panda"]

local thefunc = function(self,clicker)
        minetest.chat_send_player(clicker:get_player_name(),"Right clicked a panda!")
        return true
end

-- assign arbitrary function after saving the original

local orig = themob.on_rightclick

themob.on_rightclick = function(...)
        if thefunc(unpack(...)) then orig(unpack(...)) end -- line 27
end



When I run this, I get an error

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
2016-09-15 17:31:21: ACTION[Server]: singleplayer right-clicks object 42: LuaEntitySAO at (-3.62019,4,100.595)
2016-09-15 17:31:21: ERROR[Main]: ServerError: Lua: Runtime error from mod 'dmobs' in callback luaentity_Rightclick(): /home/tai/.minetest/mods/mobs_override/test.lua:27: bad argument #2 to 'unpack' (number expected, got userdata)
2016-09-15 17:31:21: ERROR[Main]: stack traceback:
2016-09-15 17:31:21: ERROR[Main]:       [C]: in function 'unpack'
2016-09-15 17:31:21: ERROR[Main]:       /home/tai/.minetest/mods/mobs_override/test.lua:27: in function </home/tai/.minetest/mods/mobs_override/test.lua:26>



Note: "arg" has been deprecated in favour of "..."

Note 2 : if I check they type of "..." it is "table"

Therefore, unpack function itself should be receiving a single table (and should be outputting self,clicker)

Can anyone guide here?

Re: unpack() and arbitrary length arguments

PostPosted: Thu Sep 15, 2016 18:06
by rubenwardy
AFAIK, you don't need "..."
You can just do

function(...) other(...) end

To pass in the args

Re: unpack() and arbitrary length arguments

PostPosted: Thu Sep 15, 2016 19:35
by taikedz
Thanks. Will try that asap when i am home.

Though if "..." is a table in type() then aurely it would be passed as one arg - a table - not a set of params?

Re: unpack() and arbitrary length arguments

PostPosted: Thu Sep 15, 2016 20:49
by taikedz
That worked.

I don't understand.

So be it.