Home   Help Search Login Register  

Author Topic: Selecting Primary after spawn not working  (Read 4902 times)

0 Members and 1 Guest are viewing this topic.

Offline desertjedi

  • Members
  • *
Selecting Primary after spawn not working
« on: 08 Apr 2009, 22:03:54 »
There are two fairly sophisticated co-op missions I play that share the same problem even though they are completely unrelated. They are not quite as complex as Evo/Dom. but more complex than your average mission.

They both share the same problem which makes me wonder if this command really works or not. Each mission has a script to equip the player after respawn. Here is one of the two scripts from a mission that uses "revive":

Code: [Select]
if (!(local player)) exitWith {};

waitUntil {alive player};

while {true} do
{
waitUntil {!alive player};
_weapons = weapons player;
_magazines = magazines player;
waitUntil {alive player};
_p = player;
removeAllWeapons _p;
{_p addMagazine _x;} forEach _magazines;
{_p addWeapon _x;} forEach _weapons;
_p selectWeapon (primaryWeapon _p);
};

if (true) exitWith {};

The other mission has a very similar script except that it checks the player's weapon count. If it's greater than zero, it knows to restore the player's current kit. Here is that excerpt:

Code: [Select]
if (!local player) exitwith {};

_player = player;
_weapons = weapons player;
_magazines = magazines player;
_playerGrp = count (units group player);

if (count _weapons != 0) then
{
waituntil {alive player};
removeallweapons player;
{player addmagazine _x;} foreach _magazines;
{player addweapon _x;} foreach _weapons;
player selectweapon (primaryWeapon player);
}
else
{
waituntil {alive player};
removeallweapons player;
player addWeapon "NVGoggles";
player addWeapon "Binocular";
player addMagazine "M136";
player addMagazine "M136";
player addMagazine "M136";
player addWeapon "M136";
for "_i" from 0 to 3 do
{
player addMagazine "15Rnd_9x19_M9SD";
};
for "_i" from 0 to 3 do
{
player addMagazine "1Rnd_HE_M203";
};
player addWeapon "M9SD";
for "_i" from 0 to 5 do
{
player addMagazine "30Rnd_556x45_Stanag";
};
player addWeapon "M4A1GL";
player selectweapon (primaryWeapon player);
};
.
.
.

After respawning in either mission, the player ends up holding his pistol and not his primary weapon.

Anyone have any suggestions regarding this? In one mission, you respawn to a chosen spawn point and it may have enemies in close vicinity. I'm not exactly sure what is happening but the player goes through a lot of animation gyrations upon respawn. I'd at least like to start off with the right gun in my hand.

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Selecting Primary after spawn not working
« Reply #1 on: 08 Apr 2009, 22:49:05 »
Have you tried adding the primary weapon before the handgun in the script?


Planck
I know a little about a lot, and a lot about a little.

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: Selecting Primary after spawn not working
« Reply #2 on: 09 Apr 2009, 09:26:10 »
Despite its name selectWeapon expects a muzzle name, not a weapon name.

Both are the same for most weapons but not for all. So the "player selectWeapon (primaryWeapon player)" will fail for some weapons (e.g. rifles with grenade launchers). Muzzle names can be found on this page.

Instead of hardcoding muzzle names you could try something like this:
Code: [Select]
// NOTE: This code expects that the muzzle name of weapons with a single muzzle
//       match the weapon's name! This is the case for all weapons included in
//       vanilla ArmA.
_muzzles = getArray(configFile >> "CfgWeapons" >> (primaryWeapon player) >> "muzzles");
_muzzleName = if (count _muzzles > 1) then { _muzzles select 0 } else { primaryWeapon player };
player selectWeapon _muzzleName;
try { return true; } finally { return false; }

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Selecting Primary after spawn not working
« Reply #3 on: 09 Apr 2009, 16:00:05 »
Or even this page.


Planck
I know a little about a lot, and a lot about a little.

Offline desertjedi

  • Members
  • *
