Please login or register.

Login with username, password and session length
Advanced search  

News:

Forum rules - please read before posting, it can save you a lot of time.

Author Topic: optional parameters in a custom method  (Read 1426 times)

0 Members and 1 Guest are viewing this topic.

MrBehemoth

  • Lurker
  • *
  • Karma: 2
  • Offline Offline
  • Gender: Male
  • Posts: 18
    • View Profile
optional parameters in a custom method
« 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?

2.0

  • Regular poster
  • ***
  • Karma: 4
  • Offline Offline
  • Posts: 217
    • View Profile
Re: optional parameters in a custom method
« Reply #1 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. }
Logged

MrBehemoth

  • Lurker
  • *
  • Karma: 2
  • Offline Offline
  • Gender: Male
  • Posts: 18
    • View Profile
Re: optional parameters in a custom method
« Reply #2 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!
« Last Edit: January 25, 2014, 01:38:22 PM by MrBehemoth »
Logged
 

Page created in 0.182 seconds with 25 queries.