|
how can i declare function parameters as optional? something like:
(comments are locked)
|
|
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:
And it might be worth adding that this is equally applicable when working in C# too, where it would look very similar: duck, you rock! thanks!
Dec 04 '09 at 03:19 PM
flexrails
(comments are locked)
|
|
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.
Feb 13 '11 at 12:45 AM
Waz
I meant for C# (which I've tested). Tweaked my answer to be more explicit.
Feb 14 '11 at 08:31 PM
yoyo
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.
Oct 05 '11 at 01:39 PM
grimmy
This helped awesomely! Didn't know about the "Build project in MonoDevelop" 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!
Jan 15 at 04:37 PM
The Oddler
(comments are locked)
|
|
Here's another way, in C# only as far as I know.
Oct 11 '11 at 01:40 PM
Tseng
(comments are locked)
|
|
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 Mono, which is an open-source implementation of Microsoft's .Net platform. So, while it looks like Javascript, its functionality is defined by how Mono operates.
Dec 04 '09 at 02:09 PM
duck ♦♦
Hmm, not sure I fully agree. For C#, I use MSDN 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.
Feb 04 '11 at 09:22 PM
yoyo
C# is a language, .NET and Mono are Frameworks. And Mono is the platform independent (and open source) version of .NET, so year: C# is equal in both Mono and .NET. But the .NET and Mono may be different or implement certain features differently. Maybe one of the Mono 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 programming 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# & Mono (i.e. the 4.0 syntax functionality or the new async/await feature for threading), because Mono lags behind the original .NET Framework.
Oct 11 '11 at 01:39 PM
Tseng
(comments are locked)
|

Might be worth editing this question slightly so it's not Javascript specific, because the answer is applicable to both JS and C#