Scripting: Do's and Don'ts – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<syntaxhighlight lang="c#">" to "<enforce>") |
Lou Montana (talk | contribs) m (Text replacement - "</syntaxhighlight>" to "</enforce>") |
||
Line 16: | Line 16: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
<enforce> | <enforce> | ||
Line 35: | Line 35: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
Line 56: | Line 56: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
<enforce> | <enforce> | ||
Line 70: | Line 70: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
Line 90: | Line 90: | ||
// as there are no references to it | // as there are no references to it | ||
} | } | ||
</ | </enforce> | ||
| | | | ||
<enforce> | <enforce> | ||
Line 102: | Line 102: | ||
// 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> | ||
|} | |} | ||
Line 136: | Line 136: | ||
} | } | ||
} | } | ||
</ | </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> | <enforce> | ||
Line 167: | Line 167: | ||
} | } | ||
} | } | ||
</ | </enforce> | ||
|} | |} | ||
Line 182: | Line 182: | ||
<enforce> | <enforce> | ||
</ | </enforce> | ||
| | | | ||
<enforce> | <enforce> | ||
</ | </enforce> | ||
|} | |} | ||
--> | --> |
Revision as of 19:18, 30 July 2022
Don't | Do |
---|---|
Keep variables as close as possible to their usage:
Don't | Do |
---|---|
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);
}
} |