Difference between revisions of "Arma Reforger:Scripting: Keywords"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (1 revision imported)
m (Text replacement - "lang="cpp">" to "lang="C#">")
Line 7: Line 7:
 
=== class ===
 
=== class ===
 
| Declares a class
 
| Declares a class
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 16: Line 16:
 
=== modded ===
 
=== modded ===
 
| See Modded Class
 
| See Modded Class
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 29: Line 29:
 
=== sealed ===
 
=== sealed ===
 
| A class marked with the '''sealed''' keyword cannot be inherited from
 
| A class marked with the '''sealed''' keyword cannot be inherited from
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
sealed class Infertile
 
sealed class Infertile
 
{
 
{
Line 42: Line 42:
 
=== extends ===
 
=== extends ===
 
| states that the declared class inherits from another
 
| states that the declared class inherits from another
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ChildClass extends ParentClass
 
class ChildClass extends ParentClass
 
{
 
{
Line 56: Line 56:
 
=== typedef ===
 
=== typedef ===
 
| Declares an alias for a type
 
| Declares an alias for a type
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
typedef string Text;
 
typedef string Text;
 
   
 
   
Line 86: Line 86:
 
=== private ===
 
=== 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.
 
| 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.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 108: Line 108:
 
=== protected ===
 
=== 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.
 
| 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.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ParentClass
 
class ParentClass
 
{
 
{
Line 136: Line 136:
 
=== static ===
 
=== 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.
 
| 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.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 158: Line 158:
 
=== override ===
 
=== 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.
 
| Modifier for class method indicating overriding of base class method. Compiler checks if is method present in base class and if method signature matches.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ParentClass
 
class ParentClass
 
{
 
{
Line 185: Line 185:
 
=== sealed ===
 
=== sealed ===
 
| Modifier for class method. The method cannot be overridden in derived classes.
 
| Modifier for class method. The method cannot be overridden in derived classes.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ParentClass
 
class ParentClass
 
{
 
{
Line 216: Line 216:
 
=== out ===
 
=== out ===
 
| Modifier for function parameters. It tells that variable will be changed by function call.
 
| Modifier for function parameters. It tells that variable will be changed by function call.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void Method(out array<string> parameter)
 
void Method(out array<string> parameter)
 
{
 
{
Line 233: Line 233:
 
=== inout ===
 
=== inout ===
 
| Modifier for function parameters. It tells that variable will be used and then changed by function call.
 
| Modifier for function parameters. It tells that variable will be used and then changed by function call.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 257: Line 257:
 
=== const ===
 
=== const ===
 
| Declares a constant, that is a value that cannot be modified later.
 
| Declares a constant, that is a value that cannot be modified later.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
const string MY_TEXT = "Hello there";
 
const string MY_TEXT = "Hello there";
 
   
 
   
Line 274: Line 274:
 
=== notnull ===
 
=== notnull ===
 
| Modifier for function parameter of class pointer type. It tells that function expect the parameter will never be '''null'''. It shows error eventually and function won't be called.
 
| Modifier for function parameter of class pointer type. It tells that function expect the parameter will never be '''null'''. It shows error eventually and function won't be called.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
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 289: Line 289:
 
=== private ===
 
=== private ===
 
| Value can only be accessed from the class/instance methods. Mutually exclusive with "protected".
 
| Value can only be accessed from the class/instance methods. Mutually exclusive with "protected".
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 307: Line 307:
 
=== protected ===
 
=== protected ===
 
| Value can only be accessed from the class/instance methods or its children. Mutually exclusive with "private".
 
| Value can only be accessed from the class/instance methods or its children. Mutually exclusive with "private".
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ParentClass
 
class ParentClass
 
{
 
{
Line 334: Line 334:
 
=== static ===
 
=== static ===
 
| Value belongs to the class and not an instance of it. If public, it can be accessed without object pointer, just by <code>ClassName.variable</code>
 
| Value belongs to the class and not an instance of it. If public, it can be accessed without object pointer, just by <code>ClassName.variable</code>
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 366: Line 366:
 
=== new ===
 
=== new ===
 
| Creates an instance of the provided class.
 
| Creates an instance of the provided class.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 373: Line 373:
 
ExampleClass classInstance = new ExampleClass();
 
ExampleClass classInstance = new ExampleClass();
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 389: Line 389:
 
| Deletes an object and sets all its references to {{hl|null}}.
 
| Deletes an object and sets all its references to {{hl|null}}.
 
{{Feature|important|It is not possible to delete an object if there is an external reference to it, like an array.}}
 
{{Feature|important|It is not possible to delete an object if there is an external reference to it, like an array.}}
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 431: Line 431:
 
=== this ===
 
=== this ===
 
| Refers to the method's object itself.
 
| Refers to the method's object itself.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class ExampleClass
 
class ExampleClass
 
{
 
{
Line 449: Line 449:
 
=== super ===
 
=== super ===
 
| Refers to the '''parent''' class for the requested variable/function.
 
| Refers to the '''parent''' class for the requested variable/function.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
class Gen1
 
class Gen1
 
{
 
{
Line 484: Line 484:
 
=== if ===
 
=== if ===
 
| verifies a boolean condition. If the condition is true then the code is executed.
 
| verifies a boolean condition. If the condition is true then the code is executed.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void Method(int value = 0)
 
void Method(int value = 0)
 
{
 
{
Line 497: Line 497:
 
=== else ===
 
=== else ===
 
| optional, declares code to be executed if the if condition is not met
 
| optional, declares code to be executed if the if condition is not met
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void Method(int value = 0)
 
void Method(int value = 0)
 
{
 
{
Line 514: Line 514:
 
=== for ===
 
=== for ===
 
| processes code while the provided condition is met. The syntax is:
 
| processes code while the provided condition is met. The syntax is:
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
for (initialisation; loopCondition; completionCode)
 
for (initialisation; loopCondition; completionCode)
 
{
 
{
Line 520: Line 520:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void MethodA(notnull array<string> words)
 
void MethodA(notnull array<string> words)
 
{
 
{
Line 551: Line 551:
 
=== foreach ===
 
=== foreach ===
 
| processes code for each element of an iterable object (e.g an array). Iteration separator is{{hl|:}}.
 
| processes code for each element of an iterable object (e.g an array). Iteration separator is{{hl|:}}.
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
// array example
 
// array example
 
void Method(notnull array<string> words)
 
void Method(notnull array<string> words)
Line 566: Line 566:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
// map example
 
// map example
 
void Method(notnull map<string, int> data)
 
void Method(notnull map<string, int> data)
Line 580: Line 580:
 
=== while ===
 
=== while ===
 
| processes code while condition is met. Beware, as it can lock the program on wrong conditions!
 
| processes code while condition is met. Beware, as it can lock the program on wrong conditions!
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void Method(array<string> words)
 
void Method(array<string> words)
 
{
 
{
Line 595: Line 595:
 
=== switch ===
 
=== switch ===
 
| switches the code to execute depending on the condition
 
| switches the code to execute depending on the condition
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="C#">
 
void Method(string word)
 
void Method(string word)
 
{
 
{

Revision as of 11:17, 19 May 2022

Class Keywords

class

Declares a class
class ExampleClass
{
}

modded

See Modded Class
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
typedef string Text;
 
class ExampleClass
{
	Text m_sMyText = "This is my text, that is a string";
}


Method Keywords

proto

Prototyping of internal function (Engine side).

native

Native call convention of internal function (Engine side).

volatile

Internal function 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 function 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 functions. Tells to script-VM that returning variable (string or array) must not be released.

out

Modifier for function parameters. It tells that variable will be changed by function call.
void Method(out array<string> parameter)
{
	parameter = new array<string>();
	parameter.Insert("Hello");
	parameter.Insert("there");
}
 
// ...
 
array<string> myArray = null;
Method(array); // myArray is now { "Hello", "there" }

inout

Modifier for function parameters. It tells that variable will be used and then changed by function call.
class ExampleClass
{
	private ref array<string> m_aList;

	void Method(inout array<string> parameter)
	{
		parameter = new array<string>();
		parameter.Insert("Hello");
		parameter.Insert("there");

		m_aList = parameter;
	}
}
 
ExampleClass classInstance = new ExampleClass();
 
array<string> myArray = null;
classInstance.Method(array); // 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 function parameter of class pointer type. It tells that function expect the parameter will never be null. It shows error eventually and function won't be 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".
class ExampleClass
{
	private bool m_bIsActive;

	void SetActive(bool value)
	{
		m_bIsActive = value;
	}
}
 
ExampleClass classInstance = new ExampleClass();
Print(classInstance.m_bIsActive); // compilation error - m_bIsActive is private and cannot be accessed

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.


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.
It is not possible to delete an object if there is an external reference to it, like an array.
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 thread (parallel processing) from a method. Returns the thread, not the method value.

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/function.
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.
void Method(int value = 0)
{
	if (value > 50)
	{
		Print("value is > 50");
	}
}

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:.
// array example
void Method(notnull array<string> words)
{
	foreach (string word : words)
	{
		Print(word);
	}

	foreach (string word, int index : words)
	{
		PrintFormat("word #%1 is %2", index, word);
	}
}
// map example
void Method(notnull map<string, int> data)
{
	foreach (string key, int value : data)
	{
		PrintFormat("Key %1's value is %2", key, value);
	}
}

while

processes code while condition is met. Beware, as it can lock the program on wrong conditions!
void Method(array<string> words)
{
	int i = 0;
	while (i < words.Count())
	{
		Print(words[i]);
		i++; // do not forget it!
	}
}

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.