-
Notifications
You must be signed in to change notification settings - Fork 0
Features Variables & Types
CCL is strongly typed and supports value and reference types. Most variables are allocated at compile time and by default, value types cannot be created during script execution. (There are exceptions to this)
There is no new
keyword to allocate new reference variables at execution time, so you cannot create new objects in the usual c# way.
The default supported primitive types are:
- bool
- int
- float
- string
Variables are defined by specifying the datatype name and then an identifier.
int x;
Variables can also be assigned to when declared to give them an initial value
int x = 1;
Variables that are not immediately assigned to when declared can have interesting behavior. They are actually set to a default value at compile time. Which means that a variable will be set to it's default type before the script runs the first time. If the variable is modified by the script, its new value will persist the next time it is run
For example,
int i; // should default to 0 for first execution
i += 1; // i = 1 + 0;
return i; // what will this return?
If you run it the first time, you will get a result of 1
If you run it a second time, the script will return 2
Every time this script is executed, the variable i
is incremented, and the value is not reset before the script runs again. If you want the variable i
to start at 0 each time the script is run, you will have to assign a 0 to the variable when it is declared.
int i = 0; // make sure that i is 0
i += 1; // i = 1 + 0;
return i; // will always return 1
You can also declare a variable without a predefined type as object
Object variables can reference primitive, reference, and array types. Under the hood, they are C# objects, and can point to any C# object type.
object x = 1
Any object that does not have a typedef defined in the assembly can only be an object type, so if your context returns a custom class, the only type of variable that can reference it is an object
.
For example, if a function in my context returns a MyClass
type, the only variable that can reference it is an object
object ob = GetMyClass(); // assign a type MyClass to the variable ob
ob.myclassMember = 10; // use the variable like a MyClass type
One caveat with using object variables is that the compiler does not know the type at compile time. Since the type is not known at compile time, the compiler does not know which operators operate on the specified variable.
int i = 1;
int j = i + i; // this is ok, the compiler knows that i is an integer
object ob = i; // set ob to int i
j = ob + ob; // this is not ok, the compiler does not know the type at compile time. object + object doesn't work
This issue is partially rectified by type inference, which is covered in another section
You can cast objects and primitives to other primitive types
int x = (int)1.1; //int
float y = (float)x; //float
int w = (int)"123"; //int
bool z = (bool)1.1; //bool
When casting array literals, they must be surrounded in parenthesis
Vector3 x = (Vector3)([0,0,0]); // ok
Vector3 y = (Vector3)[0,0,0]; // fails miserably
Boolean Booleans are lower case, like C
true
false
Numbers Integers and floats are supported. Doubles and longs are not supported_(yet..)_
If a number literal has a radix point than it is treated as a float, otherwise it is an int
0 // int
0.0 // float
1 // int
1.2 // float
ints can also be represented in hex
0x00 // int (0)
0x0F // int (15)
0xFF // int (255)
Strings Strings can be surrounded with double or single quotes
"string"
'this is a string'
More Detailed information on Array Indexing on Array Indexing
Arrays are declared by putting brackets after a variable declaration's type. The array bounds must be specified at "compile" time, and cannot be changed while the script is running. The array size must be defined by a numeric constant. Arrays cannot have elements of different types, all elements must be the same type. Unless you use an object array object[]
Valid Array Declaration
int[10] arr;
string[100] arr2;
int[4] arr3 = [0, 1, 2, 3];
int[] arr3 = [0, 1, 2, 3]; // Size can be omitted, if you are assigning to a new array
Invalid Array Declarations
int arr[10];
int[] arr = new int[10];
Things to Keep in Mind
When declaring an array, the size must be a numeric literal. expressions and identifiers are not allowed for array size declaration.
Keep in mind! Just like the other variable types, arrays are not created when it is declared at run-time.
The array that the identifier points to is actually created at compile time. If your script modifies the value of one of the elements in the array, that change is persistent throughout multiple executions of the script. For example:
int[1] arr;
arr[0] += 1;
return arr[0]; // what does this return ?
If you run that script once, it will return 1
. That seems reasonable, because the default value for an int is 0, so you would expect int[1] to be initialized to an array that contains one 0. [0]
. so 0 + 1 = 1
right?
If you run the script again, it will return 2
. The reason for that is that arr[0]
was modified the first time the script ran and now arr[0]
equals 1
. It is not re-initialized when the script is run again, it points to the same object and is not recreated each time the script is run.
If you want manually initialize the array at the start of each script, you have to assign a new array literal
int[1] arr = [0]; // new array literal
arr[0] += 1;
return arr[0]; // what does this return? always 1
Array Literals
[0, 1, 2, 3] // int array
[0.0, 1.1, 2.2, 3.3] // float array
["hello", "world"] // string array
Arrays are reference types, so while it is true that you cannot change the size of an array at run-time, you can still assign an array variable to another array of a different size.
int[10] arr; // arr points to an array of int[10]
arr = [0,1,2,3]; // arr now points to a new array of int[4];
// The old array of int[10] is forgotten and will be cleaned up by the garbage collector
Numeric Arrays Mixed arrays of ints and floats are not allowed, but if any elements in an array literal are float types, the entire array will be an float array.
[0, 0, 0, 1] // int array, no floats
[0.0, 1, 1, 1] // float array, at least one float
[0, 0, 0, 1.0] // float array, at least one float
Side Note: Why did I choose to use []
instead of {}
for array literals?
This is one thing that I like about javascript syntax, so I decided to incorporate that syntax into this language. And it is also easier, as curly braces have several uses already. I did not want to have the compiler have to figure out if it was supposed to be a new block scope or the beginning of an array literal...
Questions? Comments?
Contact me at [email protected]
Follow on Twitter: @wasteland_11
Copyright 2019 © Kameron Brooks