Jump to content
cupcake_actual

Check if player has any weapon or is gunner

Recommended Posts

I need to check if a player has any weapon. I've got something like this, but its not working.

For some reason I'm under the impression that == "" means "anything", but I don't think thats right. 

any help? 

 

also this needs to work for MP.

 

init.sqf

execVM "hostileCheck.sqf";

 

hostileCheck.sqf

if (currentWeapon player == "") then

{

player setCaptive true; hint "no has weapon"

}

else

{

player setCaptive false; hint "has weapon"

};

Share this post


Link to post
Share on other sites
4 hours ago, pierremgi said:

weapons player isEqualTo []

returns true if no more weapon.

This even returns weapons that are put inside a backpack or vest, so pretty good for OPs purposes.

Just note that it won't return throwable weapons like hand grenades or placeable explosives like satchels.

 

Cheers

Share this post


Link to post
Share on other sites

what would i add to check for throwables?

 

this is what i have now and it works for weapons:

while {true} do {
if (weapons player isEqualTo []) then {hint "no has weapon"; player setCaptive true;} else
{player setCaptive false; hint "has weapon"};
};
 

i want there to be a delay when a player drops their weapons before it switches them to setCaptive true; if i use   {hint "no has weapon"; sleep 15; player setCaptive true;}  it causes a delay for switching to hostile also. which i don't want.

Share this post


Link to post
Share on other sites

make sure you spawn that, no reason to have this unscheduled.

0 spawn
{
    while {true} do 
    {
        if (weapons player isEqualTo []) then 
        {
			hintSilent "Player has no weapon!";
			sleep 15; //your wait time here (in seconds)
			player setCaptive true;
		};
	};
};

(For some reason the code block is refusing to update? Misaligned bracket?)

Edited by Midnighters
For some reason the code block is refusing to update? Misaligned bracket?

Share this post


Link to post
Share on other sites
13 hours ago, Midnighters said:

make sure you spawn that, no reason to have this unscheduled.


0 spawn
{
    while {true} do 
    {
        if (weapons player isEqualTo []) then 
        {
			hintSilent "Player has no weapon!";
			sleep 15; //your wait time here (in seconds)
			player setCaptive true;
		};
	};
};

(For some reason the code block is refusing to update? Misaligned bracket?)

 

 

Be careful with the while-true-if-then loop! You can run multiple times useless code with that.


 

0 = [] spawn {
  while {true} do {
    waituntil  {sleep 1; weapons player isEqualTo [] };
    hintSilent "Player has no weapon!";
    sleep 15;
    player setCaptive true;
    waituntil  {sleep 1; !(weapons player isEqualTo []) };
    hintSilent "Player has no weapon!";
    player setCaptive true;
  };
};

 

  • Like 1

Share this post


Link to post
Share on other sites

ok, so this is working great!

 

0 = [] spawn 
    {
        while {true} do 
        {
            waituntil  
                {
                    sleep 1; (weapons player isEqualTo []) 
                };
                    hintSilent "cooldown";
                    sleep 30;
                    hintSilent "not hostile!";
                    player setCaptive true;
            waituntil  
                {
                    sleep 1; !(weapons player isEqualTo []) 
                };
                    hintSilent "hostile!";
                    player setCaptive false;
        };
    };

 

 

I also need to add a check for vehicle gunner. in this case being armed or being a gunner in a vehicle would yield the same result.

 

something like this, but this isn't working...

!(weapons player isEqualTo []) OR  (gunner (vehicle player) == player) 

Share this post


Link to post
Share on other sites

Check that:

weapons player isEqualTo [] && (player ammo (vehicle player currentWeaponTurret [0]) = 0)

 

No matter if the gunner is in the turret. The question is whether or not he has some ammo ready. You can add a canFire condition also.

Share this post


Link to post
Share on other sites

Check that:

weapons player isEqualTo [] && (player ammo (vehicle player currentWeaponTurret [0]) = 0)

 

I cant seem to get this right, the above gives me errors for missing ")" but i cant seem to correct it. 

Share this post


Link to post
Share on other sites

Sorry, my bad:

(weapons player) isEqualTo [] && (player ammo (vehicle player currentWeaponTurret [0]) == 0)

Share this post


Link to post
Share on other sites

I'm so sorry for being such a noob here. I greatly appreciate your help @pierremgi. I'm still having trouble. 

this is my whole script.

 

0 = [] spawn 
    {
        while {true} do 
        {
            waituntil  
                {
                    sleep 1; (weapons player) isEqualTo [] && (player ammo(vehicle player currentWeaponTurret [0]) == 0)
                };
                    hintSilent "cooldown";
                    sleep 30;
                    hintSilent "not hostile!";
                    player setCaptive true;
            waituntil  
                {
                    sleep 1; !(weapons player) isEqualTo [] || !(player ammo(vehicle player currentWeaponTurret [0]) == 0) // I tried && here as well, but OR seems more appropriate?
                };
                    hintSilent "hostile!";
                    player setCaptive false;
                    
                    
        };
    };

 

the first half seems to be working.

Im getting an error still "type array, expected bool" for the second half. regardless of using && or ||

 

thanks again for your help.

Share this post


Link to post
Share on other sites

Not too far:

! ((weapons player) isEqualTo []) || ! (player ammo(vehicle player currentWeaponTurret [0]) == 0)

Share this post


Link to post
Share on other sites

 

0 = [] spawn 
    {
        while {true} do 
        {
            waituntil  
                {
                    sleep 1; (weapons player) isEqualTo [] && (player ammo(vehicle player currentWeaponTurret [0]) == 0)
                };
                    hintSilent "cooldown";
                    sleep 30;
                    hintSilent "not hostile!";
                    player setCaptive true;
            waituntil  
                {
                    sleep 1; !((weapons player) isEqualTo []) || !(player ammo(vehicle player currentWeaponTurret [0]) == 0)
                };
                    hintSilent "hostile!";
                    player setCaptive false;
                    
                    
        };
    };

Share this post


Link to post
Share on other sites

Sorry, but this script is not working as it should. If a player is a door gunner or piloting an attack heli the script does not work. 

Share this post


Link to post
Share on other sites

ah.. i see.

is there a cleaner way to do this like [-1,0,1,2,3,4,5](not working), instead of individually? 

 

i've also noticed that some weapons dont register. for example a tank with multiple types of ammo, some of them will trigger this script others wont.

Share this post


Link to post
Share on other sites

lol, this is what i have now.. it kinda works but its a mess. I should also reiterate. If i'm the gunner in an attack helo, different ammo types trigger this script while others done. i.e. switching to the hydra triggers the script but not the cannon?

 

 

0 = [] spawn 
    {
        while {true} do 
        {
            waituntil  
                {
                    sleep 1; (weapons player) isEqualTo [] && (player ammo(vehicle player currentWeaponTurret [0]) == 0) && 
                    (player ammo(vehicle player currentWeaponTurret [1]) == 0) && (player ammo(vehicle player currentWeaponTurret [-1]) == 0)&& 
                    (player ammo(vehicle player currentWeaponTurret [2]) == 0) && (player ammo(vehicle player currentWeaponTurret [3]) == 0) &&
                    (player ammo(vehicle player currentWeaponTurret [4]) == 0) && (player ammo(vehicle player currentWeaponTurret [5]) == 0) &&
                    (player ammo(vehicle player currentWeaponTurret [6]) == 0) && (player ammo(vehicle player currentWeaponTurret [7]) == 0)
                };
                    hintSilent "cooldown";
                    sleep 5;
                    hintSilent "not hostile!";
                    player setCaptive true;
                    
            waituntil  
                {
                    sleep 1; !((weapons player) isEqualTo []) || !(player ammo(vehicle player currentWeaponTurret [0]) == 0) || 
                    !(player ammo(vehicle player currentWeaponTurret [1]) == 0) || !(player ammo(vehicle player currentWeaponTurret [-1]) == 0) || 
                    !(player ammo(vehicle player currentWeaponTurret [2]) == 0) || !(player ammo(vehicle player currentWeaponTurret [3]) == 0) ||
                    !(player ammo(vehicle player currentWeaponTurret [4]) == 0) || !(player ammo(vehicle player currentWeaponTurret [5]) == 0) ||
                    !(player ammo(vehicle player currentWeaponTurret [6]) == 0) || !(player ammo(vehicle player currentWeaponTurret [7]) == 0)
                };
                    hintSilent "hostile!";
                    player setCaptive false;
                    
                    
        };
    };
    

Share this post


Link to post
Share on other sites

My last price:

 

0 = [] spawn {
  fn_isHostile = compileFinal "
    private _ammo = 0;
    _turret = [-2];
    {if (vehicle player turretUnit _x == player) exitWith {_turret = _x}} forEach (allTurrets [vehicle player,true] + [[-1]]);
    call {
      if (isNull objectParent player && _turret isEqualTo [-2]) exitWith {_ammo = count weapons player};
      if  (!isNull objectParent player && _turret isEqualTo [-2]) exitWith {
        _ammo = 0};
      if (!(_turret isEqualTo [-2]) && vehicle player currentMagazineTurret _turret == '') exitWith {_ammo = count weapons player};
      _ammoArray = (magazinesAllTurrets  vehicle player) select { _x select 0 isEqualTo (vehicle player currentMagazineTurret _turret)} apply {_x select 2};
      {_ammo = _ammo + _x} forEach _ammoArray; hint str (_ammo);
    };
    _ammo
  ";
  while {true} do {
    waituntil {
      sleep 1; _ammo = call fn_isHostile; _ammo == 0
    };
    hintSilent "cooldown";
    sleep 3;
    hintSilent "not hostile!";
    player setCaptive true;
    waituntil {
      sleep 1; _ammo = call fn_isHostile; _ammo != 0
    };
    hintSilent "hostile!";
    player setCaptive false;
  };
};

 

This script can manage all the turrets (cargo included).

Some very specific cases like copilot "armed" with a searchlight led to choose its own weapon even if it can't fire.

But more or less, every unit able to fire is hostile.

 

NB: I don't know why it's so difficult to have BI reliable commands for turret's magazine and ammo. At least two of them are missing:

the currentTurret unit (returning the turret path) and the currentTurretMagazineAmmo (same as the HUD display). Of course one command for infantry(no turret) or crew (turret)situation could be a must!

 

 

 

  • Like 1

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

×