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
method TestMethod(param1)
{
Game.Msg("You called TestMethod with 1 parameter: "+param1);
}
method TestMethod(param1,param2)
{
Game.Msg("You called TestMethod with 2 parameters: "+param1+","+param2);
}
...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
method TestMethod(param1, param2 = null)
{
if(param2 == null)
Game.Msg("You called TestMethod with 1 parameter: "+param1);
else
Game.Msg("You called TestMethod with 2 parameters: "+param1+","+param2);
}
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!