spacer graphic
spacer graphic
Montage of games AGS Logo
spacer graphic

 

spacer graphic
Menu
spacer graphic Home
About
News
Features
Download AGS
spacer graphic Games 
Games main page
Award Winners
Picks of the month
Short games
Medium length games
Full length games
In Production
Hints & Tips
Community
Forums
AGSers World Map
Member websites
Chat
Resources
Tutorials
FAQ
Knowledge Base
Downloads
Links
AGS-related links
* AGS Manual
  * Scripting
    * Script language keywords

function

function name ( [type1 param1, type2 param2, ... ] )

Declares a custom function in your script. A function is a way in which you can separate out commonly used code into its own place, and thus avoid duplicating code.

For example, suppose that you quite often want to play a sound and add an inventory item at the same time. You could write both commands each time, or you could define a custom function:

function AddInvAndPlaySound(int inventoryItem) {
  player.AddInventory(inventory[inventoryItem]);
  PlaySound(5);
}

then, elsewhere in your code you can simply call:

AddInvAndPlaySound(8);
to add inventory item 8 and play the sound.

Generally, you place your functions in your global script. You then need to add an import line to your script header to allow the function to be called from room scripts.

Optional parameters

You can make int parameters optional if there is a default value that the user doesn't need to supply. To do this, change the script header import declaration like this:

import function TestFunction(int stuff, int things = 5);
that declares a function with a mandatory stuff parameter, and an optional things parameter. If the caller does not supply the second parameter, it will default to 5.

NOTE: To use optional parameters, you need to have an "import" declaration for the function in the script header. The default values cannot be specified in the actual function declaration itself.


User comments and notes
There are currently no user comments on this page.
The user comment facility is currently disabled.