Jump to content
Sign in to follow this  
rangoon

Rearming a pilot - testing scripts

Recommended Posts

I'm brand new to scripting, but have reviewed a lot of material, including Mr. Murray's guide to ArmA 1 scripting and the ArmA 2 SQF Scripting Guide by Taurus, among others (forum threads, the BIS WIKI, etc.), but cannot seem to get this to work.

I'm trying to create two Pilots to spawn next to an empty AH-64. I have tied several things, but here are the basics. I'm trying to get two scripts to rearm the two pilots with different weapons/ammo. The two SQF files read:

Pilot_01_Weapons.sqf:

_Pilot = _this select 0;

removeAllWeapons _Pilot;

_Pilot addMagazine 30Rnd_556x45_Stanag;
_Pilot addMagazine 30Rnd_556x45_Stanag;
_Pilot addMagazine 30Rnd_556x45_Stanag;
_Pilot addMagazine 30Rnd_556x45_Stanag;

_Pilot addMagazine 1Rnd_HE_M203;
_Pilot addMagazine 1Rnd_HE_M203;
_Pilot addMagazine 1Rnd_HE_M203;
_Pilot addMagazine 1Rnd_HE_M203;

_Pilot addWeapon M4A1_RCO_GL;

_Pilot addMagazine 15Rnd_9x19_M9;
_Pilot addMagazine 15Rnd_9x19_M9;

_Pilot addWeapon M9;

_Pilot addMagazine M136;

_Pilot addWeapon M136;

and Pilot_02_Weapons.sqf:

_Pilot = _this select 0;

removeAllWeapons _Pilot;

_Pilot addMagazine 200Rnd_556x45_M249;
_Pilot addMagazine 200Rnd_556x45_M249;

_Pilot addWeapon M249;

_Pilot addMagazine 15Rnd_9x19_M9;
_Pilot addMagazine 15Rnd_9x19_M9;

_Pilot addWeapon M9;

_Pilot addMagazine M136;

_Pilot addWeapon M136;

I know the loadouts are unrealistic, but am just playing around here for now and I think that technically it should work. I have tried naming each pilot Pilot_01 and Pilot_02, then using the actual global variables in the scripts. I have recently tried what is shown now, with the local variables. I have tried running the scripts in the Init line for each unit as:

nul = [this] execVM 'scripts\Pilot_01_Weapons.sqf'

(I am using a 'scripts' folder)

and:

nul = [] execVM 'scripts\Pilot_01_Weapons.sqf'

and the closest I have come is to simply have all weapons removed. I cannot get any weapons or ammo added.

I have also tried compiling the .sqf files first in the init.sqf file, then calling them as functions. I even tried calling them with a Radio Alpha trigger. I am trying to get this to work first of all in a basic MP COOP environment, but also a MP JIP environment.

Thanks for any suggestions! (Also, please don't hesitate to point out any general coding mistakes or bad form - I'm new to coding as well, which I'm sure is obvious.)

Share this post


Link to post
Share on other sites

Weapon and ammo classnames must be strings, i.e. they need to be surrounded by quotation marks.

Try what you tried when you managed to remove all the pilots' weapons, but instead, use this code:

Pilot_01_Weapons.sqf

_unit = _this select 0;

removeAllWeapons _unit;

{ _unit addMagazine "30Rnd_556x45_Stanag" } forEach [1, 2, 3, 4];
{ _unit addMagazine "1Rnd_HE_M203" } forEach [1, 2, 3, 4];
{ _unit addMagazine "15Rnd_9x19_M9" } forEach [1, 2];
_unit addMagazine M136;

_unit addWeapon M4A1_RCO_GL;
_unit addWeapon M9;
_unit addWeapon M136;

Pilot_02_Weapons.sqf

_unit = _this select 0;

removeAllWeapons _unit;

{ _unit addMagazine "200Rnd_556x45_M249" } forEach [1, 2];
{ _unit addMagazine "15Rnd_9x19_M9" } forEach [1, 2];
_unit addMagazine M136;

_unit addWeapon M249;
_unit addWeapon M9;
_unit addWeapon M136;

Yet another improvement would be to bring to scripts down from 2 to 1.

This could be done by using the parameters you've used at the beginning of each script.

For example:

Pilot 1 Init Line:

nul = [this, 1] execVM "scripts\pilotWeapons.sqf";

Pilot 2 Init Line:

nul = [this, 2] execVM "scripts\pilotWeapons.sqf";

scripts\pilotWeapons.sqf

_unit = _this select 0;

switch (_this select 1) do {
case 1: {
	removeAllWeapons _unit;

	{ _unit addMagazine "30Rnd_556x45_Stanag" } forEach [1, 2, 3, 4];
	{ _unit addMagazine "1Rnd_HE_M203" } forEach [1, 2, 3, 4];
	{ _unit addMagazine "15Rnd_9x19_M9" } forEach [1, 2];
	_unit addMagazine M136;

	_unit addWeapon M4A1_RCO_GL;
	_unit addWeapon M9;
	_unit addWeapon M136;
};
case 2: {
	removeAllWeapons _unit;

	{ _unit addMagazine "200Rnd_556x45_M249" } forEach [1, 2];
	{ _unit addMagazine "15Rnd_9x19_M9" } forEach [1, 2];
	_unit addMagazine M136;

	_unit addWeapon M249;
	_unit addWeapon M9;
	_unit addWeapon M136;
};
};

Edited by Fuzzy Bandit
Adding more options...

Share this post


Link to post
Share on other sites

Thanks very much - I will give this a try in a few hours.

Share this post


Link to post
Share on other sites

@Fuzzy Bandit

lol, you forgot the quotation marks around all weapon classnames and on the M136 magazine classname. ;)

