Jump to content




Check arguments passed?


  • You cannot reply to this topic
4 replies to this topic

#1 LDShadowLord

  • Members
  • 115 posts

Posted 15 December 2013 - 01:35 PM

Does anyone know if there is a way I can see how many arguments are being passed into a variable? Without needing to individually check each and every single argument?

I ask because currently I use that system of checking each and every variable...
function cSend(localf,server)
	if localf = nil then
		print("Incorrect arguments: localname")
		return
	elseif server = nil then
		print("Incorrect argument: servername")
		return
	end
...

But I would prefer to use the system of checking how many arguments it receives (as you do with tArgs=...)
if #tArgs < 1 or #tArgs > 4 then

Any information would be appreciated, thanks.

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 15 December 2013 - 02:09 PM

You can use varargs (...) system here:

local function test (...)
  --// When dealing with varargs (those three dots) Lua automatically creates a local table called 'args'
  --// It is the same as doing this:
  local args = {...}
  args.n = #args

  print(args.n) --> Prints how many arguments there are

  for i, v in ipairs(args) do --// Prints all the given arguments
    print(v)
  end
end


#3 LDShadowLord

  • Members
  • 115 posts

Posted 15 December 2013 - 02:40 PM

Thanks :)

#4 jay5476

  • Members
  • 289 posts

Posted 16 December 2013 - 05:53 AM

they way lua works you can do
if not value then
 stuff
end
or even this
value = value or DEFAULT_VALUE


#5 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 16 December 2013 - 12:36 PM

The print value uses this iirc.
print = function ( ... )
local args = {...}
for i=1, #args do
printstuffandloadspace(args[i])
end
linereturncode
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users