Functions
First native support for functions in GTA was added in Liberty City Stories. The games prior to that only had limited subroutines invoked with the gosub command. Those subroutines allowed to avoid code duplication; however, they were operating on the same scope as the code invoking them. Any variables used in a subroutine would alter the state of the script.
CLEO Library brought support for functions (dubbed scm func) in early versions, and it became important part of the modern scripting techniques. Each SCM function owns 16 or 32 local variables (depending on the game), none of them would clash with the local variables of the script calling the functions. CLEO5 improved SCM functions a lot by completely isolating script and function state, adding support for string arguments, and more.
Sanny Builder 4 adds new syntactic element to the language to easily create and use SCM functions in the code.
Syntax
To make a new function, use the function keyword.
function <signature>
<body>
endsignature defines function's input parameters and their type, and also an optional return type.
body is the code that runs when you call the function.
A function must end with the end keyword.
Functions are available anywhere in the current scope. Functions defined inside other functions only available in that function.
Example
function sum(a: int, b: int): int
int result = a + b
return result
endSignature
A function's signature defines what types of input arguments the function receives and what type of value it returns. Function arguments may be of a primitive type int, float, string, or a class, e.g. Car or Pickup. Since v4.2 they can also be an array.
String arguments are only supported in CLEO 5. They are always passed as pointers.
A function may have zero parameters. If it has parameters, they are listed between (). Each parameter has a name and a type, separated by a colon. Parameter declaration syntax is similar to that of var..end. Each parameter can be used as a function's local variable in the function body.
modelId is an input parameter of the int type. It can be used as a local variable.
If the function returns something, its type has to be defined after the list of parameters (or the function name, if there are no parameters). E.g.:
Array Arguments
Since v4.2. function arguments can be arrays. To define an array argument, use the following syntax:
Here pos is an array with 3 elements of type float. To call this function, you need to pass three float values:
If you have an array, you can pass it to the function using the spread operator ...:
Functions can have multiple array arguments.
Variadic Functions
The last function argument can be declared as a variadic argument, using the following syntax:
Here, the last argument ...args: int[5] represents an array of integers with a maximum length of 5. Zero or more integers can be passed to this function, and they will be accessible within the function as the elements of the args array.
To find out the actual number of arguments the function was called with, you can use GET_CLEO_ARG_COUNT from CLEO5:
Calling a Function
Functions can be called using its name followed by open and closed braces:
() are required for the function call, even if function receives no arguments. Without () function name is compiled as it's offset or address (if this is a static foreign function):
Function Body
A function's body include all instructions executed when the main code calls the function. The function may have zero instructions. Function parameters can be referenced in the body as local variables. The function may create extra local variables and even new functions, available only within this function:
Return From Function
Function ends at the end keyword. You may exit early using the return keyword.
Exit from a function using return keyword is only supported in CLEO 5 (San Andreas). For other CLEO versions use cleo_return command.
Returning logical values
For a function to be used as a condition in IF..THEN, it must have a special return type: logical.
logical function returns a result of logical expression, or true or false.
A value returned from a logical function sets the script's condition result and be combined with other checks in one IF statement.
Returning Values
The function may return one or multiple values, using the return keyword.
To define a function that returns something, add a colon and a type at the end of the function signature:
To return a value use return followed a value:
To read the returned value, a caller must provide a variable:
If a function may fail and not have a valid result, its return type should be marked as optional and a blank return can be used:
optional keyword must precede the list of return types.
Optional Return Type
Some functions may not be able to return correct values (a fallible function). For example, a function reading a file may fail if the file does not exist. In this case the return type can be marked with the optional keyword:
Function getValues may return 3 integer values or nothing. On calling end, to check whether a fallible function succeeded it can be wrapped into IF..THEN condition like so:
Foreign Functions
CLEO provides an interface for calling game's native functions. There are 4 opcodes that support different calling conventions:
As you may guess, using them directly is not very convenient. These opcodes have their own quirks, like having to provide input arguments in reverse order.
Sanny Builder 4 offers an interface for defining foreign functions in code and using them as regular functions. It can be done by adding calling convention type to a forward declaration.
A cc or calling convention defines who's in charge of cleaning up the stack when function returns.
CLEO and Sanny Builder supports 3 major conventions:
cdecl - caller cleans up the stack
stdcall - callee cleans up the stack
thiscall - callee cleans up the stack. Since
thiscallis a class method function, it additionally receives a pointer to the class instance inecxregister.
You can read more about different types of calling conventions on Wikipedia.
Optional address parameter defines where this function is located in the game memory (static functions). If this address can only be known in runtime, this parameter can be omitted.
A return type can be int, float, or string.
Example
This code invokes a function at address 0x558E30 with argument 42 and stores the returned value in the variable type.
Passing multiple arguments can be done as usual:
Calling a thiscall function requires the first argument to always be a pointer to the class instance.
When function's address is not known at compile time, you still can define a foreign function and use a function pointer to call it by reference. To declare a function pointer, declare a new variable with the function name as the type:
Last updated