Local function with optional parameters

Started by monkey0506, Sun 23/10/2005 13:43:40

Previous topic - Next topic

monkey0506

The manual currently states that an import is a reference to a function (or variable) that is external to the current script.  This isn't always the case, and could cause some confusion when the user wants to create an optional parameter to a local function.

I discovered while writing the ScrollingDialog Script Module, that you can in fact import a function to the script that it is declared in.  You can't call it between the import and the definition, so the reason for doing this might not be apparent, but if you want an optional parameter, it's essential to define the default value in the function declaration (import).  You can't do it in the definition.

So, if the user wants to create a local function with optional parameters, they could do the following (I'll use an example from the manual):

Code: ags
import void AddInvAndPlaySound(int inventoryItem, int sound = 5);

void AddInvAndPlaySound(int inventoryItem, int sound) {
  player.AddInventory(inventory[inventoryItem]);
  PlaySound(sound);
  }


That way the user can specify a different sound for different items, without having to make the function global to do so.

Yes, I suppose it's sort of a nit-pickish thing, but it would be nice to know about.

Pumaman

That's a good point, I'll see about clarifying the manual.

monkey0506

Well it certainly would have cleared up my confusion before when I was trying to create an optional parameter.  But like I said, I did get it figured out. :=

SMF spam blocked by CleanTalk