And for completeness, here another variant of the script:

private ["_magazines", "_weapons"];
_unit = _this select 0;

switch (_this select 1) do {
case 1: {
	_magazines = ["30Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_Stanag", "1Rnd_HE_M203","1Rnd_HE_M203","1Rnd_HE_M203","1Rnd_HE_M203","15Rnd_9x19_M9","15Rnd_9x19_M9","M136"];
	_weapons = ["M4A1_RCO_GL", "M9", "M136"];
};
case 2: {
	_magazines = ["200Rnd_556x45_M249","200Rnd_556x45_M249","15Rnd_9x19_M9","15Rnd_9x19_M9","M136"];
	_weapons = ["M249", "M9", "M136"];
};
};
removeAllWeapons _unit;
{_unit addMagazine _x} forEach _magazines;
{_unit addWeapon _x} forEach _weapons;

Share this post


Link to post
Share on other sites

Thanks, you guys. Can't wait to try this out. Just a couple more hours...

However - can you please explain the use of "private" in the beginning?

Thanks!

Edited by rangoon

Share this post


Link to post
Share on other sites

I just released a relevant script that should make this stuff pretty easy:

addWeapon script

Just make the pilots execute the script with

removeAllWeapons pilot1name;[pilot1name,["M4A1_RCO_GL",4,0,4],["M9",2],["M136",1]] exec "cly_addweapon.sqs"

and

removeAllWeapons pilot2name;[pilot2name,["M249",2],["M9",2],["M136",1]] exec "cly_addweapon.sqs"

Share this post


Link to post
Share on other sites

Nice - I'll check that out as well!

EDIT: this stuff is working like a charm. Thanks again guys.

Edited by rangoon

Share this post


Link to post
Share on other sites

Well this is odd. I have now added another pair of units (pilots) who I would also like to have the same weapon loadouts (1 and 2 from above). For some reason, now that there are four units, in two teams, only the team that is not my own will have the new weapons. My Player unit and my squad mate have the default loadout.

Any idea what is happening here? I have the same two init lines for the units - one choosing "case 1" and the other choosing "case 2."

Share this post


Link to post
Share on other sites

This is a script I created for a rearming station at the end of a runway that identifies the vehicle and reloads it based on it's type. It doesn't have the finese of the above scripts but it does work perfectly. I create the vehicles in game and from the creation script I execute this script. It is a looping script that runs on the client machines. (I should note that this is for a Multiplayer Map) The _type and _veh are defined in the creation script. _Type is just a number I set the type of vehicle equal to and _veh is the name of the vehicle that was created with this line

_veh = "A10" createVehicle (getPos planebuild1);

Here is the script execution line:

[_veh,_type] exec "repair.sqs"

Here is the script itself sorry for the SQS but it is all I know:

_Veh = _this select 0
_type= _this select 1

#start
~1
?!(alive _veh): goto "exit"
?((player distance repair)<=50): goto "Type"
goto "Start"

