Scripting: Do's and Don'ts – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (1 revision imported) |
Bovine3dom (talk | contribs) m (Fix broken link) |
||
Line 110: | Line 110: | ||
! 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).}} | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
class MainClass | class MainClass |
Revision as of 08:27, 18 May 2022
Don't | Do |
---|---|
class ExampleClass {
int LProcLgh;
int stringlength( string Value ){
LProcLgh= Value.Length();
Print ( "String length obtained: "+LProcLgh );
return LProcLgh;
}
}
|
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;
}
}
|
Keep variables as close as possible to their usage:
Don't | Do |
---|---|
class ExampleClass
{
protected int m_iLength;
int GetStringLength(string name)
{
m_iLength = name.Length();
Print("String length obtained: " + m_iLength);
return m_iLength;
}
}
|
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;
}
}
|
Keep a strong reference (ref keyword) to required objects:
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
}
|
Avoid strong reference cyclic trap:
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);
}
}
|