Enforce Script Syntax – DayZ

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
{{Stub}}
__TOC__
__TOC__


Line 78: Line 80:
| Negation || !
| Negation || !
|}
|}
== Types ==
=== Primitive Types ===
{| class="wikitable"
|-
! Type name !! Range !! Default Value |-
|-
| int || from −2,147,483,648 to +2,147,483,647 || 0
|-
| float || from ±1.401298E−45 to ±3.402823E+38 || 0.0
|-
| bool || true or false || false
|-
| string || - || "" (empty string)
|-
| vector || see float || (0.0,0.0,0.0)
|-
| void || - ||
|-
| class || - || null
|-
| typename || - || null
|}
=== Strings ===
=== Vectors ===
=== Objects ===
=== Enums ===
=== Typenames ===
=== Templates ===
=== Arrays ===
==== Static Arrays ====
==== Dynamic Arrays ====
== Control Structures ==
Control structures work very similar to c# or c/c++ languages.
=== Conditional structures ===
==== If statement ====
<syntaxhighlight lang=cpp>
void Method()
{
    int a = 4;
    int b = 5;
 
    if (a > 0)
    {
        Print("A is greater than zero!");
    }
    else
    {
        Print("A is not greater than zero!");
    }
   
    if (a > 0 && b > 0)
    {
        Print("A and B are greater than zero!");
    }
   
    if (a > 0 || b > 0)
    {
        Print("A or B are greater than zero!");
    }
 
    // 'else if' example
    if (a > 10)
    {
        Print("a is bigger then 10");
    }
    else if (a > 5)
    {
        Print("a is bigger then 5 but smaller than 10");
    }
    else
    {
        Print("a is smaller then 5");
    }
}
</syntaxhighlight>
==== Switch statement ====
Switch statement supports switching by numbers, constants and strings.
<syntaxhighlight lang=cpp>
void Method()
{
    int a = 2;
 
    switch(a)
    {
        case 1:
            Print("a is 1");
        break;
 
        case 2:
            Print("a is 2"); // this one is called
        break;
       
        default:
            Print("it's something else");
        break;
    }
 
    // using switch with constants
    const int LOW = 0;
    const int MEDIUM = 1;
    const int HIGH = 2;
 
    int quality = MEDIUM;
    switch(quality)
    {
        case LOW:
            // do something
            break;
 
        case MEDIUM:
            // this one is called
            // do something
            break;
 
        case HIGH:
            // do something
            break;
    }
 
    // using switch with strings
    string name = "peter";
    switch(name)
    {
        case "john":
            Print("Hello John!");
            break;
 
        case "michal":
            Print("Hello Michal!");
            break;
 
        case "peter":
            Print("Hello Peter!"); // this one is called
            break;
    }
}
</syntaxhighlight>
=== Iteration structures ===
==== For  ====
The for loop consists of three parts: declaration, condition and increment.
<syntaxhighlight lang=cpp>
void Method()
{
    // this code prints
    // "i = 0"
    // "i = 1"
    // "i = 2"
    for (int i = 0; i < 3; i++)
    {
        Print(i);
    }
}
 
// this function print all elements from dynamic array of strings
void ListArray(TStringArray a)
{
    if (a == null) return; // check if "a" is not null
 
    int i = 0;
    int c = a.Count();
 
    for (i = 0; i < c; i++)
    {
        string tmp = a.Get(i);
        Print(tmp);
    }
}
</syntaxhighlight>
==== Foreach ====
Simpler and more comfortable version of for loop.
<syntaxhighlight lang=cpp>
</syntaxhighlight>
==== While ====
<syntaxhighlight lang=cpp>
void Method()
{
    int i = 0;
 
    // this code prints
    // "i = 0"
    // "i = 1"
    // "i = 2"
 
    while (i < 3)
    { 
        Print(i);
        i++;
    }
}
</syntaxhighlight>


== Classes and Objects ==
== Classes and Objects ==
Line 85: Line 289:
=== Basic Class Example ===
=== Basic Class Example ===


