Scripting First Steps – Arma Reforger
(Page creation) |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
| Line 1: | Line 1: | ||
Welcome to Enforce Script! This guide will get you writing and testing your | Welcome to Enforce Script! This guide will get you writing and testing your first lines of code in 5 minutes. | ||
{{Feature|informative|This is a hands-on quick start. Skip the theory, start coding immediately!}} | {{Feature|informative|This is a hands-on quick start. Skip the theory, start coding immediately!}} | ||
* '''Arma Reforger Tools''' ( | == Requirements == | ||
* '''Arma Reforger Tools''' ({{GameCategory|armaR|Modding|Official Tools|text= {{armaR}} Workbench}}) installed via Steam | |||
* That's it! | * That's it! | ||
== | |||
== Open Workbench and Remote Console == | |||
# Launch '''Enfusion Workbench''' from Steam (Arma Reforger Tools) | # Launch '''Enfusion Workbench''' from Steam (Arma Reforger Tools) | ||
# Click on the '''Script Editor''' icon or select it from the menu | # Click on the '''Script Editor''' icon or select it from the menu<br>[[File:WE_UI.png|thumb|left|500px|Launch Script Editor from Workbench]]{{Clear}} | ||
# If the '''Remote Console''' window is not visible, open it from the menu | |||
# Click on the '''Remote Console''' and '''Output''' tabs at the bottom to switch to the appropriate view<br><!-- | |||
-->[[File:SE_UI_Default.png|thumb|left|500px|Script Editor default UI - click the tabs at the bottom]]<!-- | |||
-->[[File:SE UI RC and output.png|thumb|left|500px|Script Editor with Remote Console and Output panels visible]]{{Clear}} | |||
# You are ready to start coding! | |||
{{Feature|informative|For these basic exercises, you do not need to load a world or run the game. The Remote Console can execute simple code directly.}} | |||
== First Line of Code == | |||
Type the following in the '''Remote Console''' and click the '''Run''' button at the top of the remote console panel: | |||
The <enforce inline methods="Print">Print</enforce> method prints (as its name states) the provided arguments into the log console: | |||
<enforce> | |||
Print("Hello there!"); // displays "Hello there!" in the log console | |||
</enforce> | |||
'''You did it!''' You just wrote your first Enforce Script code. The text appears in the console output. | |||
<enforce inline methods="PrintFormat">PrintFormat</enforce> allows for printing a string with arguments: | |||
<enforce> | <enforce> | ||
PrintFormat("Hello %1, welcome to %2!", "there", "Arma Reforger"); // %1 is replaced by "there" (the first argument), %2 by "Arma Reforger | |||
PrintFormat("Hello %1, welcome to %2!", "you", "Enfusion"); // result here is "Hello you, welcome to Enfusion!" | |||
</enforce> | </enforce> | ||
== | == Basic Math == | ||
Try these commands one by one in the Remote Console: | Try these commands one by one in the Remote Console: | ||
| Line 46: | Line 53: | ||
</enforce> | </enforce> | ||
The console shows: < | The console shows: <enforce inline>15</enforce>, <enforce inline>60</enforce>, <enforce inline>25</enforce>. | ||
== | |||
== Variables == | |||
Variables let you store and reuse values: | Variables let you store and reuse values: | ||
| Line 70: | Line 78: | ||
float distance = 150.5; | float distance = 150.5; | ||
string playerName = "Soldier"; | string playerName = "Soldier"; | ||
PrintFormat("%1 is at %2m", playerName, distance); | |||
</enforce> | </enforce> | ||
== | |||
== Decisions == | |||
<enforce> | <enforce> | ||
| Line 81: | Line 89: | ||
if (health > 50) | if (health > 50) | ||
Print("Healthy!"); | Print("Healthy!"); | ||
else | else | ||
Print("Need medical attention"); | Print("Need medical attention"); | ||
</enforce> | </enforce> | ||
Try changing < | Try changing <enforce inline>health</enforce> to different numbers! | ||
Arrays store multiple values: | == Arrays == | ||
Arrays can be seen as lists that store naught to multiple values '''of the same type''': | |||
<enforce> | <enforce> | ||
array<string> soldiers = {"Alpha", "Bravo", "Charlie"}; | array<string> soldiers = { "Alpha", "Bravo", "Charlie" }; | ||
Print(soldiers[0]); | Print(soldiers[0]); // Prints "Alpha" | ||
Print(soldiers[1]); | Print(soldiers[1]); // Prints "Bravo" | ||
soldiers.Insert("Delta"); | soldiers.Insert("Delta"); // soldiers is now { "Alpha", "Bravo", "Charlie", "Delta" } | ||
Print("Squad size: " + soldiers.Count()); | Print("Squad size: " + soldiers.Count()); // Squad size: 4 | ||
</enforce> | </enforce> | ||
== Loops == | |||
=== For === | |||
A <enforce inline>for</enforce> loop repeats instructions a specific number of times: | |||
<enforce> | <enforce> | ||
for (int i | for (int i; i < 5; i++) | ||
{ | { | ||
Print("Count: " + i); | Print("Count: " + i); // prints ("Count: ") 0, 1, 2, 3, 4 then leaves (i < 5) | ||
} | } | ||
</enforce> | </enforce> | ||
=== Foreach === | |||
A <enforce inline>foreach</enforce> loop goes through all array items: | |||
<enforce> | <enforce> | ||
array<string> weapons = {"Rifle", "Pistol", "Grenade"}; | array<string> weapons = { "Rifle", "Pistol", "Grenade" }; | ||
foreach (string weapon : weapons) | foreach (string weapon : weapons) | ||
{ | { | ||
Print("Weapon: " + weapon); | Print("Weapon: " + weapon); // prints "Rifle", "Pistol", "Grenade" | ||
} | } | ||
</enforce> | </enforce> | ||
== | The index can also be obtained this way: | ||
<enforce> | |||
foreach (int i, string weapon : weapons) | |||
</enforce> | |||
== A First Mini-Program == | |||
Combine what you learned! Copy this entire block and run it: | Combine what you learned! Copy this entire block and run it: | ||
| Line 134: | Line 150: | ||
<enforce> | <enforce> | ||
// Soldier health checker | // Soldier health checker | ||
array<string> | array<string> soldierNames = { "Alpha", "Bravo", "Charlie", "Delta" }; | ||
array<int> healthValues = {100, 45, 80, 20}; | array<int> healthValues = { 100, 45, 80, 20 }; | ||
Print("=== Squad Status Report ==="); | Print("=== Squad Status Report ==="); | ||
foreach (int i, string soldierName : soldierNames) | |||
{ | { | ||
int health = healthValues[i]; | int health = healthValues[i]; | ||
if (health > 70) | if (health > 70) | ||
PrintFormat("%1: Healthy (%2%%)", soldierName, health); | |||
else if (health > 30) | else if (health > 30) | ||
PrintFormat("%1: Injured (%2%%)", soldierName, health); | |||
else | else | ||
PrintFormat("%1: Critical! (%2%%)", soldierName, health); | |||
} | } | ||
| Line 161: | Line 170: | ||
</enforce> | </enforce> | ||
{{Feature|informative|Lines starting with <enforce inline>// | {{Feature|informative|Lines starting with <enforce inline>//</enforce> are '''comments''' - they are ignored by the code but help explain what is happening.}} | ||
== Quick Reference Card == | == Quick Reference Card == | ||
{| class="wikitable" | {| class="wikitable" | ||
! Category !! Code !! | ! Category !! Code !! Description | ||
|- | |- | ||
| '''Output''' || <enforce | | '''Output''' || <enforce>Print("text");</enforce> || Shows text in console | ||
|- | |- | ||
| '''Variables''' || <enforce | | rowspan="4" | '''Variables''' || <enforce>int number = 42;</enforce> || Stores a whole number | ||
|- | |- | ||
| <enforce>float decimal = 3.14;</enforce> || Stores a decimal number | |||
|- | |- | ||
| <enforce>string text = "Hi";</enforce> || Stores text | |||
|- | |- | ||
| <enforce>bool flag = true;</enforce> || Stores true/false | |||
|- | |- | ||
| '''Arrays''' || <enforce | | rowspan="3" | '''Arrays''' || <enforce>array<int> numbers = { 1, 2, 3 };</enforce> || Creates a list | ||
|- | |- | ||
| <enforce>myArray.Insert(value);</enforce> || Adds item to list | |||
|- | |- | ||
| <enforce>myArray.Count();</enforce> || Gets list size | |||
|- | |- | ||
| '''If/Else''' || <enforce | | rowspan="2" | '''If/Else''' || <enforce> | ||
if (condition) | |||
{ | |||
} | |||
</enforce> || Do something if true | |||
|- | |- | ||
| <enforce> | |||
else | |||
{ | |||
} | |||
</enforce> || Do something if false | |||
|- | |- | ||
| '''Loops''' || <enforce | | rowspan="2" | '''Loops''' || <enforce> | ||
for (int i; i < 10; i++) | |||
{ | |||
} | |||
</enforce> || Repeat 10 times | |||
|- | |- | ||
| <enforce> | |||
foreach (int item : myArray) | |||
{ | |||
} | |||
</enforce> || Execute for each item | |||
|- | |- | ||
| '''Math''' || <enforce inline>+</enforce> || Add | | rowspan="4" | '''Math''' || <enforce inline>+</enforce> || Add | ||
|- | |- | ||
| <enforce inline>-</enforce> || Subtract | |||
|- | |- | ||
| <enforce inline>*</enforce> || Multiply | |||
|- | |- | ||
| <enforce inline>/</enforce> || Divide | |||
|- | |- | ||
| '''Compare''' || <enforce inline>==</enforce> || Equal to | | rowspan="6" | '''Compare''' || <enforce inline>==</enforce> || Equal to | ||
|- | |- | ||
| <enforce inline>!=</enforce> || Not equal to | |||
|- | |- | ||
| <enforce inline>></enforce> || Greater than | |||
|- | |- | ||
| <enforce inline><</enforce> || Less than | |||
|- | |- | ||
| <enforce inline>>=</enforce> || Greater or equal | |||
|- | |- | ||
| <enforce inline><=</enforce> || Less or equal | |||
|} | |} | ||
== Common Mistakes | |||
== Common Mistakes == | |||
{| class="wikitable" | {| class="wikitable" | ||
! | ! Incorrect !! Correct !! Explanation | ||
|- | |- | ||
| <enforce inline>print("hello")</enforce> | | <enforce inline>print("hello")</enforce> | ||
| Line 230: | Line 256: | ||
| Use == for comparison, = for assignment | | Use == for comparison, = for assignment | ||
|- | |- | ||
| <enforce inline> | | <enforce inline>int firstElement = myArray[1];</enforce> | ||
| <enforce inline> | | <enforce inline>int firstElement = myArray[0];</enforce> | ||
| Arrays start at 0, not 1 | | Arrays start at 0, not 1 | ||
|- | |- | ||
| <enforce inline>string name = John</enforce> | | <enforce inline noGuess>string name = John</enforce> | ||
| <enforce inline>string name = "John"</enforce> | | <enforce inline>string name = "John"</enforce> | ||
| Text needs quotes | | Text needs quotes | ||
| Line 241: | Line 267: | ||
{{Feature|informative|Follow professional coding standards: {{Link|Arma Reforger:Scripting: Conventions|Scripting Conventions}}.}} | {{Feature|informative|Follow professional coding standards: {{Link|Arma Reforger:Scripting: Conventions|Scripting Conventions}}.}} | ||
== Exercises == | |||
The following challenges help in practicing and understanding scripting. | |||
=== Temperature Converter === | |||
Create a program that converts 25°C to Fahrenheit. | Create a program that converts 25°C to Fahrenheit. | ||
<spoiler text="Show solution"> | <spoiler text="Show solution"> | ||
| Line 252: | Line 280: | ||
float celsius = 25; | float celsius = 25; | ||
float fahrenheit = (celsius * 9 / 5) + 32; | float fahrenheit = (celsius * 9 / 5) + 32; | ||
PrintFormat("%1°C = %2°F", celsius, fahrenheit); | |||
</enforce> | </enforce> | ||
</spoiler> | </spoiler> | ||
=== Countdown === | |||
Create a countdown from 10 to 0, then print "Launch!". | Create a countdown from 10 to 0, then print "Launch!". | ||
<spoiler text="Show solution"> | <spoiler text="Show solution"> | ||
| Line 265: | Line 294: | ||
Print(i); | Print(i); | ||
} | } | ||
Print("Launch!"); | Print("Launch!"); | ||
</enforce> | </enforce> | ||
</spoiler> | </spoiler> | ||
=== Squad Filter === | |||
Print only the soldiers with health above 50 from two arrays (names and health values). | Print only the soldiers with health above 50 from two arrays (names and health values). | ||
<spoiler text="Show solution"> | <spoiler text="Show solution"> | ||
<enforce> | <enforce> | ||
// Only print soldiers with health above 50 | // Only print soldiers with health above 50 | ||
array<string> names = {"Alpha", "Bravo", "Charlie"}; | array<string> names = { "Alpha", "Bravo", "Charlie" }; | ||
array<int> health = {80, 30, 90}; | array<int> health = { 80, 30, 90 }; | ||
foreach (int i, string name : names) | |||
{ | { | ||
if (health[i] > 50) | if (health[i] > 50) | ||
Print(name + " is combat ready"); | |||
Print( | |||
} | } | ||
</enforce> | </enforce> | ||
</spoiler> | </spoiler> | ||
== Troubleshooting == | == Troubleshooting == | ||
'''Console | '''Console will not open?''' | ||
* Make sure | * Make sure the '''Script Editor''' is opened in Workbench | ||
* Look for the '''Remote Console''' panel - if hidden, open it from the menu | * Look for the '''Remote Console''' panel - if hidden, open it from the menu | ||
* Ensure Workbench is properly installed via Steam (Arma Reforger Tools) | * Ensure Workbench is properly installed via Steam (Arma Reforger Tools) | ||
'''Code | '''Code does not work?''' | ||
* Check for missing semicolons <enforce inline>;</enforce> | * Check for missing semicolons <enforce inline>;</enforce> | ||
* Make sure quotes match: <enforce inline>"text"</enforce> | * Make sure quotes match: <enforce inline>"text"</enforce> | ||
* Check spelling and | * Check spelling and capitalisation (casing matters: {{hl|print("ok");}} is different from {{hl|Print("ok");}}) | ||
* Look for error messages in red | * Look for error messages in red | ||
'''Getting errors?''' | '''Getting errors?''' | ||
* Read the error message - it often tells you what | * Read the error message - it often tells you what is wrong | ||
* | * Click the error message and check reported line number(s) | ||
* Make sure all brackets <enforce inline>{ }</enforce> are | * Make sure all open brackets <enforce inline>{ }</enforce> are closed | ||
== See Also == | == See Also == | ||
| Line 309: | Line 340: | ||
'''Reference Documentation:''' | '''Reference Documentation:''' | ||
* {{Link|Arma Reforger:Scripting: Values|Scripting Values}} - All data types in detail | * {{Link|Arma Reforger:Scripting: Values|Scripting Values}} - All data types in detail | ||
* {{Link|Arma Reforger:Scripting: Conventions|Scripting Conventions}} - | * {{Link|Arma Reforger:Scripting: Conventions|Scripting Conventions}} - {{Name|bi}} coding standards | ||
* {{Link|Arma Reforger:Object Oriented Programming Basics|OOP Basics}} - Object-oriented programming fundamentals | * {{Link|Arma Reforger:Object Oriented Programming Basics|OOP Basics}} - Object-oriented programming fundamentals | ||
{{GameCategory|armaR|Modding|Scripting|Tutorials}} | {{GameCategory|armaR|Modding|Scripting|Tutorials}} | ||
Revision as of 00:21, 18 November 2025
Welcome to Enforce Script! This guide will get you writing and testing your first lines of code in 5 minutes.
Requirements
- Arma Reforger Tools (Arma Reforger Workbench) installed via Steam
- That's it!
Open Workbench and Remote Console
- Launch Enfusion Workbench from Steam (Arma Reforger Tools)
- Click on the Script Editor icon or select it from the menu
- If the Remote Console window is not visible, open it from the menu
- Click on the Remote Console and Output tabs at the bottom to switch to the appropriate view
- You are ready to start coding!
First Line of Code
Type the following in the Remote Console and click the Run button at the top of the remote console panel:
The Print method prints (as its name states) the provided arguments into the log console:
You did it! You just wrote your first Enforce Script code. The text appears in the console output.
PrintFormat allows for printing a string with arguments:
Basic Math
Try these commands one by one in the Remote Console:
The console shows: 15, 60, 25.
Variables
Variables let you store and reuse values:
Try changing the number and run it again!
Common variable types:
- int - whole numbers (1, 42, -5)
- float - decimal numbers (3.14, 10.5)
- string - text ("Hello", "Arma")
- bool - true or false
Decisions
Try changing health to different numbers!
Arrays
Arrays can be seen as lists that store naught to multiple values of the same type:
Loops
For
A for loop repeats instructions a specific number of times:
Foreach
A foreach loop goes through all array items:
The index can also be obtained this way:
A First Mini-Program
Combine what you learned! Copy this entire block and run it:
Quick Reference Card
| Category | Code | Description |
|---|---|---|
| Output | Print("text"); |
Shows text in console |
| Variables | Stores a whole number | |
| Stores a decimal number | ||
| Stores text | ||
| Stores true/false | ||
| Arrays | Creates a list | |
myArray.Insert(value); |
Adds item to list | |
myArray.Count(); |
Gets list size | |
| If/Else | if (condition)
{
} |
Do something if true |
else
{
} |
Do something if false | |
| Loops | Repeat 10 times | |
| Execute for each item | ||
| Math | + | Add |
| - | Subtract | |
| * | Multiply | |
| / | Divide | |
| Compare | == | Equal to |
| != | Not equal to | |
| > | Greater than | |
| < | Less than | |
| >= | Greater or equal | |
| <= | Less or equal |
Common Mistakes
| Incorrect | Correct | Explanation |
|---|---|---|
| print("hello") | Print("hello") | Capital P in Print |
| int x = 5 | int x = 5; | Missing semicolon |
| if (x = 5) | if (x == 5) | Use == for comparison, = for assignment |
| int firstElement = myArray[1]; | int firstElement = myArray[0]; | Arrays start at 0, not 1 |
| string name = John | string name = "John" | Text needs quotes |
Exercises
The following challenges help in practicing and understanding scripting.
Temperature Converter
Create a program that converts 25°C to Fahrenheit.
Countdown
Create a countdown from 10 to 0, then print "Launch!".
Squad Filter
Print only the soldiers with health above 50 from two arrays (names and health values).
Troubleshooting
Console will not open?
- Make sure the Script Editor is opened in Workbench
- Look for the Remote Console panel - if hidden, open it from the menu
- Ensure Workbench is properly installed via Steam (Arma Reforger Tools)
Code does not work?
- Check for missing semicolons ;
- Make sure quotes match: "text"
- Check spelling and capitalisation (casing matters: print("ok"); is different from Print("ok");)
- Look for error messages in red
Getting errors?
- Read the error message - it often tells you what is wrong
- Click the error message and check reported line number(s)
- Make sure all open brackets { } are closed
See Also
Reference Documentation:
- Scripting Values - All data types in detail
- Scripting Conventions - Bohemia Interactive coding standards
- OOP Basics - Object-oriented programming fundamentals