Re: Selecting Primary after spawn not working
« Reply #4 on: 09 Apr 2009, 16:53:20 »
I tried three different modifications to the script to get the player to select his primary (M4A1GL). They all involved equipping the M4A1GL before the M9. On one attempt, I even moved the selectWeapon command up higher before the M9 assignment.

All 3 attempts had the same bizarre result: I spawned with the M4A1GL but it had the wrong reticle, would not fire, and could not be reloaded. I had to switch to another weapon and then back again to make the M4 functional. Obviously, there's a lot going on here about which I have no clue.

I've been playing Arma for six months now but have been too embarrassed to ask...what in the name of heaven is a muzzle??? Even that wiki page doesn't answer that question. It simply sounds like a variable that determines whether or not a GL kit is involved. I would have thought that the silencer on an M9 would be a muzzle but instead it's a whole different weapon class. WTH?

Thanks for the code but, unfortunately, I can't understand its exact "meaning". Line 1 forms an array. Line 2 assigns some value to _muzzleName and Line 3 has the player select the weapon determined in Line 2. That's all I got. But I will throw it up in the editor and can probably "back into" its meaning. Thanks.

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: Selecting Primary after spawn not working
« Reply #5 on: 09 Apr 2009, 17:28:14 »
Thanks for the code but, unfortunately, I can't understand its exact "meaning". Line 1 forms an array. Line 2 assigns some value to _muzzleName and Line 3 has the player select the weapon determined in Line 2. That's all I got. But I will throw it up in the editor and can probably "back into" its meaning. Thanks.

Code: [Select]
_muzzles = getArray(configFile >> "CfgWeapons" >> (primaryWeapon player) >> "muzzles");Creates an array of the muzzles of the player's primary weapon (from the CfgWeapons file).  i.e. if you have a gun with a GL, this will have both muzzle names.

Code: [Select]
_muzzleName = if (count _muzzles > 1) then { _muzzles select 0 } else { primaryWeapon player };Counts the muzzles.  If there is more than one, then we select the first one (index 0), otherwise we get the primary weapon (if it only has one muzzle, it's weapon name is the same as the muzzle name).  Selecting the first muzzle means that it'll select the gun itself above the attached GL (provided all the configs for the weapons are set up properly... which they are :)).

Code: [Select]
player selectWeapon _muzzleName;Selects the correct weapon that we've discovered by the line above, for the player.


As for 'muzzle', according to Wikipedia:
Quote
The muzzle of a firearm is the end of the barrel from which the projectile will exit.
In ArmA terms, this is basically just the number of barrels the current weapon has.  Usually this is just the main gun and the GL, as you said.
« Last Edit: 09 Apr 2009, 17:29:46 by JamesF1 »

Offline desertjedi

  • Members
  • *
Re: Selecting Primary after spawn not working
« Reply #6 on: 09 Apr 2009, 18:07:02 »
Thanks for the explanation! I actually understood a lot more than I thought but was afraid of making a fool of myself. I got it all except for why the first array entry was assigned when the weapon had multiple muzzles. That makes sense based on the order of muzzles in the array.