#Type
?(_type==1):goto "A10J"
?(_type==2):goto "F35J"
?(_type==3):goto "AV8B2J"
?(_type==4):goto "AV8BJ"
?(_type==5):goto "SU34J"
?(_type==6):goto "SU39J"
hint "Type not Found"
~1
goto "start"

#A10J
?!(alive _veh): goto "exit"
_veh removemagazines "1350Rnd_30mmAP_A10"
_veh removemagazines "14Rnd_FFAR"
hint "Rearming
~1
_veh addmagazine "1350Rnd_30mmAP_A10"
_veh addmagazine "1350Rnd_30mmAP_A10"
~1
_veh addmagazine "14Rnd_FFAR"
~1
_veh addmagazine "14Rnd_FFAR"
~1
_veh addmagazine "14Rnd_FFAR"
~1
_veh addmagazine "14Rnd_FFAR"
goto "Repair"

#F35J
?!(alive _veh): goto "exit"
_veh removemagazines "300Rnd_25mm_GAU12"
hint "Rearming"
~3
_veh addmagazine "300Rnd_25mm_GAU12"
~1
_veh addmagazine "300Rnd_25mm_GAU12"
~1
_veh addmagazine "300Rnd_25mm_GAU12"
Goto "Repair"

#AV8B2J
?!(alive _veh): goto "exit"
_Veh removemagazines "300Rnd_25mm_GAU12"
_Veh removemagazines "14Rnd_FFAR"
hint "Rearming"
~2
_Veh addmagazine "14Rnd_FFAR"
~1
_Veh addmagazine "14Rnd_FFAR"
~1
_Veh addmagazine "300Rnd_25mm_GAU12"	
~1
_Veh addmagazine "300Rnd_25mm_GAU12"	
goto "Repair"

#AV8BJ
?!(alive _veh): goto "exit"
_Veh removemagazines "300Rnd_25mm_GAU12"
hint "Rearming"
~1	
_Veh addmagazine "300Rnd_25mm_GAU12"
~1
_Veh addmagazine "300Rnd_25mm_GAU12"
~1
_Veh addmagazine "300Rnd_25mm_GAU12"
~1
_Veh addmagazine "300Rnd_25mm_GAU12"
~1
_Veh addmagazine "300Rnd_25mm_GAU12"
goto "Repair"

#SU34J
?!(alive _veh): goto "exit"
_Veh removemagazines "180Rnd_30mm_GSh301"
_Veh removemagazines "40Rnd_S8T"
hint "Rearming"
~1	
_Veh addmagazine "180Rnd_30mm_GSh301"
_Veh addmagazine "180Rnd_30mm_GSh301"
~1
_Veh addmagazine "180Rnd_30mm_GSh301"
_Veh addmagazine "180Rnd_30mm_GSh301"
~1
_Veh addmagazine "40Rnd_S8T"
_Veh addmagazine "40Rnd_S8T"
~1
_Veh addmagazine "40Rnd_S8T"
~1
_Veh addmagazine "40Rnd_S8T"
goto "Repair"

#SU39J
?!(alive _veh): goto "exit"
_Veh removemagazines "180Rnd_30mm_GSh301"
_Veh removemagazines "80Rnd_S8T"
hint "Rearming"
~1
_Veh addmagazine "180Rnd_30mm_GSh301"
~1	
_Veh addmagazine "180Rnd_30mm_GSh301"
~1
_Veh addmagazine "180Rnd_30mm_GSh301"
~2	
_Veh addmagazine "80Rnd_S8T"	
goto "Repair"


#Repair
?!(alive _veh): goto "exit"
?!((player distance repair)<=50): goto "start"
Hint "Repairing"
~5
_Veh setdamage 0

?!(alive _veh): goto "exit"
?!((player distance repair)<=50): goto "start"
Hint "Refueling"
~5
_Veh setfuel 1
Hint "Complete"
goto "Check"

#check
~1 
?((player distance repair)>50): goto "start"
goto "check"

#exit
exit

If you don't want to set it up the way I did you can use this line instead of the name of the vehicle and it will work as well

_veh = vehicle player

Even if it doesn't help you specifically I wanted it to be here so others can see a different (maybe even noobish) way of doing it. It does work though and that is all that counts

Edited by Sardaukar17
needed to mention script is for a multiplayer map

Share this post


Link to post
Share on other sites
Thanks, you guys. Can't wait to try this out. Just a couple more hours...

