Scripting Modding – Arma Reforger
No edit summary |
Lou Montana (talk | contribs) m (Text replacement - "[[OFPEC_tags" to "[[Scripting Tags") |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 45: | Line 45: | ||
Since we want to modify the scoring system, we can begin by searching for terms related to it. | Since we want to modify the scoring system, we can begin by searching for terms related to it. | ||
By typing {{hl|scoring}} into the '''Find Symbol''' search field, we should see '''SCR_ScoringSystemComponent.c''' on the first place. | By typing {{hl|scoring}} into the '''Find Symbol''' search field, we should see '''SCR_ScoringSystemComponent.c''' on the first place. | ||
Double clicking {{Controls|LMB2}} opens the file containing that class and reveals its location in the file structure. | Double clicking {{Controls|LMB2}} opens the file containing that class and reveals its location in the file structure. | ||
Next to it is '''SCR_BaseScoringSystemComponent.c''' and those two files should be enough to achieve the goals stated above. | Next to it is '''SCR_BaseScoringSystemComponent.c''' and those two files should be enough to achieve the goals stated above. | ||
Line 68: | Line 71: | ||
Once the directory is prepared, create two new script files inside the '''Modded''' folder. | Once the directory is prepared, create two new script files inside the '''Modded''' folder. | ||
To do so, right-click on the Resource Browser field to open the context menu. | To do so, right-click on the Resource Browser field to open the context menu. | ||
From there, select "Script" to create a new script file. From here, it is time to create actual code! | From there, select "Script" to create a new script file. From here, it is time to create actual code! | ||
# Create a new Script File: In Resources Manager, click the {{hl|Create}} button then "Script" to create a script file | # Create a new Script File: In Resources Manager, click the {{hl|Create}} button then "Script" to create a script file | ||
# Name the new Script File: the files should have the same name as the modded ones; namely {{hl|SCR_BaseScoringSystemComponent.c}} and {{hl|SCR_ScoringSystemComponent.c}} | # Name the new Script File: the files should have the same name as the modded ones; namely {{hl|SCR_BaseScoringSystemComponent.c}} and {{hl|SCR_ScoringSystemComponent.c}} | ||
{{Feature|informative|It is also recommended to replace '''SCR_ prefix with your own [[Scripting Tags|tag]]''' - this will ensure good inter-compatibility with other scripts by preventing naming conflicts. | |||
{{Clear}} | For instance, instead of naming file ''SCR_ScoringSystemComponent.c'' you would call it ''YOURTAG_ScoringSystemComponent.c''}}{{Clear}} | ||
It is usually a good habit to keep the original script and file structures; | It is usually a good habit to keep the original script and file structures; | ||
Line 164: | Line 171: | ||
Same as with '''SCR_ScoringSystemComponent''', we will use the '''modded''' keyword on '''SCR_BaseScoringSystemComponent''' to modify the content of that class. | Same as with '''SCR_ScoringSystemComponent''', we will use the '''modded''' keyword on '''SCR_BaseScoringSystemComponent''' to modify the content of that class. | ||
The '''AddSuicide''' method is called every time the player commits suicide and adds score according to previously defined modifiers. | The '''AddSuicide''' method is called every time the player commits suicide and adds score according to previously defined modifiers. | ||
Since we don't want to change that part and instead want to add a new behaviour to it, we will use the '''super''' keyword to call the overridden method's code. | Since we don't want to change that part and instead want to add a new behaviour to it, we will use the '''super''' keyword to call the overridden method's code. | ||
Line 203: | Line 212: | ||
=== Terrain Preparation === | === Terrain Preparation === | ||
{{Feature|informative|Below prefabs are using '''Enfusion link''' which directly loads Workbench and points you to a proper resource. This functionality has to be '''manually enabled''' in '''Resource Manager''' options though! For more details see [[Arma_Reforger:Resource_Manager:_Options#Register_.22enfusion:.2F.2F.22_protocol|'''Resource Manager: Options page''']].}} | {{Feature|informative|Below prefabs are using '''Enfusion link''' which directly loads Workbench and points you to a proper resource. | ||
This functionality has to be '''manually enabled''' in '''Resource Manager''' options though! | |||
For more details see [[Arma_Reforger:Resource_Manager:_Options#Register_.22enfusion:.2F.2F.22_protocol|'''Resource Manager: Options page''']].}} | |||
[[Image:armareforger-scripting-modding-hierarchy-prefabs.png|left|600px|thumb|Prefabs in World Editor [[Arma_Reforger:World_Editor#Hierarchy|Hierarchy tab]]]] | [[Image:armareforger-scripting-modding-hierarchy-prefabs.png|left|600px|thumb|Prefabs in World Editor [[Arma_Reforger:World_Editor#Hierarchy|Hierarchy tab]]]] |
Latest revision as of 12:01, 2 October 2024
Before starting working on modified scripts, we need to prepare the basic structure for our new version of the scoring system. Therefore we will create:
- Basic folder structure (see Directory Structure)
- Empty script files ready for our new code
In this tutorial, the scoring system will be used as an example of script modding and following things will be changed:
Permanently changing scoring coefficients for death & suicide
- Playing a sound upon character suicide
- Those changes are fairly simple and should be a good showcase about how to proceed when modding files.
File structure
Before writing any code, let's start with investigating which files we need to modify and then, prepare structure for our modded files.
Since we want to modify the scoring system, we can begin by searching for terms related to it.
By typing scoring into the Find Symbol search field, we should see SCR_ScoringSystemComponent.c on the first place.
Double clicking opens the file containing that class and reveals its location in the file structure.
Next to it is SCR_BaseScoringSystemComponent.c and those two files should be enough to achieve the goals stated above.
Note that these two files are located in the Scripts
We will be interested in changing the behaviour of:
SampleMod_ModdedScript/Scripts/Game/GameMode/Scoring/Modded
Once the directory is prepared, create two new script files inside the Modded folder.
To do so, right-click on the Resource Browser field to open the context menu.
From there, select "Script" to create a new script file. From here, it is time to create actual code!
- Create a new Script File: In Resources Manager, click the Create button then "Script" to create a script file
- Name the new Script File: the files should have the same name as the modded ones; namely SCR_BaseScoringSystemComponent.c and SCR_ScoringSystemComponent.c
It is usually a good habit to keep the original script and file structures; for the purpose of this tutorial, it assumed that following addon structure is used:
Create a Modified Script
Syntax
It is possible to modify already existing scripts by using some of special keyword:
- modded - keyword used to modify existing scripting class
- override - keyword to override methods in modded classes
- super - allows to invoke content of overridden method
We will use all these three words to create modded variants of SCR_ScoringSystemComponent.
Writing
First, we will begin with the modded keyword:
Next, we can proceed with replacing the CalculateScore method by using the override keyword:
As there is no intention to modify regular kill, team kills or objective score, lines containing:
- info.m_iKills * m_iKillScoreMultiplier
- info.m_iTeamKills * m_iTeamKillScoreMultiplier
- info.m_iObjectives * m_iObjectiveScoreMultiplier
are left untouched.
We are replacing their modifiers which would normally be provided via parameters
- m_iDeathScoreMultiplier is replaced by 10
- m_iSuicideScoreMultiplier is also replaced by 10
This translates into what we want: for every death or suicide, ten points are obtained.
Original code - calculated score is only returned if its above 0, otherwise method returns 0.
Super
Same as with SCR_ScoringSystemComponent, we will use the modded keyword on SCR_BaseScoringSystemComponent to modify the content of that class.
The AddSuicide method is called every time the player commits suicide and adds score according to previously defined modifiers.
Since we don't want to change that part and instead want to add a new behaviour to it, we will use the super keyword to call the overridden method's code.
Once this is done, we can use the PlaySound method from the AudioSystem class - this takes one argument, ResourceName of a sound file - and plays a 2D, non spatial, sound.
A more mod-friendly way would be the following:
The code is now ready to compile (⇧ Shift + F7) and the result can be tested in-game.
Mod Test
Terrain Preparation
At minimum, a new test scenario built in World Editor requires the following prefabs:
- GameMode_Deathmatch_Automatic.et - this prefab contains Deathmatch game mode configuration
- FactionManager_FFA.et - prefab defining which factions are participating in the game. FFA means represents Free For All, meaning there is only one faction
- SpawnPoint_FFA.et - spawn point with FFA specific configuration
- LoadoutManager_FFA.et - respawn loadout manager - the FFA variants contains all available loadouts for both USSR & US characters
All those prefabs can be placed in World Editor's viewport by drag and dropping them from the Resource Browser.
Debug Process
While testing scripts, built-in debugging options such as Breakpoints, Console and Watch features - see Script Editor - Debugging for more information.