Object Oriented scripting shell: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "Category:ArmA 2: Operation Arrowhead" to "Category:Arma 2: Operation Arrowhead")
m (Text replacement - "{{GameCategory|arma2|Operation Arrowhead}}" to "{{GameCategory|arma2oa}}")
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Introduction ==
{{TOC|side}}
During the implementation phase of the [[Multiplayer_Armory | Multiplayer Armory]], it was decided to make a scripted system which uses as many Object Oriented (OO) techniques as possible (without altering the engine). This page will describe the scripted scripting shell which is used to this effect.
During the implementation phase of the [[Multiplayer_Armory | Multiplayer Armory]], it was decided to make a scripted system which uses as many Object Oriented (OO) techniques as possible (without altering the engine). This page will describe the scripted scripting shell which is used to this effect.




== CfgOO ==


== CfgOO ==
Classes, their attributes and methods are defined in configs:
Classes, their attributes and methods are defined in configs:


  class CfgOO
<syntaxhighlight lang="cpp">
  {
class CfgOO
      {{codecomment|//Name used in method prefix}}
{
      class SomeClass  
// Name used in method prefix
      {
class SomeClass  
        {{codecomment|//All attributes for this class, which will be configured for}}
{
        {{codecomment|//each instantiated object of this class}}
// All attributes for this class, which will be configured for
        class Attributes  
// each instantiated object of this class
        {
class Attributes  
            {{codecomment|//Class name determines the name of the attribute in Logic variable space}}
{
            {{codecomment|//Initialized with default value for this type}}
// Class name determines the name of the attribute in Logic variable space
            class someAttribute
// Initialized with default value for this type
            {
class someAttribute
              type = "SCALAR"; {{codecomment|//Game variable type (see supported types below) (String)}}
{
              defaultValue = 0; {{codecomment|//Default value [optional] (Any)}}
type = "SCALAR"; // Game variable type (see supported types below) (String)
            };
defaultValue = 0; // Default value [optional] (Any)
        };
};
 
};
        {{codecomment|//All methods for this class, which will be precompiled and stored in BIS_OO_<Class>_<Method>}}
        class Methods
        {
            {{codecomment|//Class name determines the name of the method}}
            class SomeMethod
            {
              parameterTypes[] = {"STRING", "SCALAR"}; {{codecomment|//Types of each parameter, used for validation (Array of String)}}
              returnType = "SCALAR"; {{codecomment|//The default return value is based on this (String)}}
              script = "path\someMethod.sqf"; {{codecomment|//Function implementing the method to precompile (String)}}
            };
        };
      };
  };


// All methods for this class, which will be precompiled and stored in BIS_OO_<Class>_<Method>
class Methods
{
// Class name determines the name of the method
class SomeMethod
{
parameterTypes[] = { "STRING", "SCALAR" }; // Types of each parameter, used for validation (Array of String)
returnType = "SCALAR"; // The default return value is based on this (String)
script = "path\someMethod.sqf"; // Function implementing the method to precompile (String)
};
};
};
};
</syntaxhighlight>




== Functions ==
== Functions ==
=== addClass ===
=== addClass ===
This function loads a class in the shell and it precompiles all of its methods. These methods are then stored in global variables with the following name: ''BIS_OO_<ClassName>_<methodName>''.
This function loads a class in the shell and it precompiles all of its methods. These methods are then stored in global variables with the following name: ''BIS_OO_<ClassName>_<methodName>''.


  {{codecomment|//Assuming the function was loaded into BIS_OO_addClass}}
{{cc|Assuming the function was loaded into BIS_OO_addClass}}
  className:String call BIS_OO_addClass;
className:String [[call]] [[BIS_OO_addClass]];


=== createObject ===


=== createObject ===
To instantiate an object of a loaded class, use this function. It will create a Logic object with all of the defined attributes as variables. This function will also automatically call the class' constructor. This constructor will already receive a reference to the created object as parameter, but additional parameters can be provided.
To instantiate an object of a loaded class, use this function. It will create a Logic object with all of the defined attributes as variables. This function will also automatically call the class' constructor. This constructor will already receive a reference to the created object as parameter, but additional parameters can be provided.


  {{codecomment|//The function is automatically loaded into BIS_OO_createObject after adding the first class}}
{{cc|The function is automatically loaded into BIS_OO_createObject after adding the first class}}
  createdObject:Object = [className:String(, parameter1:Any)] call BIS_OO_createObject;
createdObject:Object = [className:String(, parameter1:Any)] [[call]] [[BIS_OO_createObject]];




== Supported types ==


== Supported types ==
These are the data types supported by the OO shell:
These are the data types supported by the OO shell:
* "SCALAR"
* "SCALAR"
Line 69: Line 72:




== Sample ==


== Sample ==
Config:
Config:
  class CfgOO
<syntaxhighlight lang="cpp">
  {
class CfgOO
    class MyClass  
{
    {
class MyClass  
      class Attributes  
{
      {
class Attributes  
        class myAttribute {type = "SCALAR";};
{
      };
class myAttribute { type = "SCALAR"; };
      class Methods  
};
      {
class Methods  
        {{codecomment|//Constructor method}}
{
        class MyClass  
// Constructor method
        {
class MyClass  
          parameterTypes[] = {"OBJECT"};
{
          script = "path\data\scripts\classes\MyClass\MyClass.sqf";
parameterTypes[] = { "OBJECT" };
        };
script = "path\data\scripts\classes\MyClass\MyClass.sqf";
        class myMethod  
};
        {
class myMethod  
          parameterTypes[] = {"OBJECT", "SCALAR", "STRING"};
{
          returnType = "SCALAR";
parameterTypes[] = { "OBJECT", "SCALAR", "STRING" };
          script = "path\data\scripts\classes\MyClass\myMethod.sqf";
returnType = "SCALAR";
        };
script = "path\data\scripts\classes\MyClass\myMethod.sqf";
      };
};
    };
};
  };
};
};
</syntaxhighlight>


Script:
Script:
  {{codecomment|//Precompiles all methods into global variables}}
{{cc|Precompiles all methods into global variables}}
  {{codecomment|//Naming convention: BIS_OO_<Class>_<method>}}
{{cc|Naming convention: BIS_OO_<Class>_<method>}}
  {{codecomment|//Here: BIS_OO_MyClass_MyClass and BIS_OO_MyClass_myMethod}}
{{cc|Here: BIS_OO_MyClass_MyClass and BIS_OO_MyClass_myMethod}}
  "MyClass" call (compile (preprocessFileLineNumbers "ca\modules_e\oo\data\scripts\functions\addClass.sqf"));
"MyClass" [[call]] ([[compile]] ([[preprocessFileLineNumbers]] "ca\modules_e\oo\data\scripts\functions\addClass.sqf"));
{{cc|Creates a Logic with all attributes initialized}}
{{cc|Invokes the constructor}}
[[private]] "_myClassObject";
_myClassObject = ["MyClass"] [[call]] [[BIS_OO_createObject]];
    
    
  {{codecomment|//Creates a Logic with all attributes initialized}}
{{cc|Invoking a method and getting an attribute}}
  {{codecomment|//Invokes the constructor}}
[[private]] ["_return", "_value"];
  private ["_myClassObject"];
_return = [<nowiki/>[[player]], 1, "Hello"] [[call]] '''BIS_OO_MyClass_myMethod''';
  _myClassObject = ["MyClass"] call BIS_OO_createObject;
_value = _myClassObject [[getVariable]] "myAttribute";
 
 
  {{codecomment|//Invoking a method and getting an attribute}}
  private ["_return", "_value"];
  _return = [player, 1, "Hello"] call BIS_OO_MyClass_myMethod;
  _value = _myClassObject getVariable "myAttribute";


[[Category:Arma 2: Operation Arrowhead]]
{{GameCategory|arma2oa}}

Revision as of 23:44, 30 December 2020

During the implementation phase of the Multiplayer Armory, it was decided to make a scripted system which uses as many Object Oriented (OO) techniques as possible (without altering the engine). This page will describe the scripted scripting shell which is used to this effect.


CfgOO

Classes, their attributes and methods are defined in configs:

class CfgOO
{
	// Name used in method prefix
	class SomeClass 
	{
		// All attributes for this class, which will be configured for
		// each instantiated object of this class
		class Attributes 
		{
			// Class name determines the name of the attribute in Logic variable space
			// Initialized with default value for this type
			class someAttribute
			{
				type = "SCALAR";	// Game variable type (see supported types below) (String)
				defaultValue = 0;	// Default value [optional] (Any)
			};
		};

		// All methods for this class, which will be precompiled and stored in BIS_OO_<Class>_<Method>
		class Methods 
		{
			// Class name determines the name of the method
			class SomeMethod 
			{
				parameterTypes[] = { "STRING", "SCALAR" };	// Types of each parameter, used for validation (Array of String)
				returnType = "SCALAR";						// The default return value is based on this (String)
				script = "path\someMethod.sqf";				// Function implementing the method to precompile (String)
			};
		};
	};
};


Functions

addClass

This function loads a class in the shell and it precompiles all of its methods. These methods are then stored in global variables with the following name: BIS_OO_<ClassName>_<methodName>.

// Assuming the function was loaded into BIS_OO_addClass
className:String call BIS_OO_addClass;

createObject

To instantiate an object of a loaded class, use this function. It will create a Logic object with all of the defined attributes as variables. This function will also automatically call the class' constructor. This constructor will already receive a reference to the created object as parameter, but additional parameters can be provided.

// The function is automatically loaded into BIS_OO_createObject after adding the first class
createdObject:Object = [className:String(, parameter1:Any)] call BIS_OO_createObject;


Supported types

These are the data types supported by the OO shell:

  • "SCALAR"
  • "STRING"
  • "BOOL"
  • "ARRAY"
  • "OBJECT"
  • "CONFIG"
  • "GROUP"
  • "TASK"


Sample

Config:

class CfgOO
{
	class MyClass 
	{
		class Attributes 
		{
			class myAttribute { type = "SCALAR"; };
		};
		class Methods 
		{
			// Constructor method
			class MyClass 
			{
				parameterTypes[] = { "OBJECT" };
				script = "path\data\scripts\classes\MyClass\MyClass.sqf";
			};
			class myMethod 
			{
				parameterTypes[] = { "OBJECT", "SCALAR", "STRING" };
				returnType = "SCALAR";
				script = "path\data\scripts\classes\MyClass\myMethod.sqf";
			};
		};
	};
};

Script:

// Precompiles all methods into global variables
// Naming convention: BIS_OO_<Class>_<method>
// Here: BIS_OO_MyClass_MyClass and BIS_OO_MyClass_myMethod
"MyClass" call (compile (preprocessFileLineNumbers "ca\modules_e\oo\data\scripts\functions\addClass.sqf"));

// Creates a Logic with all attributes initialized
// Invokes the constructor
private "_myClassObject";
_myClassObject = ["MyClass"] call BIS_OO_createObject;
 
// Invoking a method and getting an attribute
private ["_return", "_value"];
_return = [player, 1, "Hello"] call BIS_OO_MyClass_myMethod;
_value = _myClassObject getVariable "myAttribute";