Variable
Syntax¶
Variables are accessed by a scope keyword followed by a name.
In DiamondFire, variables do not need to be declared, so they don't need to be declared in Terracotta either. Think of variables in Terracotta as one-to-one representations of variable code items.
This means that whenever a variable is being referenced, you must provide its scope. This holds true even if a variable of that name has been referenced with a given scope already.
local message = "Hello world!";
# specifying the scope here is REQUIRED!
default:SendMessage(local message);
# any scope can be used
default:SendMessage(local message, global message);
String Names¶
To use text codes or special characters in variable names, wrap the variable name in a string and wrap the string in parenthese.
This string value does not accept expressions! It must be a single string literal.
Types¶
Variable types only matter when using variables in Expressions. For any other uses like inserting them into arguments, types do not matter. As of now, there is no strict typing mode.
Terracotta has a level of type inference built in. Types can be inferred in the following scenarios:
Type inference scenarios (click to expand)
-
Setting a variable to a value
-
Setting a variable to the result of a function
-
Any action that has a return type controlled by tags
-
SetToRandom if all arguments are the same type
-
Inside if var?IsType
-
Accessing parameter values
In many cases, like accessing globals created in other scripts, that's not enough. So, anywhere a variable is referenced, it can specify its expected type by putting : <type>
after the variable name.
When specified in an expression, the type hint is local to that one use of the variable.
FUNCTION GoToSpawn;
default:Teleport(global spawnLocation: loc + vec(0,1,0));
wait(20);
# this will fail to compile because the compiler doesn't know the type of spawnLocation
default:Teleport(global spawnLocation + vec(0,10,0));
Types can be given to variables on their own and will persist for the rest of the file unless overridden.