Scripting: Conventions – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Remove after class' semicolons) |
Lou Montana (talk | contribs) m (Entity → IEntity) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
== Tag == | == Tag == | ||
To prevent conflict with other scripts all classes, and global functions must be distinguished by a [[ | To prevent conflict with other scripts all classes, and global functions must be distinguished by a [[Scripting Tags|Tag]]. | ||
{{Name|bi}} scripts have been prefixed with {{hl|SCR_}}. A mod developer must choose their own tag and not use one already taken by {{Name|bi}} or another developer. | {{Name|bi}} scripts have been prefixed with {{hl|SCR_}}. A mod developer must choose their own tag and not use one already taken by {{Name|bi}} or another developer. | ||
When {{Link|Arma Reforger:Scripting Modding|modding}} any class, all methods and variables within that modded class must also be distinguished by the same unique tag to prevent conflict. | |||
== File/Class == | == File/Class == | ||
Line 42: | Line 43: | ||
{{Feature|informative|See {{Link|Arma Reforger:Scripting: Values#Identifier|this page}} for more information.}} | {{Feature|informative|See {{Link|Arma Reforger:Scripting: Values#Identifier|this page}} for more information.}} | ||
* '''Member''' variables are prefixed with {{hl|m_}}, a one-letter type prefix for specific types (see {{Link|Arma Reforger:Scripting: Values#Types|Value types}}), and use '''PascalCase''', e.g:<enforce> | * '''Member''' variables are prefixed with {{hl|m_}}, a one-letter type prefix for specific types (see {{Link|Arma Reforger:Scripting: Values#Types|Value types}}), and use '''PascalCase''', e.g:<enforce> | ||
IEntity m_Entity; | |||
int m_iHealth; | int m_iHealth; | ||
bool m_bIsEnemy; | bool m_bIsEnemy; | ||
</enforce> | </enforce> | ||
* '''Global''' variables are prefixed with {{hl|g_}}. {{Feature|important|Global variables are bad practice and must not be used | * '''Global''' variables are prefixed with {{hl|g_}}. {{Feature|important|Global variables are bad practice and must not be used outside of absolute necessity!}} | ||
* '''Static''' variables are prefixed with {{hl|s_}} ('''constants''' are not), eventually a one-letter type prefix, and use '''PascalCase''', e.g<enforce> | * '''Static''' variables are prefixed with {{hl|s_}} ('''constants''' are not), eventually a one-letter type prefix, and use '''PascalCase''', e.g<enforce> | ||
protected static int s_iUnitsCount; | protected static int s_iUnitsCount; | ||
Line 60: | Line 61: | ||
</enforce> | </enforce> | ||
* '''Constant''' values use '''all capital letters''' with words separated by '''underscores''' (uppercase snake casing), e.g:<enforce> | * '''Constant''' values use '''all capital letters''' with words separated by '''underscores''' (uppercase snake casing), e.g:<enforce> | ||
const int MAX_VALUE = 9999; // no 'i' type prefix | const int MAX_VALUE = 9999; // no 'm_' prefix nor 'i' type prefix | ||
static const int TOTAL = 10; // | static const int TOTAL = 10; // no 's_' prefix either | ||
</enforce> | |||
=== Order === | |||
* First go '''Attributes''' (if any), | |||
* then public, protected, private '''member variables''' | |||
* then public, protected, private '''static variables''' | |||
* then public, protected, private '''constants''' | |||
<enforce> | |||
[Attribute()] | |||
protected int m_iAttribute; | |||
int m_iPublic; | |||
protected int m_iProtected; | |||
private int m_iPrivate; | |||
static int s_iPublic; | |||
protected static int s_iProtected; | |||
private static int s_iPrivate; | |||
static const int PUBLIC; | |||
protected static const int PROTECTED; | |||
private static const int PRIVATE; | |||
</enforce> | </enforce> | ||
== Script == | == Script == | ||
Line 74: | Line 100: | ||
class SCR_HumanComponent : ScriptComponent | class SCR_HumanComponent : ScriptComponent | ||
{ | { | ||
protected int m_iAge; | |||
void SetAge(int age) | void SetAge(int age) | ||
Line 123: | Line 149: | ||
while (true) | while (true) | ||
{ | { | ||
break; // in case of a hasty copy-paste :) | |||
} | } | ||
} | } | ||
Line 132: | Line 159: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
</enforce> | </enforce> | ||
* '''Documentation''' must be done with | * '''Documentation''' must be done with {{Link|Doxygen}} support in mind, using the {{hl|//!}} comment syntax (see {{Link|#Example}}) | ||
* Methods should be '''sorted''' in the following order (top to bottom): | * Methods should be '''sorted''' in the following order (top to bottom): | ||
** General methods | ** General methods | ||
Line 140: | Line 167: | ||
** Destructor | ** Destructor | ||
<enforce> | <enforce> | ||
//! A scripted entity | |||
class SCR_ScriptedEntity : GenericEntity | class SCR_ScriptedEntity : GenericEntity | ||
{ | { | ||
Line 148: | Line 175: | ||
//! \param vectorB Second position, direction goal | //! \param vectorB Second position, direction goal | ||
//! \return The direction from A to B as a normalized vector | //! \return The direction from A to B as a normalized vector | ||
protected vector GetNormalizedDirection(vector vectorA, vector vectorB) | |||
{ | { | ||
vector dir = vectorB - vectorA; | vector dir = vectorB - vectorA; | ||
Line 170: | Line 197: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
// | // constructor | ||
void SCR_ScriptedEntity(IEntitySource src, IEntity parent) | void SCR_ScriptedEntity(IEntitySource src, IEntity parent) | ||
{ | { | ||
SetEventMask(EntityEvent.INIT | EntityEvent.FRAME | EntityEvent.CONTACT); | SetEventMask(EntityEvent.INIT | EntityEvent.FRAME | EntityEvent.CONTACT); | ||
} | } | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
// | // destructor | ||
void ~SCR_ScriptedEntity() | void ~SCR_ScriptedEntity() | ||
{ | { | ||
Line 192: | Line 218: | ||
SCR_Class myClass = new SCR_Class; // wrong | SCR_Class myClass = new SCR_Class; // wrong | ||
</enforce> | </enforce> | ||
* | * arrays can be initialised directly:<enforce> | ||
array<string> myArray = new array<string>(); // | array<string> myArray = {}; // correct | ||
array<string> myArray = new array<string>; | array<string> myArray = new array<string>(); // tolerable | ||
array<string> myArray = new array<string>; // wrong | |||
</enforce> | </enforce> | ||
== | == Moddability == | ||
It is important to keep moddability in mind when scripting to ensure. | |||
Modded classes work very similar to inherited ones and come with the same restrictions: | |||
Modded classes work very similar to inherited ones | * '''Constructor/Destructor''': a modded class has its own const/destructor and cannot modify the parent one | ||
* '''Constructor/Destructor''': | * '''Private variables & methods''': a modded class cannot override parent's private members - use <enforce inline>protected</enforce> instead of <enforce inline>private</enforce> | ||
* '''Private variables & methods''': | * '''Static variables & methods''': same as above - do not use unless absolutely necessary | ||
* '''Static variables & methods''': | * '''Global methods''': no classes to mod - do not use unless absolutely necessary. | ||
* '''Global methods''': | |||
Line 213: | Line 238: | ||
<enforce> | <enforce> | ||
[ | [EntityEditorProps("GameScripted/SomeFolder", "Description of this component", "255 0 0 255", true, true, "", "box", "-0.25 -0.25 -0.25", "0.25 0.25 0.25", "0 0 0 0")] | ||
class SCR_SomeComponentClass | class SCR_SomeComponentClass | ||
{ | { | ||
Line 225: | Line 250: | ||
MESH = 1, | MESH = 1, | ||
BODY = 2, | BODY = 2, | ||
HIERARCHY = 4 | HIERARCHY = 4, | ||
NET = 8 | NET = 8, | ||
} | } | ||
Line 234: | Line 259: | ||
class SCR_SomeComponent : ScriptComponent | class SCR_SomeComponent : ScriptComponent | ||
{ | { | ||
//! Defines the maximum distance at which this object will be rendered in metres. | //! Defines the maximum distance at which this object will be rendered in metres. | ||
[Attribute("30.0", UIWidgets.Slider, "The maximum distance at which this object will be rendered in metres.", "0 120 0.1")] | [Attribute("30.0", UIWidgets.Slider, "The maximum distance at which this object will be rendered in metres.", "0 120 0.1")] | ||
protected float m_fRenderDistance; | |||
//! Maximum count of children that can be spawned at any time. If the limit is exceeded no more children are spawned. | //! Maximum count of children that can be spawned at any time. If the limit is exceeded no more children are spawned. | ||
[Attribute("100 | [Attribute("100", desc: "Maximum count of children that can be spawned at any time. If the limit is exceeded no more children are spawned.", "0 500")] | ||
protected int m_iMaximumChildCount; | |||
//! The offset of this object in metres. | //! The offset of this object in metres. | ||
protected vector m_vPositionOffset = "0 0 0"; | |||
//! A public variable | //! A public variable | ||
Line 254: | Line 275: | ||
//! A public vector | //! A public vector | ||
vector m_vOtherPublic = "1 2 3"; | vector m_vOtherPublic = "1 2 3"; | ||
//! Defines the minimum distance (in metres) for this object to render. If below this value, object will be culled. | |||
const float RENDER_DISTANCE_MINIMUM = 10; | |||
//! Defines the maximum distance (in metres) for this object to render. If above this value, object will be culled. | |||
const float RENDER_DISTANCE_MAXIMUM = 100; | |||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 264: | Line 291: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
//! Set the render distance of this object. | //! Set the render distance of this object. | ||
//! \param renderDistance | //! \param renderDistance distance in metres. Is clamped between RENDER_DISTANCE_MINIMUM and RENDER_DISTANCE_MAXIMUM. | ||
void SetRenderDistance(float renderDistance) | void SetRenderDistance(float renderDistance) | ||
{ | { | ||
Line 271: | Line 298: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
//! Prints hello to the debug console. | //! Prints hello to the debug console. Protected method, not exposed to outside classes but available to child classes. | ||
protected void SayHello() | |||
{ | { | ||
string localString = "Hello!"; | string localString = "Hello!"; | ||
Line 280: | Line 307: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
//! Compare two integers, return the larger one. (Don't mind the silly documentation, mind the syntax) | //! Compare two integers, return the larger one. (Don't mind the silly documentation, mind the syntax) | ||
//! \param a | //! \param a first parameter to be compared with the second one | ||
//! \param b | //! \param b second parameter to be compared with the first one | ||
//! \return | //! \return true if a is equal to b, false otherwise. | ||
bool | bool AreEqual(int a, int b) | ||
{ | { | ||
// can be simplified to "return a == b;" | // can be simplified to "return a == b;" | ||
Line 299: | Line 326: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
// constructor - remove if empty | |||
void SCR_SomeComponent(IEntityComponentSource src, IEntity ent, IEntity parent) | void SCR_SomeComponent(IEntityComponentSource src, IEntity ent, IEntity parent) | ||
{ | { | ||
Line 315: | Line 343: | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
// destructor - remove if left empty | |||
void ~SCR_SomeComponent() | void ~SCR_SomeComponent() | ||
{ | { | ||
// ... | |||
} | } | ||
} | } |
Latest revision as of 11:42, 22 October 2024
Tag
To prevent conflict with other scripts all classes, and global functions must be distinguished by a Tag.
Bohemia Interactive scripts have been prefixed with SCR_. A mod developer must choose their own tag and not use one already taken by Bohemia Interactive or another developer.
When modding any class, all methods and variables within that modded class must also be distinguished by the same unique tag to prevent conflict.
File/Class
- File should be called TAG_MyObject.c
- A component must end with "component": TAG_ExampleComponent.c
- An entity must end with "entity': TAG_ExampleEntity.c
- Class should be called the same (without file extension):
- class TAG_MyObject
- class TAG_ExampleComponent
- class TAG_ExampleEntity
- Enum:
- name must be prefixed with a capital E, e.g TAG_EMyEnum
- values use all capital letters with words separated by underscores, e.g TAG_EMyEnum.VALUE_1
- The file should be located in the scripts
\Game directory - The use of plural is prohibited at the end of combined keywords: e.g TAG_NotificationsComponent → TAG_NotificationComponent
Method
- Functions/Methods naming uses PascalCase, e.g:
- Parameters are named with camelCase, e.g:
Variable
- Member variables are prefixed with m_, a one-letter type prefix for specific types (see Value types), and use PascalCase, e.g:
- Global variables are prefixed with g_.
- Static variables are prefixed with s_ (constants are not), eventually a one-letter type prefix, and use PascalCase, e.g
- Local variables and arguments (method parameters) use camelCase, e.g:
- Constant values use all capital letters with words separated by underscores (uppercase snake casing), e.g:
Order
- First go Attributes (if any),
- then public, protected, private member variables
- then public, protected, private static variables
- then public, protected, private constants
Script
General
- Curly braces must always be on a new line - Enforce Script uses Allman style
- Variables and functions should be protected whenever possible (respecting OOP black box principle) unless they are intended to be exposed
- Getters/Setters: variables should be made protected and accessed through getters and setters (entry methods getting/setting the value)
class SCR_HumanComponent : ScriptComponent
{
protected int m_iAge;
void SetAge(int age)
{
m_iAge = age;
PrintFormat("Age of instance %1 is now %2", this, m_iAge);
}
int GetAge()
{
return m_iAge;
}
}
Spacing
- Tabs are used for indentation - they are set to a size of 4 spaces in Script Editor
- A space is used before and after:
- a binary operator
- a foreach colon
- a class' inheritance colon
- A space is used after:
- if, for, foreach, switch, while keywords
- a for semicolon
- Spaces are used inside parentheses but not around their content
class SCR_HumanComponent : ScriptComponent
{
if (true)
{
}
for (int i = 0; i < 10; i++)
{
}
foreach (string item : stringArray)
{
}
switch (value)
{
case 42:
break;
}
while (true)
{
break; // in case of a hasty copy-paste :)
}
}
Method
- All methods must be separated using this sequence of characters: two slashes followed by 96 dashes (see Example)//------------------------------------------------------------------------------------------------
- Documentation must be done with Doxygen support in mind, using the
/ /! comment syntax (see Example) - Methods should be sorted in the following order (top to bottom):
- General methods
- EOnFrame
- EOnInit
- Constructor
- Destructor
//! A scripted entity
class SCR_ScriptedEntity : GenericEntity
{
//------------------------------------------------------------------------------------------------
//! Get the normalized direction vector at position A pointing to B
//! \param vectorA First position, direction origin
//! \param vectorB Second position, direction goal
//! \return The direction from A to B as a normalized vector
protected vector GetNormalizedDirection(vector vectorA, vector vectorB)
{
vector dir = vectorB - vectorA;
return dir.Normalized();
}
//------------------------------------------------------------------------------------------------
//! Frame
override void EOnFrame(IEntity owner, float timeSlice)
{
vector direction = GetNormalizedDirection(owner.GetOrigin(), vector.Zero);
Print("OnFrame was called! Direction: " + direction);
}
//------------------------------------------------------------------------------------------------
//! Init
override void EOnInit(IEntity owner)
{
Print("Init was called!");
}
//------------------------------------------------------------------------------------------------
// constructor
void SCR_ScriptedEntity(IEntitySource src, IEntity parent)
{
SetEventMask(EntityEvent.INIT | EntityEvent.FRAME | EntityEvent.CONTACT);
}
//------------------------------------------------------------------------------------------------
// destructor
void ~SCR_ScriptedEntity()
{
Print("Destructing SCR_ScriptedEntity");
}
}
Miscellaneous
- class instanciation with the new keyword must use parentheses:SCR_Class myClass = new SCR_Class(); // correct SCR_Class myClass = new SCR_Class; // wrong
- arrays can be initialised directly:
Moddability
It is important to keep moddability in mind when scripting to ensure. Modded classes work very similar to inherited ones and come with the same restrictions:
- Constructor/Destructor: a modded class has its own const/destructor and cannot modify the parent one
- Private variables & methods: a modded class cannot override parent's private members - use protected instead of private
- Static variables & methods: same as above - do not use unless absolutely necessary
- Global methods: no classes to mod - do not use unless absolutely necessary.
Example
[EntityEditorProps("GameScripted/SomeFolder", "Description of this component", "255 0 0 255", true, true, "", "box", "-0.25 -0.25 -0.25", "0.25 0.25 0.25", "0 0 0 0")]
class SCR_SomeComponentClass
{
}
SCR_SomeComponentClass SCR_SomeComponentSource;
//! Flags used for an entity to define its currently active components.
enum SomeFlags
{
MESH = 1,
BODY = 2,
HIERARCHY = 4,
NET = 8,
}
//! A brief explanation of what this component does.
//! The explanation can be spread across multiple lines.
//! This should help with quickly understanding the script's purpose.
class SCR_SomeComponent : ScriptComponent
{
//! Defines the maximum distance at which this object will be rendered in metres.
[Attribute("30.0", UIWidgets.Slider, "The maximum distance at which this object will be rendered in metres.", "0 120 0.1")]
protected float m_fRenderDistance;
//! Maximum count of children that can be spawned at any time. If the limit is exceeded no more children are spawned.
[Attribute("100", desc: "Maximum count of children that can be spawned at any time. If the limit is exceeded no more children are spawned.", "0 500")]
protected int m_iMaximumChildCount;
//! The offset of this object in metres.
protected vector m_vPositionOffset = "0 0 0";
//! A public variable
float m_fSomethingPublic = 3.2;
//! A public vector
vector m_vOtherPublic = "1 2 3";
//! Defines the minimum distance (in metres) for this object to render. If below this value, object will be culled.
const float RENDER_DISTANCE_MINIMUM = 10;
//! Defines the maximum distance (in metres) for this object to render. If above this value, object will be culled.
const float RENDER_DISTANCE_MAXIMUM = 100;
//------------------------------------------------------------------------------------------------
//! Returns the render distance of this object (metres).
float GetRenderDistance()
{
return m_fRenderDistance;
}
//------------------------------------------------------------------------------------------------
//! Set the render distance of this object.
//! \param renderDistance distance in metres. Is clamped between RENDER_DISTANCE_MINIMUM and RENDER_DISTANCE_MAXIMUM.
void SetRenderDistance(float renderDistance)
{
m_fRenderDistance = Math.Clamp(renderDistance, RENDER_DISTANCE_MINIMUM, RENDER_DISTANCE_MAXIMUM);
}
//------------------------------------------------------------------------------------------------
//! Prints hello to the debug console. Protected method, not exposed to outside classes but available to child classes.
protected void SayHello()
{
string localString = "Hello!";
Print(localString);
}
//------------------------------------------------------------------------------------------------
//! Compare two integers, return the larger one. (Don't mind the silly documentation, mind the syntax)
//! \param a first parameter to be compared with the second one
//! \param b second parameter to be compared with the first one
//! \return true if a is equal to b, false otherwise.
bool AreEqual(int a, int b)
{
// can be simplified to "return a == b;"
if (a == b)
return true;
else
return false;
}
//------------------------------------------------------------------------------------------------
override void EOnInit(IEntity owner)
{
Print("Initialized some component!");
}
//------------------------------------------------------------------------------------------------
// constructor - remove if empty
void SCR_SomeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
{
ent.SetEventMask(EntityEvent.INIT);
// If offset is 0, no need to update
if (m_vPositionOffset != "0 0 0")
{
// Get current transformation matrix, add the position offset and update transformation.
vector mat[4];
ent.GetTransform(mat);
mat[3] = mat[3] + m_vPositionOffset;
ent.SetTransform(mat);
}
}
//------------------------------------------------------------------------------------------------
// destructor - remove if left empty
void ~SCR_SomeComponent()
{
// ...
}
}