Now I will go play with that and see if I can get it to work in those missions. Thanks Worldeater et al.


Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: Selecting Primary after spawn not working
« Reply #7 on: 09 Apr 2009, 20:30:17 »
In theory the script could use the first muzzle name for all weapons (as in: I don't care how many muzzles this darn gun has or what the first muzzle is. Just gimme any muzzle of the primary weapon so I can select it!).

The problem is that the first muzzle of single-muzzle-weapons is "this". So for these weapons an invalid muzzle name is returned. That's why the script has to treat them differently. Luckily in this case it can simply use the weapon's name instead.
try { return true; } finally { return false; }

Offline desertjedi

  • Members
  • *
Re: Selecting Primary after spawn not working
« Reply #8 on: 10 Apr 2009, 05:38:14 »
I was able to get the script working in both missions...WOOT!!!  :clap:

Offline Ext3rmin4tor

  • Members
  • *
Re: Selecting Primary after spawn not working
« Reply #9 on: 10 Apr 2009, 10:04:19 »
I have a related problem. I wrote a script to set the equipment for a unit or a group of units. Here's the code:

Code: [Select]
/* USAGE: [[weapon array], [magazine array], unit, mode] execVM "equip.sqf"
The script equips a unit with the weapons specified in the weapon array, add
all the magazines in the magazine array. Each entry in the magazine array
is another array [classname, amount] (then "magazine array" is a matrix)
where "classname" is the classname of the magazine you want to add and "amount"
is the number of magazines.
So let's say you want to add an AK74 and a RPG7V to your unit with 6 magazines
for the AK74 and 3 rockets, the call will be the following (for the unit init
field):

call{[["AK74", "RPG7V"],[["30Rnd_545x39_AK", 6], ["PG7", 3]], this, false] execVM "equip.sqf"}

If "mode" is set to true then every
unit will be equiped with the given weapons and magazines. Note that the
script removes every weapon from the unit (or the whole group depending on
the selected mode). */

_weapons = _this select 0;
_magazines = _this select 1;
_unit = _this select 2;
_wholeGroup = _this select 3;

//equip the whole group
if (_wholeGroup) then
{
_unitGroup = units group _unit;

//pick every unit in the group and add the equipment
{
removeAllWeapons _x;
_temp = _x;
{_temp addWeapon _x} forEach _weapons;

{

for [{_i = 0}, {_i < (_x select 1)}, {_i = _i + 1}] do
{
_temp addMagazine(_x select 0);
};
}
forEach _magazines;

_x selectWeapon(primaryWeapon _x);
reload _x;

}
forEach _unitGroup;
}

//equip a single unit only
else
{
removeAllWeapons _unit;
{_unit addWeapon _x} forEach _weapons;

{

for [{_i = 0}, {_i < (_x select 1)}, {_i = _i + 1}] do
{
_unit addMagazine(_x select 0);
};
}
forEach _magazines;

_unit selectWeapon(primaryWeapon _unit);
reload _unit;
};

The problem is that I don't want the player to reload the weapon, but to start with the weapon already loaded and ready to fire (basically I don't want to see the reloading animation but to instantly have a magazine loaded). Is there a way to do that?
How can you shoot women and children?
Easy! Ya' just don't lead'em so much! Ain't war hell?!!

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: Selecting Primary after spawn not working
« Reply #10 on: 10 Apr 2009, 10:37:51 »
Yes, add the magazines first then add the weapons.
try { return true; } finally { return false; }

Offline desertjedi

  • Members
  • *
Re: Selecting Primary after spawn not working
« Reply #11 on: 13 Apr 2009, 20:13:53 »
Hmm, this seems to be quite a common problem. I just found another "complex-script" mission that sticks a pistol in your hand when you respawn!  :dunno:

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Selecting Primary after spawn not working
« Reply #12 on: 14 Apr 2009, 01:29:50 »
SPON Kits has done this perfectly well for a long time. Can't suggest anything more, but I realize everyone likes to reinvent the wheel :whistle:
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline JamesF1

  • Editors Depot Staff
  • *****
    • JamesBurgess.co.uk
Re: Selecting Primary after spawn not working
« Reply #13 on: 14 Apr 2009, 01:32:11 »
It's so hard for you, being the solution to everyone's problem, isn't it? :D

#EDIT: Don't quote the entire previous post you're replying to..  h-
« Last Edit: 14 Apr 2009, 13:46:07 by h- »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Selecting Primary after spawn not working
« Reply #14 on: 14 Apr 2009, 01:40:57 »
Yeah, sorry, I just get bored answering the same questions over and over. Forgive me for my flipancy ::)

Still, I realise that SPON Kits implements a particular system of kits which might not suit everyone (intended for respawn with the same kit, whereas I know some people like respawn with the equipment you died with). Either way, it is either directly useful or you can look at it to see some of the techniques I use to ensure correct loadout, assuming you want a different system.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)