<syntaxhighlight lang=java>
<syntaxhighlight lang=cpp>
class MyClass  
class MyClass  
{
{
Line 117: Line 321:


Example
Example
 
<syntaxhighlight lang=cpp>
  class MyClass {
  class MyClass {
       int attributeExample = 1;
       int attributeExample = 1;
Line 133: Line 337:
  myClassI.attributeExample // 1
  myClassI.attributeExample // 1
  myClassI.myMethod // "This is a string return"
  myClassI.myMethod // "This is a string return"
 
</syntaxhighlight>
=== Basic Script Example ===
=== Basic Script Example ===
<syntaxhighlight lang=java>
<syntaxhighlight lang=cpp>
MyClass myClass = new MyClass();
MyClass myClass = new MyClass();
myClass.myMethod();
myClass.myMethod();
Line 145: Line 349:
In order to get your custom code to work and function on top of another class already in the game, a modded class is where you want to turn to.
In order to get your custom code to work and function on top of another class already in the game, a modded class is where you want to turn to.


<syntaxhighlight lang=java>
<syntaxhighlight lang=cpp>
modded class PlayerBase  
modded class PlayerBase  
{  
{  

Revision as of 16:23, 10 January 2019

Template:Stub

Enforce Script is the language that is used by the Enfusion engine first introduced in DayZ Standalone. It is a Object-Oriented Scripting Language (OOP) that works with objects and classes.

Operators

Operator Priority: Priority of operators is similar than in C language, more info.

Arithmetic Operators

Operation Symbol
Add +
Subtract -
Multiply *
Divide /
Modulo %

Assignments

Operation Symbol
Assign value to variable =
Increment variable by value +=
Decrement variable by value -=
Multiply variable by value *=
Divide variable by value /=
Increment variable by 1 ++
Decrement variable by 1 --

Relational (conditional)

Operation Symbol
More than value >
Less than value <
More or equal to the value >=
Less or equal to the value <=

Others

Category -
Logical &&, ||
Bitwise &, |, ~
String +
Shift <<, >>
Assignment =
Indexing []
Negation !

Types

Primitive Types

Type name Range -
int from −2,147,483,648 to +2,147,483,647 0
float from ±1.401298E−45 to ±3.402823E+38 0.0
bool true or false false
string - "" (empty string)
vector see float (0.0,0.0,0.0)
void -
class - null
typename - null

Strings

Vectors

Objects

Enums

Typenames

Templates

Arrays

Static Arrays

Dynamic Arrays

Control Structures

Control structures work very similar to c# or c/c++ languages.

Conditional structures

If statement

void Method()
{
    int a = 4;
    int b = 5;
  
    if (a > 0)
    {
        Print("A is greater than zero!");
    }
    else
    {
        Print("A is not greater than zero!");
    }
     
    if (a > 0 && b > 0)
    {
        Print("A and B are greater than zero!");
    }
     
    if (a > 0 || b > 0)
    {
        Print("A or B are greater than zero!");
    }
  
    // 'else if' example
    if (a > 10)
    {
        Print("a is bigger then 10");
    }
    else if (a > 5)
    {
        Print("a is bigger then 5 but smaller than 10");
    }
    else
    {
        Print("a is smaller then 5");
    }
}

Switch statement

Switch statement supports switching by numbers, constants and strings.

void Method()
{
    int a = 2;
  
    switch(a)
    {
        case 1:
            Print("a is 1");
        break;
  
        case 2:
            Print("a is 2"); // this one is called
        break;
         
        default:
            Print("it's something else");
        break;
     }
  
    // using switch with constants
    const int LOW = 0;
    const int MEDIUM = 1;
    const int HIGH = 2;
  
    int quality = MEDIUM;
    switch(quality)
    {
        case LOW:
            // do something
            break;
  
        case MEDIUM:
            // this one is called
            // do something
            break;
  
        case HIGH:
            // do something
            break;
    }
  
    // using switch with strings
    string name = "peter";
    switch(name)
    {
        case "john":
            Print("Hello John!");
            break;
  
        case "michal":
            Print("Hello Michal!");
            break;
  
        case "peter":
            Print("Hello Peter!"); // this one is called
            break;
    }
}

Iteration structures

For

The for loop consists of three parts: declaration, condition and increment.

void Method()
{
    // this code prints
    // "i = 0"
    // "i = 1"
    // "i = 2"
    for (int i = 0; i < 3; i++)
    {
        Print(i);
    }
}
  
// this function print all elements from dynamic array of strings
void ListArray(TStringArray a)
{
    if (a == null) return; // check if "a" is not null
  
    int i = 0;
    int c = a.Count();
  
    for (i = 0; i < c; i++)
    {
        string tmp = a.Get(i);
        Print(tmp);
    }
}

Foreach

Simpler and more comfortable version of for loop.

While

void Method()
{
    int i = 0;
  
    // this code prints
    // "i = 0"
    // "i = 1"
    // "i = 2"
  
    while (i < 3)
    {  
        Print(i);
        i++;
    }
}

Classes and Objects

Classes can be seen as a blueprint of an object. An object is an instance of a class. A class can have more than one object.

Basic Class Example

class MyClass 
{
	private string _test;

	void MyClass() 
	{
		// Constructor that will be called when class gets instantiated
	}

	void myMethod()
	{
		// Some code here
	}
	
	string getTest() 
	{
		return _test;
	}

	string setTest(value) 
	{
		_test = value;
	}
}

Instantiate and Use

Use the new operator to instantiate a class. You can access methods or attributes by using dot notation.

Example

 class MyClass {
      int attributeExample = 1;
 
     public MyClass() {
          // Constructor
     }
 
     public void myMethod() {
          return "This is a string return";
     }
 }
 
 MyClass myClassI = new MyClass();
 myClassI.attributeExample // 1
 myClassI.myMethod // "This is a string return"

Basic Script Example

MyClass myClass = new MyClass();
myClass.myMethod();
myClass.setTest("Hello, Enforce Script!");
string str = myClass.getTest(); // str is now "Hello, Enforce Script!"

Modded class

In order to get your custom code to work and function on top of another class already in the game, a modded class is where you want to turn to.

modded class PlayerBase 
{ 
	override void EEKilled(Object killer) 
	{
		// This will call the method in which you are overriding.
		// Do this if you want to keep the original functionality
		super.EEKilled(killer);

		// Custom code here (i.e. Print(killer.GetIdentity().GetName()))
	}
}