Optional arguments

How do i make a function with optional arguments? I have this, but its giving the error

[lua]
function new(index,ply,ent[,pos[,ang[,text[,scale[,color]]]]])
[/lua]
error:



 ')' expected near '['


That is line 47 btw. thanks!

What are you trying to do?

I don’t really get what you are doing but normally arguments are stored in a table. Like so

[lua]
function example(args)
print(args[1])
print(args[2])
end
[/lua]

I want to be able to call that function with all of the nesecary arguments, and then any of the optional arguments. here is the rest of the function so you see what i mean.
[lua]
function new(index,ply,ent[,pos[,ang[,text[,scale[,color]]]]])
Plyind[ply][index] = {}
Plyind[ply][index][“Pos”] = pos or ent:GetPos()
Plyind[ply][index][“Ang”] = ang or ent:GetAngles()
Plyind[ply][index][“Txt”] = text or “Hello world”
Plyind[ply][index][“Scl”] = scale or 1
Plyind[ply][index][“Clr”] = color or Color(255,255,255,255)
umsg.Start(“wire_textholo_add”,player.GetAll())
umsg.Entity(ply)
umsg.Short(index)
umsg.End()
setpos(index,Plyind[ply][index][“Pos”],ply)
setang(index,Plyind[ply][index][“Ang”],ply)
setext(index,Plyind[ply][index][“Txt”],ply)
setscale(index,Plyind[ply][index][“Scl”],ply)
setcolor(index,Plyind[ply][index][“Clr”],ply)
end
[/lua]

You can either declare the function normally and call it by passing nil to the arguments you don’t want to use, like this:

[lua]function new(index, ply, ent, pos, ang, text, scale, color)
Plyind[ply][index] = {}
Plyind[ply][index][“Pos”] = pos or ent:GetPos()
Plyind[ply][index][“Ang”] = ang or ent:GetAngles()
Plyind[ply][index][“Txt”] = text or “Hello world”
Plyind[ply][index][“Scl”] = scale or 1
Plyind[ply][index][“Clr”] = color or Color(255,255,255,255)
umsg.Start(“wire_textholo_add”,player.GetAll())
umsg.Entity(ply)
umsg.Short(index)
umsg.End()
setpos(index,Plyind[ply][index][“Pos”],ply)
setang(index,Plyind[ply][index][“Ang”],ply)
setext(index,Plyind[ply][index][“Txt”],ply)
setscale(index,Plyind[ply][index][“Scl”],ply)
setcolor(index,Plyind[ply][index][“Clr”],ply)
end

new(1, Player(1), Entity(5), Vector(0, 0, 0), Angle(0, 0, 0), nil, nil, Color(0, 255, 255, 255))
[/lua]

or you can use “named arguments”, which is a table of arguments passed to the function, doing it this way means you don’t need to put nil when you don’t want to pass an argument:

[lua]function new(tab)
Plyind[tab.ply][tab.index] = {}
Plyind[tab.ply][tab.index][“Pos”] = tab.pos or ent:GetPos()
Plyind[tab.ply][tab.index][“Ang”] = tab.ang or ent:GetAngles()
Plyind[tab.ply][tab.index][“Txt”] = tab.text or “Hello world”
Plyind[tab.ply][tab.index][“Scl”] = tab.scale or 1
Plyind[tab.ply][tab.index][“Clr”] = tab.color or Color(255,255,255,255)
umsg.Start(“wire_textholo_add”,player.GetAll())
umsg.Entity(tab.ply)
umsg.Short(tab.index)
umsg.End()
setpos(tab.index,Plyind[tab.ply][tab.index][“Pos”],tab.ply)
setang(tab.index,Plyind[tab.ply][tab.index][“Ang”],tab.ply)
setext(tab.index,Plyind[tab.ply][tab.index][“Txt”],tab.ply)
setscale(tab.index,Plyind[tab.ply][tab.index][“Scl”],tab.ply)
setcolor(tab.index,Plyind[tab.ply][tab.index][“Clr”],tab.ply)
end

new{index = 1, ply = Player(1), ent = Entity(5), pos = Vector(0, 0, 0), ang = Angle(0, 0, 0), color = Color(0, 255, 255, 255)}
[/lua]
function{ } is syntactical sugar for function( { } )

None of the arguments are ‘required’, they merely just identify how the values passed to the function are named. Lua will not throw any errors if you don’t pass any arguments or if you pass too many.

If you want your function to work only if you do pass arguments, use the assert functions.

Oh lol. Fail. But wait, what if i wanted to pass some arguments, but not in the right order? would i just pass nil for the ones i dont want, like the person above you said?

yes, or use the table arguments.

Ok. Thanks :slight_smile: I didnt know you could not pass args and not error.