Tutorial: Angelscript overview
Angelscript is a powerful object-oriented C/C++ like scripting language. Below are some very simple examples that should give you a brief overview of its syntax.
There's a lot more features in the language that is not included in this page, so for a more in-depth overview, read the official Angelscript documentation.
Functions
int GetNumber()
{
return 5;
}
void PrintNumber(int num)
{
print("Number: " + num);
}
void Main()
{
int num = GetNumber();
PrintNumber(num);
}
Variables
void Main()
{
int a = 5;
float b = 0.5f;
double c = 0.5;
uint d = 0xFFFFFFFF;
uint64 e = 0xFFFFFFFFFFFFFFFF;
bool f = false;
auto g = 1;
auto h = 0.5f;
auto i = true;
auto j = d;
}
Statements
void Main()
{
int num = 10;
if (num == 10) {
print("Num is 10!");
} else if (num == 5) {
print("Num is 5!");
} else {
print("Num is not 10!");
}
for (int i = 0; i < 10; i++) {
print("Let's count: " + i);
}
int x = 50;
while (x > 25) {
x--;
}
do {
x--;
} while (x > 0);
}
Classes
class Foo
{
void DoSomething()
{
print("Something!");
}
}
void Main()
{
Foo f;
f.DoSomething();
}
Class inheritance
class Bar
{
void DoSomething()
{
}
}
class Foo : Bar
{
void DoSomething() override
{
print("Do something!");
}
}
void Main()
{
Bar b;
b.DoSomething(); // Nothing
Foo f;
f.DoSomething(); // "Do something!"
}
Class constructors
class Bar
{
Bar()
{
print("Bar ctor");
}
}
class Foo : Bar
{
Foo()
{
super();
print("Foo ctor");
}
}
Handles
class Foo
{
void DoSomething()
{
print("Something");
}
}
void CallHandle(Foo@ f)
{
f.DoSomething();
}
void Main()
{
Foo valf;
valf.DoSomething(); // OK
Foo@ f = null;
f.DoSomething(); // Null reference exception
f = Foo(); // Same as f.opAssign(Foo()) so this is a null reference exception
@f = Foo(); // OK
f.DoSomething(); // OK
Foo@ f2 = null;
if (f2 == null) { } // Same as if (f2.opEquals(null)) so this is a null reference exception
if (f2 != null) { } // Same as if (!f2.opEquals(null)) so this is a null reference exception
if (f2 is null) { } // OK
if (f2 !is null) { } // OK
f2 = null; // Missing opEquals
@f2 = null; // OK
}
References
void PrintString(const string &in str)
{
print("String: \"" + str + "\"");
}
void Main()
{
string foo = "Foo bar";
PrintString(foo);
}
Interfaces
interface IFoo
{
void DoSomething();
}
class Foo : IFoo
{
void DoSomething()
{
print("Something!");
}
}
Arrays
void Main()
{
int[] arr; // Same as array<int> arr;
arr.InsertLast(1);
arr.InsertLast(2);
arr.InsertLast(3);
for (uint i = 0; i < arr.Length; i++) {
print("arr[" + i + "] = " + arr[i]);
}
print("Index of 2: " + arr.Find(2)); // 1
print("Index of 4: " + arr.Find(4)); // -1
arr.RemoveAt(2);
print("Length is now: " + arr.Length); // 2
int[]@ arrRef = arr;
arrRef.RemoveAt(0);
print("arrRef: " + arrRef.Length); // 1
print("arr: " + arr.Length); // 1
}
Ref counting
void Main()
{
int[]@ arrRef = null;
{
int[] arr;
arr.InsertLast(1);
arr.InsertLast(2);
arr.InsertLast(3);
@arrRef = arr;
}
// arr is not destructed yet, there's still a reference to it!
for (uint i = 0; i < arrRef.Length; i++) {
print("arrRef[" + i + "] = " + arrRef[i]);
}
}