Scripting: Do's and Don'ts – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Fix <tt> usage)
m (Text replacement - "<syntaxhighlight lang="c#">" to "<enforce>")
Line 6: Line 6:
|- style="vertical-align: top"
|- style="vertical-align: top"
|
|
<syntaxhighlight lang="c#">
<enforce>
class ExampleClass {
class ExampleClass {
   int LProcLgh;
   int LProcLgh;
Line 18: Line 18:
</syntaxhighlight>
</syntaxhighlight>
|
|
<syntaxhighlight lang="c#">
<enforce>
class ExampleClass
class ExampleClass
{
{
Line 44: Line 44:
|- style="vertical-align: top"
|- style="vertical-align: top"
|
|
<syntaxhighlight lang="c#">
<enforce>
class ExampleClass
class ExampleClass
{
{
Line 58: Line 58:
</syntaxhighlight>
</syntaxhighlight>
|
|
<syntaxhighlight lang="c#">
<enforce>
class ExampleClass
class ExampleClass
{
{
Line 79: Line 79:
|- style="vertical-align: top"
|- style="vertical-align: top"
|
|
<syntaxhighlight lang="c#">
<enforce>
// this array only lists pointers but does not increase the reference count
// this array only lists pointers but does not increase the reference count
array<ExampleClass> classArray = new array<ExampleClass>();
array<ExampleClass> classArray = new array<ExampleClass>();
Line 92: Line 92:
</syntaxhighlight>
</syntaxhighlight>
|
|
<syntaxhighlight lang="c#">
<enforce>
// this array keeps strong references to its items
// this array keeps strong references to its items
array<ref ExampleClass> classArray = new array<ref ExampleClass>();
array<ref ExampleClass> classArray = new array<ref ExampleClass>();
Line 111: Line 111:
|- style="vertical-align: top"
|- style="vertical-align: top"
| {{Feature|informative|Here, both ParentClass and ChildClass have a strong reference to each other, keeping the reference count above zero - creating an "island of isolation" (see [[Arma Reforger:Scripting: Automatic Reference Counting]] for more information).}}
| {{Feature|informative|Here, both ParentClass and ChildClass have a strong reference to each other, keeping the reference count above zero - creating an "island of isolation" (see [[Arma Reforger:Scripting: Automatic Reference Counting]] for more information).}}
<syntaxhighlight lang="c#">
<enforce>
class MainClass
class MainClass
{
{
Line 138: Line 138:
</syntaxhighlight>
</syntaxhighlight>
| {{Feature|informative|Here, the MainClass needs the SubClass (it creates it in its constructor meaning it needs it to work) but the subClass does not require MainClass - if the MainClass reference doesn't exist, it will simply not use it.}}
| {{Feature|informative|Here, the MainClass needs the SubClass (it creates it in its constructor meaning it needs it to work) but the subClass does not require MainClass - if the MainClass reference doesn't exist, it will simply not use it.}}
<syntaxhighlight lang="c#">
<enforce>
class MainClass
class MainClass
{
{
Line 180: Line 180:
|- style="vertical-align: top"
|- style="vertical-align: top"
|
|
<syntaxhighlight lang="c#">
<enforce>


</syntaxhighlight>
</syntaxhighlight>
|
|
<syntaxhighlight lang="c#">
<enforce>


</syntaxhighlight>
</syntaxhighlight>
|}
|}
-->
-->

Revision as of 20:17, 30 July 2022

Don't Do

<enforce> class ExampleClass {

 int LProcLgh;

int stringlength( string Value ){ LProcLgh= Value.Length(); Print ( "String length obtained: "+LProcLgh ); return LProcLgh; } } </syntaxhighlight>

<enforce> class ExampleClass { protected int m_iLastProcessedLength;

int GetStringLength(string value) { m_iLastProcessedLength = value.Length(); Print("String length obtained: " + m_iLastProcessedLength); return m_iLastProcessedLength; }

int GetLastProcessedLength() { return m_iLastProcessedLength; } } </syntaxhighlight>

Keep variables as close as possible to their usage:

Don't Do

<enforce> class ExampleClass { protected int m_iLength;

int GetStringLength(string name) { m_iLength = name.Length(); Print("String length obtained: " + m_iLength); return m_iLength; } } </syntaxhighlight>

<enforce> class ExampleClass { // keeping the variable into method scope and away from the instance

int GetStringLength(string name) { int length = name.Length(); Print("String length obtained: " + length); return length; } } </syntaxhighlight>

Keep a strong reference (ref keyword) to required objects:

Don't Do

<enforce> // this array only lists pointers but does not increase the reference count array<ExampleClass> classArray = new array<ExampleClass>();

for (int i = 0; i < 10; i++) { ExampleClass newInstance = new ExampleClass(); classArray.Insert(newInstance); // newInstance will be deleted at the end of the scope // as there are no references to it } </syntaxhighlight>

<enforce> // this array keeps strong references to its items array<ref ExampleClass> classArray = new array<ref ExampleClass>();

for (int i = 0; i < 10; i++) { ExampleClass newInstance = new ExampleClass(); classArray.Insert(newInstance); // classArray keeps a strong reference to newInstance - it will not be cleared } </syntaxhighlight>

Avoid strong reference cyclic trap:

Don't Do
Here, both ParentClass and ChildClass have a strong reference to each other, keeping the reference count above zero - creating an "island of isolation" (see Arma Reforger:Scripting: Automatic Reference Counting for more information).

<enforce> class MainClass { ref SubClass m_subClass;

void MainClass() { m_subClass = new SubClass(this); } }

class SubClass { ref MainClass m_parent;

void SubClass(MainClass parent) { m_parent = parent; }

void DoSomething() { Print(m_parent); } } </syntaxhighlight>

Here, the MainClass needs the SubClass (it creates it in its constructor meaning it needs it to work) but the subClass does not require MainClass - if the MainClass reference doesn't exist, it will simply not use it.

<enforce> class MainClass { ref SubClass m_subClass;

void MainClass() { m_subClass = new SubClass(this); } }

class SubClass { MainClass m_parent; // ref removed

void SubClass(MainClass parent) { m_parent = parent; }

void DoSomething() { if (!m_parent) // null safety check { return; } Print(m_parent); } } </syntaxhighlight>