Instead of having to define every parameter every time, is there a way to select a default?
I know in some languages it's possible to do something like this:
function myNewFunc(defaultVar = true) --do code end
So that if defaultVar
doesn't get a value passed in it'll be true.
I'd like a simpler way than this:
function myNewFunc(defaultVar) if defaultVar == nil then defaultVar = true; end --do code end
Any insights?
There is no shortcut built into Lua to do such a thing.
However, instead of using an if statement you could use a conditional expression:
function myFunction(a, b, c, d) a = a or 7 b = b or "String" c = c or Vector3.new() d = d or true -- code end
No, there is no simpler way to do it, unless you create another function that you just run to set the value. E.G;
function setDefault(val) if val == nil then return true else return val end end function funcName(defaultParam) defaultParam = setDefault(defaultParam) end
This doesn't really count as simpler, though.
Hope I helped :)
~TDP