|
That would work -- however, there is a neater way, using the else keyword:
This also allows you to modify the variable inside the first block of code, without affecting whether the second block gets run or not. You can do as many tests as you like, using the else if keyword. So, a complete piece of code could look like this:
|
function dialog_request(int param) { // contents of function go here } |
You start with the keyword function, then follow it by the function name, and then parenthesis listing the parameter types and names. For each parameter that you want, you need to write its type (int or string), followed by the name it will be known by inside the function. This name can be anything you like - it is similar to naming a variable.
There are some fixed functions, such as dialog_request and on_event, which are part of AGS and therefore you MUST use the correct number and type of parameters. However, you may also add your own functions by naming them however you like, and having as many parameters as you need.
Functions are useful if you have a block of script code that you need to use in two different places - putting it in a function instead means you don't have to copy & paste, and that if you modify it, all other script that relies on it gets updated too.
To call your function from elsewhere in the script, just do it exactly like you call a built-in function - ie. just write its name, parameters then a semicolon.
I think a couple of examples are in order. First of all, let's show a fixed function, on_event:
function on_event (EventType event, int data) { if (event == eEventGotScore) { if (data == 5) { aSpecialScoreSound.Play(); } Display("You just got %d points!", data); } } |
With this script, whenever the player scores points, they will get a message telling them so. Also, if they happen to get 5 points at once, it will play audio clip aSpecialScoreSound.
As you can see, you use the function parameters just like any other script variables.
Our own function
Now, suppose we have a special animation of the player doing a dance, and we want to be able to play it from various points in the script. By far the easiest way to do this would be to put it in a function:
function do_dance() { cEgo.LockView(10); cEgo.Animate(2, 5); cEgo.UnlockView(); } |
This function runs view 10, loop 2, as the character's animation, waits until it finishes and then reverts to the default view.
TIP: if you're wondering where to place your custom functions, just open up the global script (Game menu, Edit Global Script) and write them in there. The function must be outside all other functions.
Now, elsewhere in your script, when you want the player to dance, just do:
do_dance(); |
Returning a value
You may have noticed that some of the built-in functions, such as IsGamePaused, return a value to the script. You can do this from your own functions, using the return keyword. So:
function add(int a, int b) { int result; result = a + b; return result; } |
This function adds the two numbers together and returns the result (a
useless function in practice since the + operator does the same thing, but it
demonstrates the point).
Another part of your script could then do:
total = add(5, 10); |
for example.
Using functions from room scripts
You may notice that when you add your own function to your global script, you can call it fine from other places in the global script but attempting to use it in a room script gives a parse error. The manual explains how to solve this using the script header.
We've covered some of the more advanced topics of scripting. I'm sure
there's a lot of stuff I've forgotten to mention, so feel free to comment
on it on the forums.
Page created 7 June 2002; updated 19 August 2007. Copyright (c) 2002-2007 Chris Jones.