Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: monkey0506 on Sun 23/10/2005 13:43:40

Title: Local function with optional parameters
Post by: monkey0506 on Sun 23/10/2005 13:43:40
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):

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.
Title: Re: SUGGESTION (for the manual): On imports...
Post by: Pumaman on Sun 23/10/2005 20:58:23
That's a good point, I'll see about clarifying the manual.
Title: Re: SUGGESTION (for the manual): On imports...
Post by: monkey0506 on Tue 25/10/2005 22:53:18
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. :=