Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: MrBehemoth on January 25, 2014, 11:52:04 AM

Title: optional parameters in a custom method
Post by: MrBehemoth on January 25, 2014, 11:52:04 AM
This might be a dumb question (or at least a question that shows I'm still learning as I go!) but how do you use optional parameters in a custom method definition?

For example, how would you get something like this to work:

Code: WME Script
  1. method TestMethod(param1)
  2. {
  3.   Game.Msg("You called TestMethod with 1 parameter: "+param1);
  4. }
  5.  
  6. method TestMethod(param1,param2)
  7. {
  8.   Game.Msg("You called TestMethod with 2 parameters: "+param1+","+param2);
  9. }

...Obviously that just makes a duplicate declaration. Is it even possible for custom methods?
Title: Re: optional parameters in a custom method
Post by: 2.0 on January 25, 2014, 12:09:05 PM
What about following solution?

Code: WME Script
  1. method TestMethod(param1, param2 = null)
  2. {
  3.   if (param2 == null)
  4.     Game.Msg("You called TestMethod with 1 parameter: "+param1);
  5.   else
  6.     Game.Msg("You called TestMethod with 2 parameters: "+param1+","+param2);
  7. }
Title: Re: optional parameters in a custom method
Post by: MrBehemoth on January 25, 2014, 12:15:19 PM
Quick reply 2.0, thanks.

Edit: I misunderstood in my earlier reply. I was thinking you would have to call TestMethod(something, null), but you can leave out the 2nd parameter when calling it. That's exactly what I wanted, and the answer was actually pretty simple. Thanks!