Introduction to Arma Scripting: Difference between revisions
m (Started page introduction overhaul) |
m (More work on the overhaul) |
||
Line 1: | Line 1: | ||
{{TOC|side}} <!--{{Table/RVScripting}}--><!-- Table/RVScripting will | {{TOC|side}} <!--{{Table/RVScripting}}--><!-- Table/RVScripting will be moved down a couple sections --> | ||
<!--This page should give beginners an overview of how to begin with scripting and where to find most of the information they need in the beginning. Some information might not be complete and can be found on other wiki pages. | <!--This page should give beginners an overview of how to begin with scripting and where to find most of the information they need in the beginning. Some information might not be complete and can be found on other wiki pages. | ||
Line 7: | Line 7: | ||
The solution for this is to take advantage of the game-engine's ability to employ an even more versatile feature known as scripting. Arma's scripting language [[SQF Syntax|SQF]] (and its predecessor [[SQS Syntax|SQS]] from {{ofp}}) can give you more direct control over core game features through [[:Category: Scripting Commands|script commands]] and [[:Category: Functions|scripted functions]].--> | The solution for this is to take advantage of the game-engine's ability to employ an even more versatile feature known as scripting. Arma's scripting language [[SQF Syntax|SQF]] (and its predecessor [[SQS Syntax|SQS]] from {{ofp}}) can give you more direct control over core game features through [[:Category: Scripting Commands|script commands]] and [[:Category: Functions|scripted functions]].--> | ||
{{Wiki|WIP}} | {{Wiki|WIP}} | ||
Scripting is one of the most powerful and most versatile | Scripting is one of the most powerful and most versatile tools available in the Arma sandbox. Unfortunately, it is also one of the most complex and unforgiving aspects of addon (mod) and mission creation. <!--As such, this page is intended to serve as a reference ...--> | ||
The scripting language currently used in the Arma series is called [[SQF Syntax|SQF]]. The predecessor to SQF, [[SQS Syntax|SQS]], has been considered deprecated since {{arma1}} (2006) and is no longer used. | |||
{{ | |||
Line 78: | Line 64: | ||
Because script files are essentially plain text files, they can be edited with just about any text editor. For instance, the native Windows Notepad can be used, but working with it is not very comfortable. As such, regular scripters typically use more versatile applications such as [https://notepad-plus-plus.org Notepad++] or [https://code.visualstudio.com Visual Studio Code], usually in combination with plugins that add additional support for SQF. One feature commonly provided by these plugins is ''syntax highlighting'', and for good reason: Syntax highlighting makes code significantly more readable and helps recognizing and avoiding basic syntax errors. Consequently, it is highly recommended to use syntax highlighting. | Because script files are essentially plain text files, they can be edited with just about any text editor. For instance, the native Windows Notepad can be used, but working with it is not very comfortable. As such, regular scripters typically use more versatile applications such as [https://notepad-plus-plus.org Notepad++] or [https://code.visualstudio.com Visual Studio Code], usually in combination with plugins that add additional support for SQF. One feature commonly provided by these plugins is ''syntax highlighting'', and for good reason: Syntax highlighting makes code significantly more readable and helps recognizing and avoiding basic syntax errors. Consequently, it is highly recommended to use syntax highlighting. | ||
See [[:Category:Community Tools#Code Edition|Community Tools]] for a selection of community-made applications and plugins for Arma Scripting. | See [[:Category:Community Tools#Code Edition|Community Tools - Code Edition]] for a selection of community-made applications and plugins for Arma Scripting. | ||
=== Script Execution === | === Script Execution === | ||
Line 86: | Line 72: | ||
<code>[[execVM]] "scripts\myFirstScript.sqf";</code> | <code>[[execVM]] "scripts\myFirstScript.sqf";</code> | ||
This can be done anywhere: Within another script, from the [[Arma_3:_Debug_Console|Debug Console]], in the ''On Activation'' or ''On Deactivation'' expression of a [[Trigger]] or in the init field of an [[Eden Editor: Object|Object]] in the Editor. | This can be done anywhere: Within another script, from the [[Arma_3:_Debug_Console|Debug Console]], in the ''On Activation'' or ''On Deactivation'' expression of a [[Trigger]] or in the init field of an [[Eden Editor: Object|Object]] in the Editor. | ||
== Before anything ==<!-- "General Considerations" perhaps? --> | |||
; Is your idea necessary? | |||
: Will players even notice or use what you want to script? Just because you can does not mean you should. Sometimes less is more! | |||
; Is it possible to do this in the editor? | |||
: The [[Eden Editor]] is a powerful tool and with it alone one can achieve a lot of things without writing a single line of code. | |||
: Poorly written scripts are a frequent cause of poor performance, both in singleplayer and multiplayer scenarios. | |||
; Can it be scripted using SQF? | |||
: This question might be hard to answer. Try to get as much information about what you want to do and what [[:Category: Scripting Commands|commands]] and [[:Category: Functions|functions]] there are before spending time on writing a script, just to find out that what you hoped to achieve is not possible after all. | |||
{{Feature|Informative|Scripting is <u>not</u> the solution for everything!}} | |||
Revision as of 18:55, 13 June 2021
Scripting is one of the most powerful and most versatile tools available in the Arma sandbox. Unfortunately, it is also one of the most complex and unforgiving aspects of addon (mod) and mission creation.
The scripting language currently used in the Arma series is called SQF. The predecessor to SQF, SQS, has been considered deprecated since Armed Assault (2006) and is no longer used.
Basics
File Creation
Scripts are usually placed in script files. It is of course possible and sometimes even necessary to use short pieces of code in the Editor (e.g. in the On Activation expression of a Trigger), but scripts can become long and complex, and then working with them is far easier when they are properly placed in script files. Additionally, some features are only accessible through the use of script files: Event Scripts for example.
Script files are basically just text files with a certain filename extension. For script files, that file extension is .sqf (or .sqs), but in the broader context of Arma scripting, modification, configuration and mission design, more file extensions can be encountered: .ext, .hpp, .cpp and .cfg to mention the most common ones.
Unfortunately, Windows does not make the creation of blank files with a desired file extension easily accessible.
For instance, a common pitfall when trying to use Description.ext (a file that is used to configure certain mission features such as the respawn settings) for the first time is (unknowingly) creating Description.ext.txt instead of Description.ext because the Windows File Explorer hides file extensions by default. Obviously, Description.ext.txt will not work and will not have any of the desired effects on the mission because the game does not recognize it as Description.ext, but identifying a wrong file extension as the root cause of an issue when troubleshooting is notoriously difficult as one is usually looking for errors in the code and not in the filename.
While there are many different ways to create a blank text file with a specific file extension, the easiest method using native Windows tools is probably this:
- Preparation (only needs to be done once):
- Open the File Explorer
- Open the View tab at the top
- Tick the File name extensions checkbox
- File Creation:
- Navigate to the location where you want to create a new script file
- Right-click
- Go to New
- Click on Text Document
- Rename New Text Document.txt to what you need
File Locations
In the context of mission creation, script files generally need to be placed in the corresponding scenario folder (often also called the mission root folder). Every mission has its own scenario folder, it is created by the Editor when saving the scenario for the first time. By default it only contains a single file called mission.sqm; this file mostly stores data regarding Editor-placed entities and does not need to be touched when scripting.
Description.ext and Event Scripts have to be placed directly in the root of the scenario folder (i.e. next to mission.sqm), but all other files can be placed in subfolders of the scenario folder. A well-structured scenario folder could look like this:
Apex%20Protocol.Tanoa/ ├── functions/ │ ├── fn_myFirstFunction.sqf │ └── fn_mySecondFunction.sqf ├── scripts/ │ ├── myFirstScript.sqf │ └── mySecondScript.sqf ├── description.ext ├── initPlayerLocal.sqf ├── initServer.sqf └── mission.sqm
Each scenario folder is stored in either the missions or the mpmissions subfolder of the folder containing the Arma Profile that was used to create the scenario. For instance, the path to the scenario folder from the previous example could be:
C:\Users\Scott Miller\Documents\Arma 3 - Other Profiles\Keystone\missions\Apex%20Protocol.Tanoa
The Editor uses percent-encoding for scenario folder names, that is why whitespaces in the scenario name are replaced with %20.
See Also:
- 2D Editor: External
- Eden Editor: Scenario Folder
- Profile (the possible profile folder paths are listed there)
Editing Script Files
Because script files are essentially plain text files, they can be edited with just about any text editor. For instance, the native Windows Notepad can be used, but working with it is not very comfortable. As such, regular scripters typically use more versatile applications such as Notepad++ or Visual Studio Code, usually in combination with plugins that add additional support for SQF. One feature commonly provided by these plugins is syntax highlighting, and for good reason: Syntax highlighting makes code significantly more readable and helps recognizing and avoiding basic syntax errors. Consequently, it is highly recommended to use syntax highlighting.
See Community Tools - Code Edition for a selection of community-made applications and plugins for Arma Scripting.
Script Execution
The execVM command is used to execute script files.
For instance, myFirstScript.sqf from the File Locations example above can simply be executed like so:
execVM "scripts\myFirstScript.sqf";
This can be done anywhere: Within another script, from the Debug Console, in the On Activation or On Deactivation expression of a Trigger or in the init field of an Object in the Editor.
Before anything
- Is your idea necessary?
- Will players even notice or use what you want to script? Just because you can does not mean you should. Sometimes less is more!
- Is it possible to do this in the editor?
- The Eden Editor is a powerful tool and with it alone one can achieve a lot of things without writing a single line of code.
- Poorly written scripts are a frequent cause of poor performance, both in singleplayer and multiplayer scenarios.
- Can it be scripted using SQF?
- This question might be hard to answer. Try to get as much information about what you want to do and what commands and functions there are before spending time on writing a script, just to find out that what you hoped to achieve is not possible after all.
Terms
The following is a collection of terms frequently encountered when talking or reading about scripting.
- Game Engine
- The core program of the game which executes your scripting commands at run time.
- Script / Script File
- Scripts are usually placed in script files. Script files contain code.
- Syntax
- See SQF Syntax (Arma, Arma 2, Arma 3).
- See SQS Syntax (Operation Flashpoint, Arma).
- Variables
- A Variable is a named storage container for data.
- The name of a variable is called its Identifier.
- Data Types
- The Data Type of a variable specifies which kind of data that variable can contain.
- Operators
- See Operators
- Control Structures
- See Control Structures
- Functions
- See Function
Must-read articles
Best Practices
Debugging
Optimisation
Useful Links
These pages cover further aspects of scripting:
- Example Code
- Control Structures
- Multiplayer Scripting
- Exception handling
- Script (File)
- Function
- SQS to SQF conversion
Consider the following resources for more general learning:
- 6thSense.eu Editing Guide
- Fockers Arma 3 Scripting Guide (dead link)
- Mr-Murray's Armed Assault Editing Guide - Deluxe Edition (dead link)
- Excellent German SQF tutorial (YouTube)