Object Oriented Programming Advanced Usage – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "\{\{GameCategory\|armaR\|Modding\|(Guidelines|Tutorials)\|([^=↵]*)\}\}" to "{{GameCategory|armaR|Modding|$2|$1}}") |
Lou Montana (talk | contribs) (Add modding order example) |
||
Line 100: | Line 100: | ||
A mod can inherit/replace an existing class with the use of the {{hl|modded}} keyword.<br> | A mod can inherit/replace an existing class with the use of the {{hl|modded}} keyword.<br> | ||
It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding). | It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding). | ||
A modded class behaves like a class inherited from the original class (one can use {{hl| | A modded class behaves like a class inherited from the original class (one can use {{hl|{{Link|Arma Reforger:Object Oriented Programming Basics#super|super}}}} to access the original class) but also allows '''{{hl|private}}''' methods and functions access and modification. | ||
When a modded class is declared, the modded class will be instanced instead of the original class. | When a modded class is declared, the modded class will be instanced instead of the original class. | ||
{{Feature|important|Only classes within the same module can be modded (to mod a class in e.g {{hl|GameLib}} module, the modded class has to be placed in the {{hl|GameLib}} module).}} | {{Feature|important|Only classes within the same module can be modded (to mod a class in e.g {{hl|GameLib}} module, the modded class has to be placed in the {{hl|GameLib}} module).}} | ||
Line 140: | Line 140: | ||
} | } | ||
</enforce> | </enforce> | ||
=== Precedence === | |||
Multiple mods can edit the same class if loaded together. In such case, vanilla class gets modded in order by mod1, mod2, mod3. | |||
Mod loading order is defined by dependency (if mod2 requires mod1, mod1 is loaded first) - if no dependencies are involved, order is not guaranteed. | |||
{| class="valign-top" style="width: 100%" | |||
! Vanilla | |||
! Mod 1 | |||
! Mod 2 | |||
|- | |||
| | |||
<enforce> | |||
class SCR_ExampleClass | |||
{ | |||
string GetMessage() | |||
{ | |||
return "Vanilla message"; | |||
} | |||
} | |||
</enforce> | |||
| | |||
<enforce> | |||
modded class SCR_ExampleClass | |||
{ | |||
override string GetMessage() | |||
{ | |||
return super.GetMessage() + ", Mod A"; | |||
} | |||
} | |||
</enforce> | |||
| | |||
<enforce> | |||
modded class SCR_ExampleClass | |||
{ | |||
override string GetMessage() | |||
{ | |||
return super.GetMessage() + ", Mod B"; | |||
} | |||
} | |||
</enforce> | |||
|} | |||
{| style="width: 100%" | |||
| If Mod B requires Mod A, the load order will be Vanilla > Mod A > Mod B. | |||
| <enforce>instance.GetMessage(); // Vanilla message, Mod A, Mod B</enforce> | |||
|- | |||
| If Mod A requires Mod B, the load order will be Vanilla > Mod B > Mod A (obviously). | |||
| <enforce>instance.GetMessage(); // Vanilla message, Mod B, Mod A</enforce> | |||
|- | |||
| If no dependencies are defined between mods, the load order is arbitrary. | |||
| <enforce> | |||
// can be one of | |||
instance.GetMessage(); // Vanilla message, Mod A, Mod B | |||
// or | |||
instance.GetMessage(); // Vanilla message, Mod B, Mod A | |||
</enforce> | |||
|} | |||
{{GameCategory|armaR|Modding|Scripting|Guidelines}} | {{GameCategory|armaR|Modding|Scripting|Guidelines}} |
Latest revision as of 10:23, 11 April 2025
Casting
Casting is the act of "presenting" a value as another type. For example, if a class hierarchy is Animal > Dog > Cocker, a dog is an animal, a cocker is a dog (that is an animal), but a dog is not especially a cocker.
Upcasting
Upcasting means seeing the class as one of its parents:
Downcasting
Downcasting means seeing a parent class as a specific child - this must be done by manually casting:
Manual Casting
Template
A template is a class that allows a generic management for multiple types. Its methods cannot assume anything about the type.
Modding
A mod can inherit/replace an existing class with the use of the modded keyword.
It is used to inject inherited class into class hierarchy without modifying other scripts (especially suitable in modding).
A modded class behaves like a class inherited from the original class (one can use super to access the original class) but also allows private methods and functions access and modification.
When a modded class is declared, the modded class will be instanced instead of the original class.
Precedence
Multiple mods can edit the same class if loaded together. In such case, vanilla class gets modded in order by mod1, mod2, mod3. Mod loading order is defined by dependency (if mod2 requires mod1, mod1 is loaded first) - if no dependencies are involved, order is not guaranteed.
Vanilla | Mod 1 | Mod 2 |
---|---|---|
|
modded class SCR_ExampleClass
{
override string GetMessage()
{
return super.GetMessage() + ", Mod A";
}
} |
modded class SCR_ExampleClass
{
override string GetMessage()
{
return super.GetMessage() + ", Mod B";
}
} |
If Mod B requires Mod A, the load order will be Vanilla > Mod A > Mod B. | instance.GetMessage(); // Vanilla message, Mod A, Mod B |
If Mod A requires Mod B, the load order will be Vanilla > Mod B > Mod A (obviously). | instance.GetMessage(); // Vanilla message, Mod B, Mod A |
If no dependencies are defined between mods, the load order is arbitrary. | // can be one of
instance.GetMessage(); // Vanilla message, Mod A, Mod B
// or
instance.GetMessage(); // Vanilla message, Mod B, Mod A |