[solved] unpack() and arbitrary length arguments
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)
When I run this, I get an error
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?
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?