AngelScript quick reference
Types, operators, handles and other basics of the language at a glance.
Statements
if/else,while,do…whileandfor (int i = 0; i < 10; i++)work like in C++, withbreakandcontinue.switchworks only with integers, eachcaseneeds abreakunless it should continue with the next one.autodeclares a variable of the type of its initial value,constone that can't be changed.- Comments are
// lineand/* block */. - Calling a method of a
nullhandle, indexing out of range or dividing an integer by zero raises a script exception, which stops the running function.
Operators
| Operators | Meaning |
|---|---|
+ - * / % | Arithmetic. Dividing two integers drops the remainder, % gives the remainder, also of floating-point numbers. |
** | Power, 2 ** 10 is 1024. |
++ -- | Increment and decrement, either before the value is used (++i) or after it (i++). |
== != < > <= >= | Comparison. |
&& || ^^ ! | Logical and, or, exclusive or and not, also written as and, or, xor and not. && and || skip the right side when the left side decides the result. |
& | ^ ~ | Bitwise and, or, exclusive or and not. |
<< >> >>> | Bit shifts. >> fills with zeros even for negative numbers, >>> keeps the sign. |
= += -= *= /= %= **= &= |= ^= <<= >>= >>>= | Assignment, a += b is the same as a = a + b. |
? : | Conditional, a > b ? a : b is the larger of the two. |
@ is !is | Handles, see Handles and references. |
. [] () :: | Member, index, call and scope, NS::name is a name in a namespace and ::name one in the global namespace. |
Operators have the usual precedence, for example * before + and comparisons before &&. Parentheses make the order clear when unsure.
Primitive types
Types built into the language. Values of these types are always copied when assigned or passed by value.
| Type | Size (bytes) | Values |
|---|---|---|
void | No value. Used only as the return type of functions that don't return anything. | |
bool | 1 | true or false. |
int8 | 1 | Signed integer from -128 to 127. |
int16 | 2 | Signed integer from -32 768 to 32 767. |
int, int32 | 4 | Signed integer from -2 147 483 648 to 2 147 483 647. The usual integer type, whole number literals such as 42 are of this type. int32 is the same type. |
int64 | 8 | Signed integer from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807. |
uint8 | 1 | Unsigned integer from 0 to 255. |
uint16 | 2 | Unsigned integer from 0 to 65 535. |
uint, uint32 | 4 | Unsigned integer from 0 to 4 294 967 295. uint32 is the same type. |
uint64 | 8 | Unsigned integer from 0 to 18 446 744 073 709 551 615. |
float | 4 | Floating-point number with about 7 significant digits. Literals with the f suffix, such as 1.5f, are of this type. |
double | 8 | Floating-point number with about 15 significant digits. Literals with a decimal point and no suffix, such as 1.5, are of this type. |
auto | Not a type of its own, a variable declared as auto takes the type of the value it's initialized with, e.g. auto count = 5; declares an int. | |
? | A value of any type. Only functions of the application use it, for parameters that accept anything, such as const ?&in. |
Literals
- Integers:
42, hexadecimal0x2A, binary0b101010and octal0o52. - Floating-point numbers:
1.5and1e3aredouble,1.5fisfloat. trueandfalse, andnullfor a handle with no object.- Strings:
"text"with escape sequences such as\n,\t,\",\\and\u00E9. Text between"""can span more lines and has no escape sequences. The string type itself is provided by the application.
Conversions
Numbers convert between each other implicitly. Converting to a smaller type, between signed and unsigned types or from a floating-point number to an integer may lose a part of the value, the compiler warns about some of these cases. A floating-point number converted to an integer is truncated towards zero. Use an explicit conversion such as int(value) to make the intent clear.
A handle converts to a handle of a base class or an interface implicitly. The other way, cast<T>(handle) gives a handle of type T, or null if the object isn't of that type.
Handles and references
Other types are either value types, copied just like the primitive types, or reference types, whose objects are reached through handles. In declarations the following marks appear:
@— a handle, a reference to an object of a reference type. Several handles may refer to the same object and the object lives as long as some handle refers to it, unless the application owns it. A handle with no object isnull.@a = @bmakesarefer to the object ofb, whilea = bcopies the object itself.a is btells whether both refer to the same object,a !is nullwhether there's any.&in— a parameter the function only reads, usually together withconst. The function may get a copy of the value.&out— a parameter the function writes a result to, the variable passed in gets the value when the function returns.&inoutor&— a reference to the original variable, which the function can read and change.const— the value can't be changed through this variable or parameter.
Some types of the application don't allow handles at all, a script then only uses the instances the application provides. Such types are marked no handles, value types are marked by value.