Multiple Return Values in UnrealScript
UnrealScript doesn’t have built-in support for multiple return values like some modern languages. However, we can simulate this behavior using output parameters or by returning a custom struct. Here’s an example using output parameters:
class MultipleReturnValues extends Object;
// This function uses output parameters to return multiple values
function Vals(out int OutA, out int OutB)
{
OutA = 3;
OutB = 7;
}
// The main function in UnrealScript is often called DefaultProperties
function DefaultProperties()
{
local int A, B, C;
// Here we use the output parameters to get multiple values
Vals(A, B);
`log("A:" @ A);
`log("B:" @ B);
// If you only want a subset of the returned values,
// you can ignore one of the output parameters
Vals(, C);
`log("C:" @ C);
}In this UnrealScript version:
We define a
Valsfunction that uses output parametersOutAandOutBto return multiple values.In the
DefaultPropertiesfunction (which is similar tomainin other languages), we callValsand capture the output in local variablesAandB.We use the
`logfunction to print the values, as UnrealScript doesn’t have a direct equivalent tofmt.Println.To demonstrate ignoring one of the values, we call
Valsagain but only capture the second output inC.
To run this code in Unreal Engine:
- Create a new UnrealScript file named
MultipleReturnValues.ucin your project’s Scripts folder. - Paste the above code into the file.
- Compile the script in the Unreal Editor.
- The output will be visible in the Unreal Console or log files.
Note that UnrealScript doesn’t have a direct equivalent to Go’s blank identifier (_), so we simply omit the first parameter when we want to ignore it.
This example demonstrates how to work with multiple return values in UnrealScript, even though the language doesn’t natively support this feature in the same way as some modern languages.