Enforce Script Syntax – Arma Reforger

From Bohemia Interactive Community
m (Add content)
(Add data types)
 
Line 9: Line 9:
== Data Types ==
== Data Types ==


See {{Link|Arma Reforger:Scripting: Values#Types}}.
{{Feature|informative|See {{Link|Arma Reforger:Scripting: Values#Types}}.}}
 
There are many types of data, the most common being:
 
{| class="wikitable align-center-col-1"
! Type
! Description
! Example
! Wikipedia
|-
 
! colspan="4" | Native types
 
|-
| <enforce>bool</enforce>
| a bool, a.k.a <enforce inline>true</enforce> or <enforce inline>false</enforce> (and nothing else!)
| <enforce>bool value = true;</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Boolean_data_type|Bool}}
|-
| <enforce>int</enforce>
| an integer, a.k.a a whole number e.g 1, 42, -10, etc.
| <enforce>int value = 42;</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Integer_(computer_science)|Integer}}
|-
| <enforce>float</enforce>
| a floating point value, a.k.a a partial number e.g 1.0, 4.2, -0.1, etc
| <enforce>float value = 0.333;</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Floating-point_arithmetic|Float}}
|-
| <enforce>string</enforce>
| a text value, a.k.a a sequence of characters, e.g "Hello there"
| <enforce>string owk = "Hello" + " " + "there";</enforce>
| see {{Link|https://en.wikipedia.org/wiki/String_(computer_science)|String}}
|-
| <enforce>vector</enforce>
| an array of three float values e.g <enforce inline>{ 1.0, 42.2, 66.6 }</enforce>
| <enforce>vector position = { 5, 1.5, 10 };</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Euclidean_vector|Vector}}
|-
 
! colspan="4" | Objects
 
|-
| <enforce>class</enforce>
| an object able to hold properties and methods.
| see {{Link|Arma Reforger:Object Oriented Programming Basics}}
| see {{Link|https://en.wikipedia.org/wiki/Class_(programming)|Class}}
|-
| <enforce>enum</enforce>
| a structure having static values "listed" as its properties.{{Feature|informative|An enum is not an object ''per se''.}}
| <enforce>SCR_EBloodType myBloodType = SCR_EBloodType.O_POSITIVE;</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Enumerated_type|Enum}}
|-
| static array
| a '''static''' array of elements (of ''a'' type) - it cannot contain a mix of types.{{Feature|important|Arrays (dynamic or static) are the only list type that can be initialised in line.}}
| <enforce>string helloThereWords[] = { "Hello", "there" };</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Array_(data_type)|Array}}
|-
| <enforce>array<x></enforce>
| a '''dynamic''' array of elements (of ''a'' type) - it cannot contain a mix of types.{{Feature|important|Arrays (dynamic or static) are the only list type that can be initialised in line.}}
| <enforce>array<string> helloThereWords = { "Hello", "there" };</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Array_(data_type)|Array}}
|-
| <enforce>set<x></enforce>
| a set of '''''unique''''' elements, it can only contain one type of elements.{{Feature|important|Values are '''not''' stored in insertion order.}}
| <enforce>
set<string> helloThereWords = new set<string>();
helloThereWords.Insert("Hello");
helloThereWords.Insert("there");
helloThereWords.Insert("Hello"); // ignored
Print(helloThereWords.Count()); // prints '2'
</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Set_(abstract_data_type)|Set}}
|-
| <enforce>map<x, y></enforce>
| a map constituted of key-value pairs, where the key aspect is used to obtain the value.{{Feature|important|Values are '''not''' stored in insertion order.}}
| <enforce>
map<string, int> wordsAndLength = new map<string, int>();
wordsAndLength.Insert("General", 7);
wordsAndLength.Insert("Kenobi", 6);
Print(replyWordsAndLength.Get("Kenobi")); // prints '6'
</enforce>
| see {{Link|https://en.wikipedia.org/wiki/Map_%28higher-order_function%29|Map}}
|}




Line 21: Line 104:
=== Assignation ===
=== Assignation ===


Assignation operations cast the first value to the expected type; see below:
Assignation operations convert the value to the expected type; see below:
<enforce>
<enforce>
// adding int and int
// adding int and int
Line 36: Line 119:
float result = 1.9 + 1; // result == 2.9
float result = 1.9 + 1; // result == 2.9


bool invalid = new SCR_Ray(); // error
bool invalid = new SCR_Ray(); // error: Types 'SCR_Ray' and 'bool' are unrelated
SCR_Ray instance = new SCR_Ray();
SCR_Ray instance = new SCR_Ray();
bool valid = instance; // valid == true
bool valid = instance; // valid == true
bool result = 42; // result == true
</enforce>
</enforce>


Line 63: Line 147:
</enforce>
</enforce>


{{Feature|important|<enforce inline>if (intValue)</enforce> is '''not''' recommended by {{Link|Arma Reforger:Scripting: Best Practices}}.}}
{{Feature|important|<enforce inline>if (intValue)</enforce> and <enforce inline>if (stringValue)</enforce> are '''not''' recommended by {{Link|Arma Reforger:Scripting: Best Practices}}.}}





Latest revision as of 15:39, 9 December 2025

Enforce Script is the language that is used by the Enfusion engine first introduced in DayZ Standalone. It is an Object-Oriented scripting language that works with objects and classes and is similar to the C# programming language.



Data Types

There are many types of data, the most common being:

Type Description Example Wikipedia
Native types
a bool, a.k.a true or false (and nothing else!)
bool value = true;
see Bool
an integer, a.k.a a whole number e.g 1, 42, -10, etc.
int value = 42;
see Integer
a floating point value, a.k.a a partial number e.g 1.0, 4.2, -0.1, etc
float value = 0.333;
see Float
a text value, a.k.a a sequence of characters, e.g "Hello there"
string owk = "Hello" + " " + "there";
see String
an array of three float values e.g { 1.0, 42.2, 66.6 }
vector position = { 5, 1.5, 10 };
see Vector
Objects
class
an object able to hold properties and methods. see Object Oriented Programming Basics see Class
enum
a structure having static values "listed" as its properties.
An enum is not an object per se.
SCR_EBloodType myBloodType = SCR_EBloodType.O_POSITIVE;
see Enum
static array a static array of elements (of a type) - it cannot contain a mix of types.
Arrays (dynamic or static) are the only list type that can be initialised in line.
string helloThereWords[] = { "Hello", "there" };
see Array
a dynamic array of elements (of a type) - it cannot contain a mix of types.
Arrays (dynamic or static) are the only list type that can be initialised in line.
array<string> helloThereWords = { "Hello", "there" };
see Array
set<x>
 a set of unique elements, it can only contain one type of elements.
Values are not stored in insertion order.
set<string> helloThereWords = new set<string>(); helloThereWords.Insert("Hello"); helloThereWords.Insert("there"); helloThereWords.Insert("Hello"); // ignored Print(helloThereWords.Count()); // prints '2'
see Set
map<x, y>
a map constituted of key-value pairs, where the key aspect is used to obtain the value.
Values are not stored in insertion order.
map<string, int> wordsAndLength = new map<string, int>(); wordsAndLength.Insert("General", 7); wordsAndLength.Insert("Kenobi", 6); Print(replyWordsAndLength.Get("Kenobi")); // prints '6'
see Map


Object-Oriented Programming

See Object Oriented Programming Basics and Object Oriented Programming Advanced Usage.


Operations

Assignation

Assignation operations convert the value to the expected type; see below:

// adding int and int int result = 1 + 2; // result == 3 // adding float and float float result = 1.25 + 2.75; // result == 4 // adding int and float int result = 1 + 0.9; // result == 1 - int truncates a float result int result = 0.9 + 0.9 + 0.9; // result == 2 - 2.7 truncated to 2 float result = 0.9 + 0.9 + 0.9; // result == 2.7 float result = 1 + 1.9; // result == 2.9 float result = 1.9 + 1; // result == 2.9 bool invalid = new SCR_Ray(); // error: Types 'SCR_Ray' and 'bool' are unrelated SCR_Ray instance = new SCR_Ray(); bool valid = instance; // valid == true bool result = 42; // result == true

Logic

SCR_Ray instance = new SCR_Ray(); if (instance) // identical to "instance != null" Print("Instance exists"); else Print("Instance does not exists"); string text; if (text) // identical to "!text.IsEmpty()" Print("Text is not empty"); else Print("Text is empty"); int val = 42; // same with float if (val) // identical to "val != 0" Print("Val is not zero"); else Print("Val is zero");

if (intValue) and if (stringValue) are not recommended by Scripting: Best Practices.


🏗
This article is a work in progress!