Scripting: Keywords – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Remove after class' semicolons) |
Lou Montana (talk | contribs) m (Fix examples and descriptions) |
||
Line 73: | Line 73: | ||
! | ! | ||
=== proto === | === proto === | ||
| Prototyping of internal | | Prototyping of internal method (Engine side). | ||
|- | |- | ||
! | ! | ||
=== native === | === native === | ||
| Native call convention of internal | | Native call convention of internal method (Engine side). | ||
|- | |- | ||
! | ! | ||
=== volatile === | === volatile === | ||
| Internal | | Internal method that may call back to script (hint for compiler that context need to be saved on stack). | ||
|- | |- | ||
! | ! | ||
Line 153: | Line 153: | ||
! | ! | ||
=== event === | === event === | ||
| Hint for tools that the | | Hint for tools that the method should be exposed as Entity script event. | ||
|- | |- | ||
! | ! | ||
Line 211: | Line 211: | ||
! | ! | ||
=== owned === | === owned === | ||
| Modifier for returning internal | | Modifier for returning internal methods. Tells the script-VM that the returning variable (string or array) must not be released. | ||
|- | |- | ||
! | ! | ||
=== out === | === out === | ||
| Modifier for | | Modifier for method parameters. It means that the argument may be changed by the method call. | ||
<enforce> | <enforce> | ||
void Method(out array<string> parameter) | void Method(out array<string> parameter) | ||
{ | { | ||
parameter = | parameter = {}; | ||
parameter.Insert("Hello"); | parameter.Insert("Hello"); | ||
parameter.Insert("there"); | parameter.Insert("there"); | ||
Line 227: | Line 227: | ||
array<string> myArray = null; | array<string> myArray = null; | ||
Method( | Method(myArray); // myArray is now { "Hello", "there" } | ||
</enforce> | </enforce> | ||
|- | |- | ||
! | ! | ||
=== inout === | === inout === | ||
| Modifier for | | Modifier for method parameters. It means that the argument will be used and may be changed by the method call. | ||
<enforce> | <enforce> | ||
class ExampleClass | class ExampleClass | ||
{ | { | ||
private ref array<string> | private ref array<string> m_aOldParameter; | ||
void Method(inout array<string> parameter) | void Method(inout array<string> parameter) | ||
{ | { | ||
parameter = | m_aOldParameter = parameter; | ||
parameter = {}; | |||
parameter.Insert("Hello"); | parameter.Insert("Hello"); | ||
parameter.Insert("there"); | parameter.Insert("there"); | ||
} | } | ||
} | } | ||
Line 251: | Line 251: | ||
array<string> myArray = null; | array<string> myArray = null; | ||
classInstance.Method( | classInstance.Method(myArray); // myArray is now { "Hello", "there" } and classInstance.m_aList is referencing myArray | ||
</enforce> | </enforce> | ||
|- | |- | ||
Line 273: | Line 273: | ||
! | ! | ||
=== notnull === | === notnull === | ||
| Modifier for | | Modifier for method parameter of class pointer type. It means that the method expect the parameter to never be '''null'''; if a null value is provided, a VM Exception is raised and the method is not called. | ||
<enforce> | <enforce> | ||
void Method(notnull IEntity entity) // no need to check for "if (!entity)" inside the method | void Method(notnull IEntity entity) // no need to check for "if (!entity)" inside the method | ||
Line 480: | Line 480: | ||
! | ! | ||
=== super === | === super === | ||
| Refers to the '''parent''' class for the requested variable/ | | Refers to the '''parent''' class for the requested variable/method. | ||
<enforce> | <enforce> | ||
class Gen1 | class Gen1 |
Revision as of 17:58, 16 June 2023
Class Keywords
class |
Declares a class
class ExampleClass
{
} |
---|---|
modded |
See Object Oriented Programming Advanced Usage - Modding.
class ExampleClass
{
}
modded class ExampleClass // inherits and "takes the place" of the original ExampleClass
{
} |
sealed |
A class marked with the sealed keyword cannot be inherited from
sealed class Infertile
{
}
class Child : Infertile // compilation error - a class cannot inherit from a sealed class
{
} |
extends |
states that the declared class inherits from another
class ChildClass extends ParentClass
{
}
// alias
class ChildClass : ParentClass
{
} |
typedef |
Declares an alias for a type
|
Method Keywords
proto |
Prototyping of internal method (Engine side). |
---|---|
native |
Native call convention of internal method (Engine side). |
volatile |
Internal method that may call back to script (hint for compiler that context need to be saved on stack). |
private |
Modifier for class method. The method can be called only from inside of the same class' methods. A modded class will not be able to use it either.
class ExampleClass
{
private void HelloWorld()
{
Print("Hello there");
}
void PublicHelloWorld()
{
HelloWorld(); // works from within the object
}
}
ExampleClass classInstance = new ExampleClass();
classInstance.PublicHelloWorld(); // works - calls PublicHelloWorld() that calls HelloWorld()
classInstance.HelloWorld(); // compilation error - HelloWorld() is private and cannot be accessed |
protected |
Modifier for class method. The method can be called only from inside of class' methods or methods of its extended (children) classes, or a modded class.
class ParentClass
{
protected void HelloWorld()
{
Print("Hello there");
}
}
class ChildClass : ParentClass
{
void PublicHelloWorld()
{
HelloWorld(); // works from within the object
}
}
ChildClass classInstance = new ChildClass();
classInstance.PublicHelloWorld(); // works - calls PublicHelloWorld() that calls HelloWorld()
classInstance.HelloWorld(); // compilation error - HelloWorld() is protected and cannot be accessed
ParentClass classInstance = new ParentClass();
classInstance.HelloWorld(); // compilation error - HelloWorld() is protected and cannot be accessed |
static |
Modifier for class method. The method can be called without object pointer, just by ClassName.methodName(). Only static values/methods can be accessed from a static method.
class ExampleClass
{
static void HelloWorld()
{
Print("Hello there");
}
}
ExampleClass.HelloWorld(); // works
ExampleClass classInstance = new ExampleClass();
classInstance.HelloWorld(); // works |
event |
Hint for tools that the method should be exposed as Entity script event. |
override |
Modifier for class method indicating overriding of base class method. Compiler checks if is method present in base class and if method signature matches.
class ParentClass
{
void HelloWorld()
{
Print("Hello world");
}
}
class ChildClass : ParentClass
{
override void HelloWorld()
{
Print("Hello there");
}
}
ParentClass parentClassInstance = new ParentClass();
parentClassInstance.HelloWorld(); // prints "Hello world"
ChildClass childClassInstance = new ChildClass();
childClassInstance.HelloWorld(); // prints "Hello there" |
sealed |
Modifier for class method. The method cannot be overridden in derived classes.
class ParentClass
{
sealed void HelloWorld()
{
Print("Hello there");
}
}
class ChildClass : ParentClass
{
override void HelloWorld() // compilation error - cannot override a sealed method
{
Print("Hello world");
}
} |
Value Keywords
owned |
Modifier for returning internal methods. Tells the script-VM that the returning variable (string or array) must not be released. |
---|---|
out |
Modifier for method parameters. It means that the argument may be changed by the method call. |
inout |
Modifier for method parameters. It means that the argument will be used and may be changed by the method call.
class ExampleClass
{
private ref array<string> m_aOldParameter;
void Method(inout array<string> parameter)
{
m_aOldParameter = parameter;
parameter = {};
parameter.Insert("Hello");
parameter.Insert("there");
}
}
ExampleClass classInstance = new ExampleClass();
array<string> myArray = null;
classInstance.Method(myArray); // myArray is now { "Hello", "there" } and classInstance.m_aList is referencing myArray |
const |
Declares a constant, that is a value that cannot be modified later.
const string MY_TEXT = "Hello there";
void Method()
{
Print(MY_TEXT); // will print "Hello there"
MY_TEXT = "General Kenobi"; // compilation error - a constant is set in stone and cannot be modified
} |
reference |
Hint for tools (Material editor) that the value may be used as parameter in material. |
notnull |
Modifier for method parameter of class pointer type. It means that the method expect the parameter to never be null; if a null value is provided, a VM Exception is raised and the method is not called.
void Method(notnull IEntity entity) // no need to check for "if (!entity)" inside the method
{
entity.DoSomething(); // the argument can be accessed safely
}
if (myEntity) // it is still needed to check for null before calling the method
{
Method(myEntity);
} |
private |
Value can only be accessed from the class/instance methods. Mutually exclusive with "protected". |
protected |
Value can only be accessed from the class/instance methods or its children. Mutually exclusive with "private".
class ParentClass
{
protected bool m_bProtected;
private bool m_bPrivate;
void SetPrivate(bool value)
{
m_bPrivate = value;
}
}
class ChildClass : ParentClass
{
void SetProtected(bool value)
{
m_bProtected = value; // works - m_bProtected declaration reaches ChildClass
}
}
ChildClass childClassInstance = new ChildClass();
Print(childClassInstance.m_bProtected); // compilation error - m_bProtected is protected and cannot be accessed |
static |
Value belongs to the class and not an instance of it. If public, it can be accessed without object pointer, just by ClassName.variable class ExampleClass
{
static bool bStaticValue;
private static bool m_bPrivate; // it is good practice to make it protected/private and have getters/setters instead
}
Print(ExampleClass.bStaticValue); // works
ExampleClass.bStaticValue = true; // works
ExampleClass classInstance = new ExampleClass();
Print(classInstance.bStaticValue); // works
classInstance.bStaticValue = true; // works |
autoptr |
Modifier for class/pointer values. Pointed object will be automatically destroyed upon end of variable lifetime (end of scope, or deletion of class, that contain it). |
ref |
Strong reference. See Automatic Reference Counting for more information. |
Code Keywords
new |
Creates an instance of the provided class.
class ExampleClass
{
}
ExampleClass classInstance = new ExampleClass(); class ExampleClass
{
void ExampleClass(int myValue) // a method named from the class is the constructor
{
Print(myValue);
}
}
ExampleClass classInstance = new ExampleClass(5); // needs Constructor-defined arguments |
---|---|
delete |
Deletes an object and sets all its references to null.
class ExampleClass
{
void Method()
{
ReferencedClass obj;
if (!obj)
Print("obj is null");
obj = new ReferencedClass();
if (obj)
Print("obj is not null");
delete obj;
if (!obj)
Print("obj is null");
// everything was fine
obj = new ReferencedClass();
if (obj)
Print("obj is not null");
array<ref ReferencedClass> list = {};
list.Insert(obj);
delete obj; // throws a VM Exception
}
} |
thread |
Creates a script thread from a method.
class ExampleClass
{
// calling this method will print
// "MainMethod start"
// "ThreadMethod start",
// "MainMethod end"
// then 500ms later, "ThreadMethod end"
void MainMethod()
{
Print("MainMethod start");
thread ThreadMethod();
Print("MainMethod end");
}
protected void ThreadMethod()
{
Print("ThreadMethod start");
Sleep(500);
Print("ThreadMethod end");
}
}
|
null |
A null value. |
this |
Refers to the method's object itself.
class ExampleClass
{
void MethodA()
{
this.MethodB();
}
void MethodB()
{
Print("It works");
}
} |
super |
Refers to the parent class for the requested variable/method.
class Gen1
{
void Method()
{
Print("Gen1 method");
}
}
class Gen2 : Gen1
{
void Method()
{
Print("Gen2 method");
}
}
class Gen3 : Gen2
{
void Method()
{
super.Method(); // prints "Gen2 method"
}
} |
Code Flow Keywords
if |
verifies a boolean condition. If the condition is true then the code is executed.
|
---|---|
else |
optional, declares code to be executed if the if condition is not met
void Method(int value = 0)
{
if (value > 50)
{
Print("value is > 50");
}
else
{
Print("value is <= 50");
}
} |
for |
processes code while the provided condition is met. The syntax is:
for (initialisation; loopCondition; completionCode)
{
// code here
} void MethodA(notnull array<string> words)
{
for (int i = 0; i < words.Count(); i++)
{
Print(words[i]);
}
}
void MethodB(notnull array<string> words)
{
int i = 0;
for (; i < words.Count(); i++) // initialisation is optional
{
Print(words[i]);
}
}
void MethodC(notnull array<string> words)
{
for (int i = 0; i < words.Count();) // so is completionCode
{
Print(words[i]);
i++;
}
} |
foreach |
processes code for each element of an iterable object (e.g an array). Iteration separator is:.
|
while |
processes code while condition is met. Beware, as it can lock the program on wrong conditions! |
switch |
switches the code to execute depending on the condition
void Method(string word)
{
switch (word)
{
case "Hello":
Print("it's Hello");
break;
case "there":
Print("it's there");
break;
default:
Print("it's not interesting.");
break;
}
} |
return |
stops the code flow and returns the provided value. No value needs to be provided for void methods. |