However - can you please explain the use of "private" in the beginning?

Thanks!

The private command "assigns" those variables to this scope.

Hint "Scope 0";
if (true) then {
   hint "scope 1";
   if (true) then {
       hint "Scope 2";
   };
};

There you see roughly what a "scope" is. Since _magazines and _weapons are defined in scope 1, they would be empty in the innermost scope. To avoid this, i assigned them to the respective scope, else tha addweapon and addmagazine commands wouldn't work.

About the latest problem, please post exactly the script calls. The more we know, the easier bughunting is.

@Sardaukar17

Your script is good but not what the OP requested. Also, try to convert it to .sqf and make it more generic. Avoid pre-define plane types, read magazine and weapon loadout out from the config. This will make sure it will work with all planes, BIS default aswell as addons.

Share this post


Link to post
Share on other sites
The private command "assigns" those variables to this scope.

Since _magazines and _weapons are defined in scope 1, they would be empty in the innermost scope. To avoid this, i assigned them to the respective scope, else tha addweapon and addmagazine commands wouldn't work.

Okay - I did read up in the BIS WIKI on "private" but couldn't quite see what it was doing in my case. So a simple local variable (_magazines for example) would not have been sufficient because it was being defined outside the scope of its use at the innermost scope? I know it is being used in the last few lines of code, and I assume if that was the only place a simple local variable would be enough, right? But since we're going a couple layers deeper to define them, now we need "private"? So "private" will allow the variable to be used anywhere within this script so long as it's either at the same level as where it's defined or narrower/deeper? Whereas a local variable would lose its functionality in a narrower scope than where it's defined? And in either case (local or private) it protects the variable from being used again outside the script where it's being used unlike a global variable?

About the latest problem, please post exactly the script calls. The more we know, the easier bughunting is.

Absolutely. Here again is the script (scripts\pilotWeapons.sqf):

private ["_magazines", "_weapons"];
_unit = _this select 0;

switch (_this select 1) do
{
case 1: {
	_magazines =
		[
		"30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag",
		"M136", "1Rnd_HE_M203", "1Rnd_HE_M203", "1Rnd_HE_M203", "1Rnd_HE_M203", "1Rnd_HE_M203", "1Rnd_HE_M203", "15Rnd_9x19_M9", "15Rnd_9x19_M9"
		];
	_weapons = ["M4A1_RCO_GL", "M9", "M136", "Binocular", "ItemGPS", "NVGoggles"];
	};
case 2: {
	_magazines = 
		[
		"HandGrenade_West", "HandGrenade_West", "200Rnd_556x45_M249", "200Rnd_556x45_M249", "200Rnd_556x45_M249", "200Rnd_556x45_M249", "200Rnd_556x45_M249", 
		"15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9", "15Rnd_9x19_M9"
		];
	_weapons = ["M249", "M9", "Binocular", "ItemGPS", "NVGoggles"];
	};
};
removeAllWeapons _unit;
{_unit addMagazine _x} forEach _magazines;
{_unit addWeapon _x} forEach _weapons;

And here are the four calls. The first two are for Team Alpha, comprised of two Pilots - one Player and the other Playable. The second two are Team Bravo - both Playable.

1-1-A 1:

nul = [this, 1] execVM "scripts\pilotWeapons.sqf"

1-1-A 2:

nul = [this, 2] execVM "scripts\pilotWeapons.sqf"

1-1-B 1:

nul = [this, 1] execVM "scripts\pilotWeapons.sqf"

1-1-B 2:

nul = [this, 2] execVM "scripts\pilotWeapons.sqf"

In my testing, everything was working until I added the second team. Now, if I disable AI on the second team, it still doesn't work. When I leave AI enabled for all four and occupy one spot myself, the weapons switch for the team I am not part of, but do not switch for my team (including me the player). So it's still working, but only for the full AI team and it doesn't matter which team I belong to - it only works for the other team.

For what it's worth, I also tried making the cases 1 || 3 (1 "or" 3 as I understand) and 2 || 4. That just caused errors. Then I tried copying the code for two more cases (3 and 4) which were the same as 1 and 2, then tried specific calls using 1, 2, 3, and 4. That had the same problematic results. So I went back to the original, now assuming it has something to do with sequencing, AI, etc.

Another side question I have is: does it matter whether I use single quotes or double quotes in any case - whether calls or the code itself? Is there any logic to using one or the other?

Thanks for the help!

