Scripting, Code & Interaction: Difference between revisions
→Creating your own custom functions
Line 179: | Line 179: | ||
==Creating your own custom functions== | ==Creating your own custom functions== | ||
''How do I create my own custom function to use in my game's scripting? I keep getting errors. What am I doing wrong?'' | |||
First, decide on a function name. Perhaps you want a function to display a message that says "The number is", followed by a number that ''you'' feed the function. Remember that a function can either: | |||
# do some action, but return nothing, or | |||
# do an action, and return a value. | |||
If the function will return a value, it ''HAS'' to be an integer value (note: from AGS V2.7 onwards, you may return stuff other than integers, however, this won't be covered here as they're no long ''beginner'' information) . Also, even if you decide not to return anythng, keep in mind that sometimes it is a good idea to return ''something''. For example, the '''DisplaySpeechBackground()''' ('''cEgo.SayBackground()''' for AGS V2.7 and above) function returns a value, even though it is not needed. But it '''is''' useful. (It returns an overlay ID, in case you wanted to know, and for AGS V2.7+, it actually returns an '''Overlay''' ''pointer''.) | |||
Next, in the game's global script, add the function by itself like so: | |||
function FunctionName(int parametername) { | |||
} | |||
Keep in mind that you can have more than one parameter fed to your function, including strings, ints, chars, and any other data type supported by the AGS script system. Next, add the actions that will run when the function is called. | |||
function DisplayNumber(int number) { | |||
Display("The number is %d", number); | |||
} | |||
In the above example, when called, '''DisplayNumber()''' will display "The number is", followed by the number sent to the function, which is called, interestingly enough, '''number'''. You can call parameter variable names anything you want. They are declared by the function inside the parentheses ( '''()''' ). Next, you have to import your function into the '''script header'''. Make sure that you do this correctly. There are two ways to do this: | |||
import DisplayNumber (int); | |||
OR | |||
import DisplayNumber (int number); | |||
Either way will work just fine (but the second way is more handy since you don't need to guess what the parameters stand for using the auto-complete feature of the script editor). Now, in any room's script, or even in the global script '''below''' the newly declared function, you can call up the function like so: | |||
... | |||
DisplayNumber(57); | |||
... | |||
Finally, if you want to return a value, use the '''return''' command at the very end of the declared function, like so: | |||
function Sum(int numberone, int numbertwo) { | |||
return numberone + numbertwo; | |||
} | |||
The function will now return the value obtained when '''numberone''' and '''numbertwo''' are added together. Using the function... | |||
int x = Sum(24,61); | |||
...will set '''x''''s value to 85 (the sum of 24 and 61, as calculated in the function). That's all there is to it! | |||
==Defining custom hotkeys and shortcuts== | ==Defining custom hotkeys and shortcuts== | ||
==Having a character continuously animated in the background== | ==Having a character continuously animated in the background== |