Object Oriented scripting shell: Difference between revisions
(Describing the OO scripting shell used in the MP Armory) |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{TOC|side}} | |||
During the implementation phase of the [[ | {{Feature|arma2oa|This documentation applies to {{arma2oa}} '''only'''.}} | ||
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: | Classes, their attributes and methods are defined in configs: | ||
<syntaxhighlight lang="cpp"> | |||
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) | |||
}; | |||
}; | |||
}; | |||
}; | |||
</syntaxhighlight> | |||
=== Supported types === | |||
These are the [[:Category:Data Types|Data Types]] supported by the OO shell: | |||
{{Columns|4| | |||
* "SCALAR" | |||
* "STRING" | |||
* "BOOL" | |||
* "ARRAY" | |||
* "OBJECT" | |||
* "CONFIG" | |||
* "GROUP" | |||
* "TASK" | |||
}} | |||
== 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>''. | ||
<sqf> | |||
// Assuming the function was loaded into BIS_OO_addClass | |||
"className" call BIS_OO_addClass; | |||
</sqf> | |||
=== 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. | ||
<sqf> | |||
// The function is automatically loaded into BIS_OO_createObject after adding the first class | |||
// classname: string | |||
// parameter: anything (Optional) | |||
// returns an object | |||
_createdObject = ["className", parameter] call BIS_OO_createObject; | |||
</sqf> | |||
== Example == | |||
Config: | Config: | ||
<syntaxhighlight lang="cpp"> | |||
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"; | |||
}; | |||
}; | |||
}; | |||
}; | |||
</syntaxhighlight> | |||
Script: | Script: | ||
<sqf> | |||
// 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"; | |||
</sqf> | |||
{{GameCategory|arma2oa}} |
Latest revision as of 15:44, 10 January 2023
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)
};
};
};
};
Supported types
These are the Data Types supported by the OO shell:
- "SCALAR"
- "STRING"
- "BOOL"
- "ARRAY"
- "OBJECT"
- "CONFIG"
- "GROUP"
- "TASK"
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>.
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.
Example
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: