varargs

Posted by Robin Lionheart on Fri 24 Nov 2006 10:12 AM — 4 posts, 20,562 views.

#0
I tried to make a varargs function

function debug_message(...)
	if GetVariable("Debug") then
		Note(unpack(arg))
	end
end


But the unpack() line merely threw an error about arg being nil. Perhaps I'm not following the Lua manual correctly. Am I missing something?
Russia #1
It's
Note(unpack(...))
Australia Forum Administrator #2
Not quite, Ked.

Under Lua 5.0 there was a dummy "arg" variable created by Lua for functions that had the "..." syntax. Now (in Lua 5.1) you just refer to "..." where you need the arguments. So your function should look like this:


function debug_message(...)
  if GetVariable("Debug") then
    Note(...)
  end
end

Russia #3
Ah, right. I didn't pay attention to how the list was actually being used.