Share this post


Link to post
Share on other sites

Hmmm...it's odd...tried to reproduce the error but on my side it works perfectly. I have 2 groups, one with player unit. In each unit in the initline i have the scriptcall like you have it. And it works. Each unit gets the desired loadout.

About the "private" command, right now it is sufficient for you to know that defining those at the beginning of the script will do it for your skill level. No offence, i just suggest that you focus on actual real problems. The knowledge of advanced use of "private" will come the day you'll really need it. I just try to avoid too much confusion where it isn't needed.

Same goes for the quotation marks. Right now it is sufficient to know that almost always the doublemarks are used. The others are used sometimes in addon configs so this doesn't matter right now.

Share this post


Link to post
Share on other sites

Okay, thanks Myke. Here is the problem I think. I have been playing/testing everything in multiplayer. I just exported the mission to SP missions and tested - everything is fine. Clearly there is something I need to do differently for MP missions. Apologies for not making that clearer from the start.

I know there are other considerations to be made, especially for JIP MP. I would like to make this as robust as possible and get into proper habits for making JIP MP missions. However this mission in particular will be used for cooperative MP, non-JIP, if that makes the difference between me getting in over my head or not.

Still, if there are certain things which I can do to make this JIP-friendly I'd like to go that route.

And no offense taken on the private and quote issues. I just want to make sure I'm building good habits - even if I don't understand why until later. I hate being wasteful/inefficient but it'll take some time before I'm competent with all of this.

Share this post


Link to post
Share on other sites

I just finished reading 6thSense.eu's view on multiplayer scripting and I think it has something to do with the sequence of inits happening, but I can't yet sort out what.

I am the server, and I am the only client, so it doesn't (at least yet) seem to be due to locality.

I suppose I could try putting something in the init.sqf file, since it initializes last, but I have a feeling I need to learn about Event Handlers. Taurus' guide made them seem important, but I didn't quite understand how to use them while going through the guide.

Thanks again for any pointers.

Share this post


Link to post
Share on other sites
@Sardaukar17

Your script is good but not what the OP requested. Also, try to convert it to .sqf and make it more generic. Avoid pre-define plane types, read magazine and weapon loadout out from the config. This will make sure it will work with all planes, BIS default aswell as addons.

Dude if I knew how to do that I would have lol. Just trying to be helpful in what way I can.

Share this post


Link to post
Share on other sites
Dude if I knew how to do that I would have lol. Just trying to be helpful in what way I can.

Did you tried the one I sent you?

Share this post


Link to post
Share on other sites
Dude if I knew how to do that I would have lol. Just trying to be helpful in what way I can.

To read out the default armament (same as it has when placed in editor):

_veh = _this select 0;
if (_veh iskindof "Air") then {
   _mags = getArray (configFile >> "cfgVehicles" >> (typeof _veh) >> "magazines");
   _weps = getArray (configFile >> "cfgVehicles" >> (typeof _veh) >> "weapons");
};

Share this post


Link to post
Share on other sites

@Wickedstigma your awesome but No I can´t yet. In transition from one place to another and don´t have my main comp setup yet but I will don´t worry. I´ll use what you sent me to start learning SQF plus I will finally have to sit down and read the SQF tutorials. Lol and I was starting to get good at the SQS stuff. We´ll call it a good start lol

@Myke Thank you sir. I´ve been doing really long work arounds for what you have there.

Share this post


Link to post
Share on other sites

@Sardaukar17 I see. And don't worry if you were getting really good with SQS you will learn SQF fast. Keep up the good work.

Share this post


Link to post
Share on other sites

Can anyone please point me in the right direction for getting my problem sorted out for multiplayer? I'm just trying to get the pilotWeapons.sqf script to work properly for all - not just non-player teams.

Even just a hint would be appreciated. I'm at a loss.

Thanks!

Share this post


Link to post
Share on other sites

Well if it isn´t obvious I am no pro but If it is only running your script on the AI units if could be a locality problem in that if it is only running on the server it might make sense.. that it wouldn´t fire the script on a client machine. If you are the host though that makes you the server.

Have you tested with the

? isServer : exit

line just to see how it reacts? or ?! isServer just to see how it behaves. Might tell you more on if it is a locality issue.

I would also think about trying executing the scripts with the unit name instead of [this] it may just be me but it seems it sometimes just gets confused. thats all I got. Hope I helped

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×