|
| 30 May 2012 08:38 PM |
If I had this:
function printMyParameters(A,B) print(A..","..B) end
print(printMyParameters("Hi!","Bye!")
How could I do an optional parameter, so I don't HAVE to add B, like so?
function printMyParameters(A[,B]) print(A[..","..B]) end
print(printMyParameters("Hi!","Bye!")
MrMcAero |
|
|
| Report Abuse |
|
|
|
| 30 May 2012 08:43 PM |
You don't HAVE to supply any parameters.
function derp(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) print("hi") end
derp()
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
|
| 30 May 2012 08:44 PM |
So if I did this, it wouldn't error?
function derp(a,b) print(A..","..B) end
print(derp("hi"))
> Hi,
???
MrMcAero |
|
|
| Report Abuse |
|
|
dave2011
|
  |
| Joined: 02 Oct 2010 |
| Total Posts: 10581 |
|
|
| 30 May 2012 08:44 PM |
| @swim. I think he means that he wants it to print the parameters if they are there but not error if they are not, couldn't this be solved with an if statement? |
|
|
| Report Abuse |
|
|
|
| 30 May 2012 08:45 PM |
I guess, but I know it's possible without.
MrMcAero |
|
|
| Report Abuse |
|
|
|
| 30 May 2012 08:46 PM |
Erm, not exactly.
function derp(a,b) if not b then print(a) else print(a..','..b) end end
derp('hi')
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 30 May 2012 08:59 PM |
lol
function derp(...) print(...) end
derp('hi')
|
|
|
| Report Abuse |
|
|
|
| 30 May 2012 09:00 PM |
Smurf wins
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
| |
|
rayoma
|
  |
| Joined: 13 Nov 2009 |
| Total Posts: 1911 |
|
|
| 30 May 2012 09:36 PM |
function printMyParameters(...) print(table.concat({...},',')); end |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 30 May 2012 10:20 PM |
You can't make it optional, but you can have it default to something; such as..
function NameLarry(a,b) b = b or true -- Default = true return (a .. "and is that his real name..? " .. tostring(b) .. " I guess it was!") end
print(NameLarry("LOL! ")) --> LOL! and is that his real name..? true I guess it was!
print(NameLarry("LOL", false)) --> LOL! and is that his real name..? false I guess it was!
|
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 30 May 2012 10:21 PM |
But in the situation you explained, you could have b default to "".
Its really simple. |
|
|
| Report Abuse |
|
|
|
| 30 May 2012 10:26 PM |
I'd suggest using the table.concat function instead.
function concat_with_commas(...) print(table.concat({...}, ", ")) end
If you don't know what the '...' is for, read this: http://wiki.roblox.com/index.php/User:JulienDethurens/Essays/Variadic_functions. |
|
|
| Report Abuse |
|
|
|
| 31 May 2012 10:46 AM |
| This thread went from a simple question to BLAH |
|
|
| Report Abuse |
|
|
su8
|
  |
| Joined: 06 Mar 2009 |
| Total Posts: 6334 |
|
|
| 31 May 2012 10:53 AM |
@everybyddy suggesting the table.concat
He doesn't only want to print every parameter..
He just wants to figure out how to do optional parameters like
function direction(pointA,pointB,unitVector) unitVector = unitVector or false return (pointB-pointA).unit and unitVector or (pointB-pointA) end
pretty useless example but whatever |
|
|
| Report Abuse |
|
|
| |
|
|
| 31 May 2012 11:14 AM |
@people doing ...
... is as many arguments as they give, not just one optional one. |
|
|
| Report Abuse |
|
|
| |
|
su8
|
  |
| Joined: 06 Mar 2009 |
| Total Posts: 6334 |
|
| |
|
|
| 31 May 2012 11:19 AM |
| http://www.roblox.com/Nou-item?id=79090142 |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 31 May 2012 11:56 AM |
Yes, I am me. Thank you for noticing.
---------- ~pwnedu46, wiki writer~ |
|
|
| Report Abuse |
|
|
rayoma
|
  |
| Joined: 13 Nov 2009 |
| Total Posts: 1911 |
|
|
| 31 May 2012 12:45 PM |
Fine then king
function printMyParameters(A,B) print(A..(B and ","..B or "")); end |
|
|
| Report Abuse |
|
|
|
| 31 May 2012 02:53 PM |
@JulienDethurens,
Something like this?
function derp(...) print(table.concat(...,",")) end
print(derp(A,B)) >A,B
MrMcAero |
|
|
| Report Abuse |
|
|