Scripting: Do's and Don'ts – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (1 revision imported) |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{| class="wikitable" style="width: 100%" | {| class="wikitable" style="width: 100%" | ||
|+ {{Feature|important|Do follow {{Link|Arma Reforger:Scripting: Conventions|Scripting Conventions}}.}} | |||
! style="width: 50%" | Don't | ! style="width: 50%" | Don't | ||
! style="width: 50%" | Do | ! style="width: 50%" | Do | ||
|- style="vertical-align: top" | |- style="vertical-align: top" | ||
| | | | ||
< | <enforce noGuess> | ||
class ExampleClass { | class ExampleClass { | ||
int LProcLgh; | int LProcLgh; | ||
Line 16: | Line 15: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
< | <enforce> | ||
class ExampleClass | class ExampleClass | ||
{ | { | ||
Line 35: | Line 34: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
{| class="wikitable" style="width: 100%" | {| class="wikitable" style="width: 100%" | ||
|+ {{Feature|important|Keep variables as close as possible to their usage.}} | |||
! style="width: 50%" | Don't | ! style="width: 50%" | Don't | ||
! style="width: 50%" | Do | ! style="width: 50%" | Do | ||
|- style="vertical-align: top" | |- style="vertical-align: top" | ||
| | | | ||
< | <enforce> | ||
class ExampleClass | class ExampleClass | ||
{ | { | ||
Line 56: | Line 55: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
< | <enforce> | ||
class ExampleClass | class ExampleClass | ||
{ | { | ||
Line 70: | Line 69: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
{| class="wikitable" style="width: 100%" | {| class="wikitable" style="width: 100%" | ||
|+ {{Feature|important|Keep a strong reference (<enforce inline>ref</enforce> keyword) to required objects.}} | |||
! style="width: 50%" | Don't | ! style="width: 50%" | Don't | ||
! style="width: 50%" | Do | ! style="width: 50%" | Do | ||
|- style="vertical-align: top" | |- style="vertical-align: top" | ||
| | | | ||
< | <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 90: | Line 89: | ||
// as there are no references to it | // as there are no references to it | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
< | <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 102: | Line 101: | ||
// classArray keeps a strong reference to newInstance - it will not be cleared | // classArray keeps a strong reference to newInstance - it will not be cleared | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
{| class="wikitable" style="width: 100%" | {| class="wikitable" style="width: 100%" | ||
|+ {{Feature|important|Avoid strong reference cyclic trap.}} | |||
! style="width: 50%" | Don't | ! style="width: 50%" | Don't | ||
! style="width: 50%" | Do | ! style="width: 50%" | Do | ||
|- 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 [[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).}} | ||
< | <enforce> | ||
class MainClass | class MainClass | ||
{ | { | ||
ref SubClass | ref SubClass m_SubClass; | ||
void MainClass() | void MainClass() | ||
{ | { | ||
m_SubClass = new SubClass(this); | |||
} | } | ||
} | } | ||
Line 124: | Line 123: | ||
class SubClass | class SubClass | ||
{ | { | ||
ref MainClass | ref MainClass m_Parent; | ||
void SubClass(MainClass parent) | void SubClass(MainClass parent) | ||
{ | { | ||
m_Parent = parent; | |||
} | } | ||
void DoSomething() | void DoSomething() | ||
{ | { | ||
Print( | Print(m_Parent); | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
| {{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.}} | ||
< | <enforce> | ||
class MainClass | class MainClass | ||
{ | { | ||
ref SubClass | ref SubClass m_SubClass; | ||
void MainClass() | void MainClass() | ||
{ | { | ||
m_SubClass = new SubClass(this); | |||
} | } | ||
} | } | ||
Line 151: | Line 150: | ||
class SubClass | class SubClass | ||
{ | { | ||
MainClass | MainClass m_Parent; // ref removed | ||
void SubClass(MainClass parent) | void SubClass(MainClass parent) | ||
{ | { | ||
m_Parent = parent; | |||
} | } | ||
void DoSomething() | void DoSomething() | ||
{ | { | ||
if (! | if (!m_Parent) // null safety check | ||
return; | return; | ||
Print( | Print(m_Parent); | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
Line 180: | Line 178: | ||
|- style="vertical-align: top" | |- style="vertical-align: top" | ||
| | | | ||
< | <enforce> | ||
</ | </enforce> | ||
| | | | ||
< | <enforce> | ||
</ | </enforce> | ||
|} | |} | ||
--> | --> |
Revision as of 02:11, 17 June 2023
Don't | Do |
---|---|
Don't | Do |
---|---|
Don't | Do |
---|---|
// 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
} |
// 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
} |
Don't | Do |
---|---|
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);
}
} |
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);
}
} |