generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Optional Parameter?

Previous Thread :: Next Thread 
FPShooter319 is not online. FPShooter319
Joined: 28 May 2012
Total Posts: 153
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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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
FPShooter319 is not online. FPShooter319
Joined: 28 May 2012
Total Posts: 153
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 is not online. 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
FPShooter319 is not online. FPShooter319
Joined: 28 May 2012
Total Posts: 153
30 May 2012 08:45 PM
I guess, but I know it's possible without.

MrMcAero
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
30 May 2012 08:59 PM
lol


function derp(...)
print(...)
end

derp('hi')
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
30 May 2012 09:00 PM
Smurf wins

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
30 May 2012 09:01 PM
^.^
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
30 May 2012 09:36 PM
    function printMyParameters(...)
        print(table.concat({...},','));
    end
Report Abuse
KingBoo is not online. 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 is not online. 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
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
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
Trapper4O77 is not online. Trapper4O77
Joined: 03 Aug 2011
Total Posts: 5648
31 May 2012 10:46 AM
This thread went from a simple question to BLAH
Report Abuse
su8 is not online. 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
Trapper4O77 is not online. Trapper4O77
Joined: 03 Aug 2011
Total Posts: 5648
31 May 2012 10:57 AM
At least you admit it...
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
31 May 2012 11:14 AM
@people doing ...

... is as many arguments as they give, not just one optional one.
Report Abuse
Trapper4O77 is not online. Trapper4O77
Joined: 03 Aug 2011
Total Posts: 5648
31 May 2012 11:16 AM
^

dnq;ui
Report Abuse
su8 is not online. su8
Joined: 06 Mar 2009
Total Posts: 6334
31 May 2012 11:17 AM
Stop spamming srsly..
Report Abuse
Trapper4O77 is not online. Trapper4O77
Joined: 03 Aug 2011
Total Posts: 5648
31 May 2012 11:19 AM
http://www.roblox.com/Nou-item?id=79090142
Report Abuse
pwnedu46 is not online. 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 is not online. 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
FPShooter319 is not online. FPShooter319
Joined: 28 May 2012
Total Posts: 153
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image