[solved] unpack() and arbitrary length arguments

User avatar
taikedz
Member
 
Posts: 587
Joined: Sun May 15, 2016 11:11
GitHub: taikedz
IRC: DuCake
In-game: DuCake

[solved] unpack() and arbitrary length arguments

by taikedz » Thu Sep 15, 2016 16:40

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?
Last edited by taikedz on Thu Sep 15, 2016 20:49, edited 1 time in total.
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: unpack() and arbitrary length arguments

by rubenwardy » Thu Sep 15, 2016 18:06

AFAIK, you don't need "..."
You can just do

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

To pass in the args
 

User avatar
taikedz
Member
 
Posts: 587
Joined: Sun May 15, 2016 11:11
GitHub: taikedz
IRC: DuCake
In-game: DuCake

Re: unpack() and arbitrary length arguments

by taikedz » Thu Sep 15, 2016 19:35

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?
 

User avatar
taikedz
Member
 
Posts: 587
Joined: Sun May 15, 2016 11:11
GitHub: taikedz
IRC: DuCake
In-game: DuCake

Re: unpack() and arbitrary length arguments

by taikedz » Thu Sep 15, 2016 20:49

That worked.

I don't understand.

So be it.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron