- Home /
optional function parameters in javascript and c#
how can i declare function parameters as optional? something like:
function foo(required:String, optional:float = 1.0) {
...
}
$$anonymous$$ight be worth editing this question slightly so it's not Javascript specific, because the answer is applicable to both JS and C#
The comment above is not true anymore. As of C# 4.0, the optional parameters are supported (also in Unity since version 3.1, see @yoyo's answer below). They are still not available in UnityScript though (a.k.a. JS; tested today).
-----=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-
Question And Answers Outdated
since U3d version 4.0.
rest of page false or not complete answer.
Answer by duck · Dec 04, 2009 at 01:50 PM
Unity's Javascript doesn't support optional parameters directly (and neither does C#), but it does support function overloading, so the correct way to achieve this is to define the function twice, once with both parameters, and once with just one parameter. You can then optionally have the single-parameter implementation just pass the default value through to the two-parameter implementation, like this:
function foo(str : String) { // pass 1.0 as the default value for 'num' foo(str, 1.0); }
function foo(str : String, num : float) { Debug.Log(str + " : " + num); }
And it might be worth adding that this is equally applicable when working in C# too, where it would look very similar:
private void foo(string str) { // pass 1.0 as the default value for 'num' foo(str, 1.0f); }
private void foo(string str, float num) { Debug.Log(str + " : " + num); }
Answer by yoyo · Feb 04, 2011 at 09:21 PM
As of Unity 3.1, default arguments are supported in C#. See MSDN for documentation on how they work. Note that Mono Develop doesn't like them, so if you're using the debugger you need to select Tools > Preferences > Unity > Debugger and turn off "Build project in MonoDevelop".
I can't find any evidence of support in JavaScript, and it's not in the 3.1 release notes.
I meant for C# (which I've tested). Tweaked my answer to be more explicit.
Doesn't seem to work for me. I get errors for those function calls that don't have the 'optional' argument included in the call. Eg. foo("help"); errors with BCE0126: It is not possible to evaluate an expression of type 'void'. ? EDIT- this was because I had a yield statement within my function.
This helped awesomely! Didn't know about the "Build project in $$anonymous$$onoDevelop" thing, I always set the build for the project to C# 4.0, though that resets sometimes and really sucks. This seems permanent :D Thanks!
Answer by Bart Wttewaall · Mar 07, 2011 at 04:04 PM
Here's another way, in C# only as far as I know.
public void foo(required:String, params Object[] optionalArgs) {
Debug.Log(optionalArgs.Length);
}
required:String
is UnityScript ;) In C# we just do String required
Answer by elevishstar · Dec 04, 2009 at 01:25 PM
Here is a good explanation: http://www.tipstrs.com/tip/354/Using-optional-parameters-in-Javascript-functions. I hope it works in Unity as well!
This won't work in Unity. And in general, you shouldn't rely on non-Unity-specific Javascript documentation (i.e. browser-based Javascript) as a guide to Unity's Javascript functionality. Unity's Javascript is not real Javascript, it only looks like it! This is because it is built on top of $$anonymous$$ono, which is an open-source implementation of $$anonymous$$icrosoft's .Net platform. So, while it looks like Javascript, its functionality is defined by how $$anonymous$$ono operates.
Hmm, not sure I fully agree. For C#, I use $$anonymous$$SDN all the time for additional documentation that's not available from the Unity web site. Sure, you need to understand that not everything may apply, but it's still a useful resource.
C# is a language, .NET and $$anonymous$$ono are Frameworks. And $$anonymous$$ono is the platform independent (and open source) version of .NET, so year: C# is equal in both $$anonymous$$ono and .NET. But the .NET and $$anonymous$$ono may be different or implement certain features differently. $$anonymous$$aybe one of the $$anonymous$$ono classes is implemented wrong, so you may get different results from .NET.
This has nothing to do with C#.
UnityScript on the other side is a completely separate program$$anonymous$$g language which has many similarities to JavaScript, but isn't JavaScript. Duck is right, while you can use all of C# features. However, some of the latest stuff doesn't work in C# & $$anonymous$$ono (i.e. the 4.0 syntax functionality or the new async/await feature for threading), because $$anonymous$$ono lags behind the original .NET Framework.
Actually $$anonymous$$ono isn't lagged much behind .NET at all.
But Unity uses a custom build of $$anonymous$$ono that lags behind the open source released version by a good bit.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Using var as a placeholder for velocity... why? 2 Answers
how to typecast from GameObject 1 Answer
UnityScript, JavaScript, C# 1 Answer
Item Scripting Error 2 Answers