Skip to content

Scripts

mystborn edited this page Jun 22, 2018 · 3 revisions

Scripts are the bread and butter of TaffyScript. A script is just another name for a function or method.

Defining a Script

To define a script, start with script then the script name. Then inside of a block, write the code! Example:

script scr_example {
    print("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.

Arguments

If you want your script to have arguments, you can add them to the script signature like so:

script scr_example(arg1, arg2) {
    print(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") {
    print(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 {
    print(argument0);
    print(argument[1]);
}

You can determine how many arguments were passed to a script using the keyword argument_count:

script print_argument_count {
    print(argument_count);
}

Calling a script

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);

Return Values

You can exit from a script early using the return keyword. Optionally, it can be followed by a value to return. By default, all scripts return the value null. If you want to make sure the script returned something, you can use the function is_null(value) to check.

Examples

This is an example of a script that does something similar to string.Join in c#.

script scr_join(seperator) {
    var result = string(argument1); //The result start with the second argument by itself, then appends the rest of the arguments with the seperator string in between.
    for(var i = 2; i < argument_count; i++) {
        result += seperator + string(argument[i]);
    }
    return result;
}

script main() {
    print(scr_join(", ", "moo", 0, 32.5, "cow", "etc"));
    
    // Output: moo, 0, 32.5, cow, etc
}
Clone this wiki locally