Sunday 6 October 2013

Automatic Gear Box - Part 1

EDIT: The original if statement coding (name player == "unitname") has been changed to (IsPlayer unitname) this is because of a slight update to the sqf format in ArmA III that results in name player now returning the actually players profile name as opposed to the unit name, sorry for any inconvenience this may have caused

Sometimes during a scenario, you will want certain soldiers to be equipped with specific gear, i.e, you don't want every player choosing that lovely .50 Cal sniper. An SQF script can be created in order to gear up, say, a sniper with long range gear, and a grenadier with an under barreled grenade-launcher.

Before anything else, this tutorial is quite long to write with the detail needed to understand every aspect, for this reason the tutorial will be split into parts. This part will explain the main script, the second part will go into the event handler and animations and the third and final part will go into putting it all together and adding it to your mission.

First things first, we are going to need a basic squad to work with, by placing a small rifle squad (from groups, the faction of your choice, then rifle squad). It is very important that you give them each soldier a unique name, you can do this by typing in the editbox above the INIT line here:
 For the purpose of this script. let's name the soldiers based on their role:

s_marksman
s_rifleman
s_spot
s_medic
s_auto
s_engineer

These names can be changed to anything, just remember to change the names that the script references. it is worth noting, that you don't have to place a sniper unit to call it s_sniper or a rifleman for s_rifleman. The script will provide the user with the correct gear regardless of starting role.

So now we can begin to write our script to apply the gear we want onto characters. This will recall some of the commands explained in previous tutorials such as addWeapon and removeVest.

We will start by opening Notepad or any other text editor. We can treat Notepad like the INIT line of a unit a just type code directly into it. However, unlike the INIT line, it will not give you the options available for a command or give you immediate error reports, it is important that you take your time and check your code for errors. Now that we have that out of the way, let's get onto the actual code. We will be using a simple if statement. They look like this:

if (staement to be checked)
     if statement is true
     {
           Do this
     } 
     else
     {
          Do this
     }

We can use this statement to check if the unit is being played and perform the action on the player that has undertaken that role. The if statement we will be using is this:

if (IsPlayer s_marksman)
     {
           Do this
     }

We do not need an else loop as there is nothing to be performed if the unit is not being played. So now let's break down this loop.

IsPlayer is a command that will check if the unit, in this case s_marksman, is actually a player unit. If the value is returned as true, then our if statement will be executed, otherwise it will be ignored.

In short, this command will check if the player name is equal to s_marksman and if it is, perform the code segment within the curly braces {}.

"{}" - These are know as curly braces, they denote the start and end of code segments, in this case, our if statement-true coding.

Now for the Do This section. We need to think of the basic things a unit has, and needs. As we are removing all the gear first, we will need to re-add maps, compasses, NVG's etc.

So we will need to remove all pieces of gear currently equipped, this can be done using the remove command showed in earlier tutorials:

removeHeadgear s_marksman;
removeVest s_marksman;
removeGoggles s_marksman;
removeBackpack s_marksman;
removeAllWeapons s_marskman;
removeUniform s_marksman;

This has cleared all the gear from the s_marksman soldier and now he is ready to be kitted out, this should be placed in between the curly braces of the if statement. Now we can begin to kit out our marksman with all the gear he needs.

s_marksman addHeadgear "Classname";
s_marksman addUniform "Classname";
s_marksman addVest "Classname";
s_marksman addBackpack "Classname";
s_marksman addGoggles "Classname";

This will add the headgear, uniform, vest, backpack and goggles to our marksman. "Classname" will be replaced by the classnames used for the item we want, for example, "G_Shades_Black" for a set of lovely black sunglasses. Now for his weapons:

 s_marksman addMagazine "Classname";
 s_marksman addWeapon "Classname";
 s_marksman addPrimaryWeaponItem "Classname";


Here we are adding the magazine, weapons and an attahcment (such as a silencer or scope). However, it is very important to note that the magazine must be added before the weapon, if not, then the gun will be empty when the player receives it! Again, the "Classname" will be replaced by an item classname such as "muzzle_snds_B".

Time to add those crucial items needed for battle. This can be used by a single line of code:

s_marksman addItem "Classname"

The item to be added depends on the "Classname" field, for example, "nvgoggles" for night vision goggles. NVG's on demand!

Now that we have all the code for s_marksman, we can simply copy it for all the other units, changing the name player == "unit name" segment and the objects!

There is another line of code that we will be using, this code will remove the gearbox used for re-arming, off the map after we are done with it.

deleteVehicle name;

This line of code does exactly what is says on the tin, it will delete a vehicle (vehicles being objects in most scenarios) of the name entered. We can replace name with the name of our gearbox in order to remove it from the world after play.

So that is the main script completed, with the exception of the animation. So here is all the coding done so far:
_________________________________________________________________
If statement
if (statement to check)
is true then
{
      Do This;
 
else
{
      Do This;
}

Player Check Statement
 Isplayer unitname

If true re-arm coding:

-Gear Removal-
removeHeadger "Classname";
removeVest "Classname";
removeGoggles "Classname";
removeBackpack "Classname";
removeAllWeapons "Classname";
removeUniform "Classname";

-Gear Addition-
unit name addHeadgear "Classname";
unit name addUniform "Classname";
unit name addVest "Classname";
unit name addBackpack "Classname";  
unit name addGoggles "Classname";

-Weapon and Item Addition-
unit name addMagazine "Classname";
unit name addWeapon "Classname";
unit name addPrimaryWeaponItem "Classname";
(unit name addSecondaryWeaponItem "Classname") 
- This can be used to add attachments to sidearms
unit name addItem "Classname";

-GearBox Removal-
deleteVehicle name;

Here is a full segment from a script I used so you can see what it will look like when fully implemented

  
if (IsPlayer s_marksman) then
{
    removeHeadgear s_marksman;
    removeVest s_marksman;
    removeGoggles s_marksman;
    removeBackpack s_marksman;
    removeAllWeapons s_marksman;
    removeUniform s_marksman;
  
    s_marksman addheadgear "H_Watchcap_camo";
    s_marksman addUniform "U_I_CombatUniform";
    s_marksman addVest "V_PlateCarrierIA1_dgtl";
    s_marksman addBackpack "B_AssaultPack_ocamo";
    s_marksman addGoggles "G_Shades_Black";

    s_marksman addMagazine "20Rnd_762x51_Mag";
    s_marksman addMagazine "20Rnd_762x51_Mag";
    s_marksman addMagazine "20Rnd_762x51_Mag";
    s_marksman addMagazine "20Rnd_762x51_Mag";
    s_marksman addMagazine "20Rnd_762x51_Mag";
    s_marksman addWeapon "srifle_EBR_MRCO_pointer_F";
    s_marksman addPrimaryWeaponItem "muzzle_snds_B";
    s_marksman addMagazine "16Rnd_9x21_Mag";
    s_marksman addMagazine "16Rnd_9x21_Mag";
    s_marksman addWeapon "hgun_P07_snds_F";
};


deleteVehicle Box1;

The player unit would look like this after the code has been executed:

__________________________________________________________________
That is how to create a simple if statement that will allow us to automatically re-arm a unit and then remove the box. In part 2 we will be looking at adding the animation and event handler using a rudimentary coding method within the INIT line. Part 3 will detail a much cleaner method of calling our code.



No comments:

Post a Comment