-
Notifications
You must be signed in to change notification settings - Fork 2
Scripts
Scripts are the bread and butter of TaffyScript. A script is just another name for a function or method.
To define a script, start with script then the script name. Then inside of a block, write the code! Example:
script scr_example {
show_debug_message("I'm in a script!");
}I tend to follow a Gamemaker like convention for naming scripts by prefixing them with scr_ in order to avoid naming conflicts, but it isn't necessary to do so.
If you want your script to have arguments, you can add them to the script signature like so:
script scr_example(arg1, arg2) {
show_debug_message(arg1);
}Script arguments can aslo have a default argument. Any number of parameters can have a default, but they must come at the END of the script.
script scr_example(arg1, arg2 = "moo") {
show_debug_message(arg2);
}While the above methods of accessing arguments is nice, sometimes it is not sufficent. You can access the argument array directly, either by writing argument followed by a number, or by using traditional array syntax:
script scr_example {
show_debug_message(argument0);
show_debug_message(argument[1]);
}This method is necessary for any script that takes a variable number of arguments.
You can call a script by writing it's name, followed by (, writing a comma seperated argument list, then ending with ). For example:
script_without_args();
script_with_args("hello", "world", 10);You can return a value from a script by using the return keyword followed by the value to return. Note that you must provde a return value. If you just want to quit the script early, you can use the exit keyword.
By default, all scripts return the value undefined. If you want to make sure the script returned something, you can use the function is_undefined(value) to check.