Difference between revisions of "Introduction to Arma Scripting"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Removed "Further reading" and merged it with see also)
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Stub}}
+
{{SideTOC}}
 +
== Introduction ==
 +
 
 +
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.
  
 
During [[ArmA: Mission Editing|mission editing]] and [[ArmA: Addon Editing|addon editing]] you may come across situations where actions or features you would like to have in your mission or addon cannot be accomplished using the basic (or even the more advanced) capabilities of the [[ArmA: Mission Editor|mission editor]] or within config files (in the case of addons). Some examples of this might be really cinematic cutscenes in missions or special animations for an addon.
 
During [[ArmA: Mission Editing|mission editing]] and [[ArmA: Addon Editing|addon editing]] you may come across situations where actions or features you would like to have in your mission or addon cannot be accomplished using the basic (or even the more advanced) capabilities of the [[ArmA: Mission Editor|mission editor]] or within config files (in the case of addons). Some examples of this might be really cinematic cutscenes in missions or special animations for an addon.
  
 +
The '''solution''' to this is to take advantage of the game-engine's ability to call on an even more advanced feature known as [[Scripting|scripting]]. '''Arma''''s '''scripting language''' gives you a more direct control of core game features. With any combination of these '''[[:Category: Scripting Commands|commands]]''' and '''[[:Category: Functions|functions]]''' you can create custom processes that meet the specific needs of your mission or addon.
  
The '''solution''' to this is to take advantage of the game-engines ability to call on an even more advanced feature known as [[Scripting|scripting]]. [[Armed Assault|Armed Assault's]] '''scripting language''' gives you more direct control of core game commands. With any combination of these [[:Category:ArmA: Scripting Commands|scripting commands]] you can then create custom processes that meet the specific needs of your mission or addon.
 
 
== Terms ==
 
 
Before getting started, you should understand the meaning of these terms.
 
 
;Script
 
: When speaking about a [[Script (File)|script]], we usually mean a .sqs or .sqf file. Both file types can be edited as a plain text file using a program like Notepad, [https://notepad-plus-plus.org/ Notepad++] or [http://www.geany.org/ Geany].
 
 
;Game Engine
 
: The core program of the game which reads and executes your scripting commands at run time.
 
 
;Function
 
: Piece of code which performs a specific task when executed. A function may return a value. The mission author often writes complicated functions in .sqf files and then executes them in-game. Functions can be considered a kind of script. Usually, however, a "script" means a .sqs or .sqf file that runs over a period of time and directs gameplay, while a "function" is usually meant to run instantaneously and return a value. See [[function]].
 
  
 +
== Before anything ==
  
 +
=== Is your idea necessary? ===
  
= The things to ask yourself beforehand =
 
 
{{note|Be careful: Scripting isn't a solution to everything. }}
 
 
=== Is it not possible to do this in the editor? ===
 
* [[Eden Editor]] is an incredible powerful tool and with it alone one can achive a lot of things, even without writing a single line of SQF.
 
*Poorly written scripts are often the reason for poor performance both in singleplayer and multiplayer scenarios.
 
=== Is it necessary? ===
 
 
* Will players even notice or use what you want to script? Just because you can doesn’t mean you should. Sometimes less is more!
 
* Will players even notice or use what you want to script? Just because you can doesn’t mean you should. Sometimes less is more!
=== Can it be scripted by using SQF? ===
 
* This might be hard to answer, especially for beginners. Try to get as much information about what you want to do and what commands there are before spending time on writing a script, just to find out it’s #not possible in the end.
 
  
 +
=== Is it possible to do this in the editor? ===
  
 +
* [[Eden Editor]] is an incredibly powerful tool and with it alone one can achive a lot of things, even without writing a single line of [[SQF syntax|SQF]].
 +
* ''Poorly written'' scripts are often the reason for poor performance both in singleplayer and multiplayer scenarios.
  
*Should any question above be answered with ‘’’Yes’’’, then you should rethink your approach.
+
=== Can it be scripted using SQF? ===
  
== The Concept ==
+
* This might be hard to answer, especially for beginners. 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 it is ''not'' possible in the end.
  
Scripts are an essential part of making missions. They allow you to create amazing cutscenes, create effects and customize almost every aspect of your mission. Some diverse examples of what could be scripted are: a simulation of artillery fire, a poisonous gas cloud, or a money system for purchasing equipment.       
 
  
===Algorithm===
+
Depending on your answer for these questions, you may want to rethink your approach.
  
Your first task in writing scripts is to decide what sequence of steps is needed to solve a particular problem. This sequence is what would be called an ''algorithm''.  
+
{{Informative | '''Scripting''' is <u>not</u> a solution to everything.}}
  
For example, if you wanted to transport weapons from Petrovice to Lipany, you would (as commander) give these orders:
 
  
*'''Commander:''' Private Honka, private Kouba, come here!
+
== Terms ==
*'''Commander:''' Private Kouba, get in Ural as driver!
 
*'''Commander:''' Private Honka, get in Ural!
 
*'''Commander:''' Private Kouba, drive to Petrovice and load Ural!
 
 
 
*'''Private Kouba''': I don't know where Petrovice is, sir.
 
  
Now we have a problem. Kouba only knows how to get to Lipany and Honka only knows how to get to Petrovice. Our soldiers are new recruits and are really quite stupid. They can only take simple orders, one order at a time.  They can get in and out of vehicles, drive, and load and unload weapons and ammo. The commander must plan this sequence of orders and make provisions for obstacles that may arise.
+
Before getting started, you should understand the meaning of these terms.
  
1. Destination is Petrovice
+
'''Data Types''':
2. Get in vehicle
+
See [[Data Types]]
3. If driver does not know the way to Destination, swap driver and passenger
 
4. Drive to Destination
 
5. Get out of vehicle
 
6. Load vehicle
 
7. Destination is Lipany
 
8. Get in vehicle
 
9. If driver does not know the way to Destination, swap driver and passenger
 
10. Drive to Destination
 
11. Get out of vehicle
 
12. Unload vehicle
 
  
===Interpreter===
+
'''Algorithm''':
 +
In mathematics and computer science, an algorithm is an explicit specification of how to solve a class of problems. Algorithms can perform calculation, data processing and automated reasoning tasks.
  
If you have an algorithm, you need something that can execute it. We have a computer game, Armed Assault, which is able to do this; commanders have soldiers.
+
'''Interpreter''':
 +
Reads your code from a script file and translates it into instructions for you to achieve your desired outcome/effect in the game.
  
===Programming language===
+
'''Control Structures''':
 +
See [[Control Structures]]
  
It's the way how to write our algorithm for Armed Assault. ArmA scripts are a sequence of 'orders' describing how to do it.
+
'''Syntax''':
 +
* See [[SQF syntax]]
 +
* See [[SQS syntax]] (obsolete)
  
===Source code===
+
'''Script''':
 +
When speaking about a [[Script (File)|script]], we usually mean a .sqs or .sqf file.
  
Source code is algorithm written in any programming language.
+
'''Game Engine''':
 +
The core program of the game which reads and executes your scripting commands at run time.
  
===Syntax===
+
'''Function''':
 
+
See [[Function]]
'''Syntax''' is made up of commands and parameters, sometimes just the command is required as in [[exit]], other times the '''syntax''' is made up of one or more parameters. Each command has it's own reference in the [[Scripting]] section of the wiki. In each case the command + its parameters are listed with examples on how that particular command works.
 
 
 
'''[[if]]'''(PrivateHonka == TheMostCleverSoldierInTheWorld) [[then]] {
 
    IAmChineseGodOfHumour = true;
 
};
 
 
 
You should however be aware that there are two similar scripting grammars in Armed Assault, namely [[SQF]] and [[SQS]]. While the functions are almost the same, there are still minor differences in statement notations, program flow and control structures.
 
 
 
===Interpreting works===
 
 
 
The '''Armed Assault''' engine reads your code from script files and translates those instructions for you to achieve your desired outcome/effect in the game.
 
 
 
===Let's start===
 
 
 
Every script consist of commands (see category [[:Category:Scripting Commands|Scripting Commands]]) and [[Control Structures|program flow]] statements (they are implemented as commands in Armed Assault, but it isn't relevant for now). The most useful command in your first script is [[titleText]]. It can write any text you want to the player's screen.
 
 
 
[[titleText]]["Good morning, captain", "PLAIN DOWN"]
 
 
 
It's a typical way to run commands. Behind the name of the command the parameters follow (but it depends on the command (see [[:Category:Scripting Commands]]). Every command can only have one parameter before it's name and/or one after (more arguments can be given with arrays which count as one parameter). Parameters can be of various [[:Category:Types|Data types]]. In this case it's an [[Array]] - a list of other data types. It can contain 0 - 4096? values. The first value is a [[String]] representing the text to be displayed and the second, in this case, says in what position on the screen the text will be displayed. There can be a third value: a [[Number]] which says how long the text needs to fade in. If this value is not entered, its default value (1) is used.
 
 
 
[[titleText]]["Good morning, captain", "PLAIN DOWN", 5]
 
 
 
Scripts which are called [[function]]s) are stored in .SQF files the mission folder, or in .SQSs files and then called only scripts.
 
 
 
If you want to try our 'script', create a mission in the mission editor, save it as ''testingmission'', open your favorite text edtior (eg. Notepad), write <code>[[titleText]]["Good morning, captain", "PLAIN DOWN"];</code>and save it as ''hello.sqf'' to gamefolder/user/yourname/missions/testingmission. Then add a soldier in the mission editor and type <code>nul = [] [[execVM]] "hello.sqf"</code> to his initialization line. When you run this mission, you should see output of your first script. Well done, soldier! (If you are confused from this quantum of informations, don't panic, more continuously explanation follows.)
 
 
 
=== Variables ===
 
  
 +
'''Variables''':
 
See [[Variables]]
 
See [[Variables]]
  
===Operators===
+
'''Operators''':
 
 
 
See [[Operators]]
 
See [[Operators]]
  
== Scripting Code ==
 
  
The core of scripting is '''scripting code'''. The code consists of [[:Category:ArmA: Scripting Commands|scripting commands]] that tell the game engine what to do. These commands are executed one after another.
+
== Recommended programs ==
  
The code is written into special fields of the [[ArmA: Mission Editor|mission editor]] (see below) or into separate files that are executed at some defined point (i.e. through [[Triggers|triggers]]) during the running mission.
+
'''Code Edition''' programs can be found on the [[:Category:Community Tools#Code Edition|Community Tools - Code Edition]] page section.
  
=== Syntax ===
 
  
Every code has to follow a '''syntax'''. The syntax is the "grammar" of the scripting language. It makes sure that the game engine can read and understand the code.
+
== Must-read articles ==
  
The primary syntax used in [[Armed Assault]] and [[ArmA 2]] is [[SQF syntax]]. Read the corresponding article to inform yourself about the exact details. There are some crucial points which you must know about:
+
=== Best Practices ===
  
* Every statement of SQF code ''except for the last one'' should end with a semicolon. Otherwise, the engine doesn't know when one statement ends and the next begins. In SQF (unlike SQS), line breaks made with the ENTER key are only visible to the script writer. Spaces between lines are completely ignored by the engine.
+
* [[Code Best Practices]]
  
:There is an exception to this rule. When using commands such as [[if||if () then {}]], the final statement of code inside the parentheses and {} brackets can go without a semicolon. If this confuses you, just put a semicolon after all statements except for the last one.
+
=== Debugging ===
  
* The final statement of SQF code should not have a semicolon after it. (As of the current version of ArmA2:OA, this is not a problem, but it was in previous games).
+
* [[Debugging Techniques]]
 +
* [[:Category:Community_Tools#Debug_Console.2FSystem|Community Tools - Debug Console/System]]
  
At some point you may also find scripts written in the deprecated [[SQS syntax]]. This syntax was the primary syntax in [[Operation Flashpoint]], but is considered deprecated since Armed Assault.
+
=== Optimisation ===
  
All scripting pages about Armed Assault will focus on SQF syntax.
+
* [[Code Optimisation]]
 +
* [[Mission Optimisation]]
  
=== Layout ===
 
  
Code should be written in a specific '''layout'''. Complementary to the syntax, the layout assures that ''you and other coders'' can easily read the code. This is especially important when you haven't looked at your code for a long time and want to improve or change this code.
+
== Useful Links ==
  
* There should be '''only one [[Statement|statement]] per line''' in [[Script (File)|scripts]]. This rule doesn't apply to script lines in the mission editor, since there all the code has to be written within a single line.
+
These links offer a great deal of information about [[Scripting]]:
* Use '''spaces or tabs''' to indent code in [[Block|blocks]]. This way you can easily tell to which block some code belongs.
+
* [[:Category:Example Code|Example Code]]
 
 
Example:
 
 
 
Statement 1;
 
 
Block
 
{
 
    Statement 2;
 
 
    Nested block
 
    {
 
        Statement 3;
 
        Statement 4;
 
    };
 
};
 
 
 
=== Comments ===
 
 
 
You can and should write comments into your [[Script (File)|scripts]] that describe the purpose of your code. These comments are written in free text and completely ignored by the game engine.
 
 
 
Check out [[SQF syntax]] for information about the notation of comments.
 
 
 
'''Important:''' Don't write down what the code does, but rather what ''you'' want to do with the code. This is not as easy, but maybe the following example explains it a bit better:
 
 
 
Bad comment:
 
 
 
// the variable i gets the value 1
 
i = 1;
 
 
 
Good comment:
 
 
 
// reset the counter to start with 1 again
 
i = 1;
 
 
 
== Code Execution ==
 
 
 
how can I execute code? (external files vs. mission editor)
 
 
 
=== Mission Editor ===
 
 
 
how to execute code in the editor, listing of mission editor fields to start scripts
 
 
 
=== External Files ===
 
 
 
how to execute code in external files, scripts & functions
 
 
 
== Developing a Script ==
 
 
 
script in this case: code in external files (scripts/functions). how to develop a script?
 
 
 
* Requirements
 
* Concept
 
* Implementation
 
* Test
 
 
 
usually in your head, for complex scripts on paper and drafts
 
 
 
=== Requirements ===
 
 
 
what shall the script do?
 
 
 
=== Concept ===
 
 
 
How shall the script do it?
 
 
 
=== Implementation ===
 
 
 
Writing the code
 
 
 
=== Test ===
 
 
 
Testing the code
 
 
 
== See also ==
 
If you want to learn more about [[Scripting]], read the following articles:
 
 
 
* [[Variables]]
 
* [[Data Types]]
 
* [[Operators]]
 
 
* [[Control Structures]]
 
* [[Control Structures]]
 +
* [[Multiplayer Scripting]]
 
* [[Exception handling]]
 
* [[Exception handling]]
 
* [[Script (File)]]
 
* [[Script (File)]]
 
* [[Function]]
 
* [[Function]]
* [[SQF syntax]]
 
 
* [[SQS to SQF conversion]]
 
* [[SQS to SQF conversion]]
  
[[Category:Arma Scripting Tutorials|Introduction to Scripting]]
+
Additionally, the following are more resources for more general learning:
 +
* [[6thSense.eu/EG|6thSense.eu Editing Guide]]
 +
* [http://www.armaholic.com/page.php?id=20465 Fockers Arma 3 Scripting Guide]
 +
* [http://www.armaholic.com/page.php?id=4847 Mr-Murray's Armed Assault Editing Guide - Deluxe Edition]
 +
* [https://www.youtube.com/watch?v=WmEBN-RbK44 Excellent German SQF tutorial (YouTube)]
 +
 
 +
 
 +
[[Category:Scripting Topics]]
 +
[[Category:Operation Flashpoint: Editing]]
 +
[[Category:ArmA: Editing]]
 +
{{GameCategory|arma2|Editing}}
 +
{{GameCategory|arma3|Editing}}
 +
[[Category:Take On Helicopters: Editing]]

Latest revision as of 21:17, 2 August 2020

Introduction

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.

During mission editing and addon editing you may come across situations where actions or features you would like to have in your mission or addon cannot be accomplished using the basic (or even the more advanced) capabilities of the mission editor or within config files (in the case of addons). Some examples of this might be really cinematic cutscenes in missions or special animations for an addon.

The solution to this is to take advantage of the game-engine's ability to call on an even more advanced feature known as scripting. Arma's scripting language gives you a more direct control of core game features. With any combination of these commands and functions you can create custom processes that meet the specific needs of your mission or addon.


Before anything

Is your idea necessary?

  • Will players even notice or use what you want to script? Just because you can doesn’t mean you should. Sometimes less is more!

Is it possible to do this in the editor?

  • Eden Editor is an incredibly powerful tool and with it alone one can achive a lot of things, even without writing a single line of SQF.
  • Poorly written scripts are often the reason for poor performance both in singleplayer and multiplayer scenarios.

Can it be scripted using SQF?

  • This might be hard to answer, especially for beginners. 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 it is not possible in the end.


Depending on your answer for these questions, you may want to rethink your approach.

Scripting is not a solution to everything.


Terms

Before getting started, you should understand the meaning of these terms.

Data Types: See Data Types

Algorithm: In mathematics and computer science, an algorithm is an explicit specification of how to solve a class of problems. Algorithms can perform calculation, data processing and automated reasoning tasks.

Interpreter: Reads your code from a script file and translates it into instructions for you to achieve your desired outcome/effect in the game.

Control Structures: See Control Structures

Syntax:

Script: When speaking about a script, we usually mean a .sqs or .sqf file.

Game Engine: The core program of the game which reads and executes your scripting commands at run time.

Function: See Function

Variables: See Variables

Operators: See Operators


Recommended programs

Code Edition programs can be found on the Community Tools - Code Edition page section.


Must-read articles

Best Practices

Debugging

Optimisation


Useful Links

These links offer a great deal of information about Scripting:

Additionally, the following are more resources for more general learning: