MaHuJa/Sandbox – User
This article assumes you are at least slightly familiar with mission editing. For a large part, this presents a quick progression along the path. Whenever I introduce some new concept or command, I try to include a link to the wiki page for it. I recommend you try to figure out why I did what I did, and looking at the referenced pages will help a lot in understanding what it does in the first place.
Introduction to ArmAscript
Oneliners in the editor
The most basic form of scripting is the oneline scripts in various init fields, trigger on (de-)activation fields, and waypoint activation fields in the editor itself.
Example: Destroying a tank
A mission requires the player, and his two helpers, to get a tank parked a little behind the engagement area, and reinforce the front with it. However, as they approach the tank, it blows up. Now they have to find and punish the saboteurs. And perhaps go back to the front to help out in any way they can.
These steps can achieve that:
- Create the player squad
- Create the tank, name it "theTank".
- Create a couple saboteurs hiding in some bushes a couple hundred meters nearby - preferrably not with line of sight to the tank so that they could start shooting the player early.
- Create a trigger near to the tank (almost on top of) with both sizes set to 150. Set it to "west" "present". In the "on activation" field, type in the following:
theTank setdamage 1
Congratulations, you've just written your first script.
theTank is your first use of Variables - they are essential to pretty much everything you do. Also take a quick look at Data Types - theTank is of the object type.
The demonstration mission itself needs a few things more to be considered a complete mission, but which aren't important for this tutorial.
- Briefing, all of that
- Waypoints for the player group
- Notification of changed goals
- Victory condition triggers
- Perhaps an explosive charge beside the tank for realism.
Customized ammobox
Create an ammobox. (Optionally give it the name ammo_one) In its init line, fill in
clearweaponcargo this; clearmagazinecargo this; this addweaponcargo ["m16a2",2]; this addmagazinecargo ["30Rnd_556x45_Stanag",30]
You've just created an ammobox that will contain two m16 rifles and 30 magazines for them. For the "scripting names" of weapons and ammunition, see here. For unofficial addons, the names are usually listed in a readme.
Whenever you are editing an object, like an ammobox, this has the special meaning of "this particular object". If you write the same thing for an "ammo_two" the lines there will not change the content of ammo_one. This allows copy/paste to be used to write it once and paste it everywhere, and it will affect the object it has been copied to. Had you written
clearweaponcargo ammo_one
instead, you'd always be clearing the contents of that first crate unless you changed it every time.
Script files
There are two formats for script files, SQS and SQF.
Use SQF: SQS are deprecated and will stop functioning at some point in time.
Besides, SQF lends itself to readability and style.
Obviously, these oneliners are of limited use when you want big things to happen. That's when script files come into play. These files are put alongside the mission.sqm which the editor makes, and get packed into the final pbo that you would give to others. In this section, I'll introduce how to create a system to 'buy weapons'.
I leave the enemies and all those little details to you.
In order to have a place to save these files, you first need to save your mission with a name. Your next job is to find that mission. It's under My Documents/ArmA/Missions/missionname.islandname (e.g. My Documents/ArmA/Missions/ScriptTutorial.Sara) or My Documents/Arma Other Profiles/Playername/Missions/missionname.islandname (e.g. My Documents/ArmA Other Profiles/MaHuJa/Missions/ScriptTutorial.Sara)
In that directory, you will find a file called mission.sqm - this is the file produced by the mission editor, and contains the type, placement, name, init script, and so on for every object you've placed, every trigger, marker, and so on. Optionally you could create files like briefing.html and description.ext but that's beyond the scope of this tutorial.
The money and income
Create an empty file called init.sqf (Init.sqf is a special name, and will be run by the game as the mission starts) and open it - notepad will do fine. (MS Word will probably not.) Here's what to put into it:
money = 0;
We need to set the money value. Like if we don't set up a bank account before we put money into it, bad things happen.
execvm "earnmoney.sqf";
This will start a script called earnmoney.sqf - which we will create later. Execvm is more or less a synonym for spawn compile loadfile - and the script is runs may continue to run in the background.
removeallweapons player;
This thing will make no sense if the player starts with good enough weapons, will it? (That command also removes magazines.)
Now, create a new blank file called earnmoney.sqf and choose one of the following:
Alternative 1: Time is money
while {true} do { sleep 60; money = money + 50; hint format ["You now have %1 dollars.",money]; }
It uses while (which requires do), sleep hint and format functions.
Alternative 2: Travel is money
while {true} do { _pos = position player; sleep 60; _lump = player distance _pos; money = money + _lump/2; hint format ["You now have %1 dollars.",money]; }
The latter will force the player to move about, even though he is unarmed and there are enemies about. This also uses the position and distance functions, as well as the player constant. If you haven't read it already, it's time for Data Types too, seeing how the above not only uses numbers, but also the position data type. Please note the use of indentation, everything that's part of the loop.
Try to run it. It if works, you should see the money counter in the upper right updating every minute. If it shows an error, you probably made an error in copying it (or I messed up). If you typed it, common pitfalls are:
- while uses {} - if uses ()
- you missed a ; at the end of a command
Buying
I wish i knew how to make those pretty dialogs and all, but I don't. And they're anything but simple so they're not good for a tutorial like this. So I'm going to use actions - those menu items in the lower right of the screen.
First, let's introduce the arrays
Dump for later use
weapons = ["m16a2","m4","m4aim","m24"]; weaponscost = [100, 200, 250, 500]; stanag = "30Rnd_556x45_Stanag"; ammo = [stanag,stanag,stanag,"5Rnd_762x51_m24"];
[] execvm "moneytime.sqf"
Going over that line by line:
- money = 0; sets a variable called money to 0. Otherwise it would be nil, which has a meaning along the lines of "not a number".
- weapons = ["m16a2","m4","m4aim","m24"]; creates an array
I'm going to introduce you to a good habit: Gather all 'magic' values in one place.