Home   Help Search Login Register  

Author Topic: Switch between two rifles  (Read 2685 times)

0 Members and 1 Guest are viewing this topic.

Walter_E_Kurtz

  • Guest
Switch between two rifles
« on: 27 Oct 2011, 02:52:31 »
This system is intended to allow the player to change between two primary weapons ie. rifles. It mimics HALO by simulating carrying a second rifle which the player can swap to at will. The active weapon can be changed in the normal fashion by picking something up from a fallen enemy (or comrade).

Features
  • No addons required.
  • Should be compatible with most weapon packs.
  • Context-appropriate option in the Action Menu.
  • Swappable weapon is identified.
  • When swapping weapons the ammo in the current magazine is preserved. Failed to reload before switching? You may regret it when you switch back.
  • Multi-language with a stringtable - no guarantees on the quality of translation, though.

Warnings
  • Not widely tested - there's bound to be some gun it won't work properly with.
  • Displays the config name of a weapon rather than a friendlier version.
  • At the moment, only suitable for Single Player.
  • Doesn't have an exit - once run it'll keep going until the end of the mission.
  • Only tested with OFP v1.96, but I see no reason why it shouldn't work with ArmA:CWA.


Setting it up

1. A corpse is needed to keep hold of the spare weapon.
      Place a soldier somewhere out of the way. Reduce his health to zero and name him ColdDeadHands. Make sure has no equipment of his own by writing the following in his Init line:
         removeAllWeapons this
      If you wish the player to be able to swap weapons from the commencement of the mission, add the appropriate weapon and ammo to this corpse. For instance:
         removeAllWeapons this; this addMagazine "HK"; this addMagazine "HK"; this addWeapon "HK"

2. Launch the script that updates the player's Action Menu.
      This script should only be run once, whenever you want the player to begin having the option of carrying a second weapon. Since the Action Menu text should always be appropriate to the situation, it's probably simplest to do this at the start of the mission from the player's Init line:
         this exec "WEK_SwapWeapon_ActionMenu.sqs"

3. Include the necessary scripts in the mission folder.
      First, save the mission and give it a name.
      From wherever you have OFP/CWA installed, this would be typically somewhere like <OFP or CWA> \ Users \ <Your username> \ missions \ <Your mission name> \

Code: (WEK_SwapWeapon_ActionMenu.sqs (v1.07)) [Select]
_CheckIfDropped = false
goto "NoWeapons"

#Loop
~1.2
if ((primaryWeapon player == "") && _CheckIfDropped) then {goto "Change"}
if ((primaryWeapon player != "") && !(_CheckIfDropped)) then {goto "Change"}
if (primaryWeapon player != _oldweap) then {goto "Change"}
if (vehicle player != player) then {player removeAction WEK_SwapWeaponOption; goto "NoWeapons"}
goto "Loop"

#NoWeapons
~3
; no weapons: no action
if ((primaryWeapon ColdDeadHands == "") && (primaryWeapon player == "")) then {goto "NoWeapons"}
if (vehicle player != player) then {goto "NoWeapons"}

#Change
player removeAction WEK_SwapWeaponOption
_oldweap = (primaryWeapon player)
_altweap = (primaryWeapon ColdDeadHands)

; two weapons: permit swap
if ((primaryWeapon ColdDeadHands != "") && (primaryWeapon player != "")) then {WEK_SwapWeaponOption = player addAction [format ["Switch to %1", _altweap], "WEK_SwapWeapon.sqs"]; _CheckIfDropped = true; goto "Loop"}

; player has weapon but ColdDeadHands not: put weapon into reserve
if ((primaryWeapon ColdDeadHands == "") && (primaryWeapon player != "")) then {WEK_SwapWeaponOption = player addAction [format ["Sling %1", _oldweap], "WEK_SwapWeapon.sqs"]; _CheckIfDropped = true; goto "Loop"}

; no weapon in player's hands but ColdDeadHands has one: take weapon from reserve
if ((primaryWeapon ColdDeadHands != "") && (primaryWeapon player == "")) then {WEK_SwapWeaponOption = player addAction [format ["Unsling %1", _altweap], "WEK_SwapWeapon.sqs"]; _CheckIfDropped = false; goto "Loop"}

goto "NoWeapons"

Code: (WEK_SwapWeapon.sqs (v1.07)) [Select]
if (primaryWeapon ColdDeadHands == "") then {goto "PutWeapon"}

; Swap Weapons
player action ["Take Weapon", ColdDeadHands, 0,0, (primaryWeapon ColdDeadHands)]

; Check if ColdDeadHands ended up with an AT weapon,
; if so, player has 2 slot machinegun (eg. M60).
; AT weapon returned to players feet.
~1.2
if (secondaryWeapon ColdDeadHands == "") then {exit}

_droppedAT = "weaponholder" createvehicle (getpos player)
hint format ["%1 dropped to make room.", (secondaryWeapon ColdDeadHands)]
ColdDeadHands action ["Drop Weapon", _droppedAT, 0,0, (secondaryWeapon ColdDeadHands)]
_droppedAT setPos [(getpos player select 0) + sin (getDir player), (getpos player select 1) + cos (getDir player)]
player reveal _dropppedAT
exit

; Put Weapon (because there was nothing to swap)
#PutWeapon
player action ["Drop Weapon", ColdDeadHands, 0,0, (primaryWeapon player)]
~1
ColdDeadHands action ["Take Weapon", player, 0,0, (primaryWeapon player)]
exit


Notes
      1. The body doesn't act like a crate so you can't take extra mags at will. The way to access more ammo is to switch weapons twice.
      Try this in the demo mission: switch to the HK and kill the guy with the RPG. Take his launcher and you will now be carrying 3 RPGs. Switch to the M16-GL. You will get two M16 mags plus a grenade whilst retaining the three RPGs. Expend some ammo: shoot the M16 and grenade launcher. To rearm you should switch to the HK and then back again to the M16-GL.

      2. I did try to implement friendly names for the weapons, but unfortunately only a few BIS weapons share their stringtable designation with their config name (eg. M16 and STR_DN_M16).
Code: [Select]
_altweapName = localize format ["STR_DN_%1", _altweap]      It would be necessary to expand the stringtable to include some original weapons, like grenade launchers, as well as all the BIS addons.


Status
      Updated to version 1.08: stringtable and change to secondaryweaponholder.
         v1.07: removed 2-slot weapon array.
         v1.06: 2-slot weapon array added to obviate vanishing RPG launchers.
         v1.05: allow for the player entering / leaving vehicles.
      Inviting comments and feedback.
      Very basic demo mission attached: see your alternative weapon by the corpse, try changing the type of soldier you play, kill the Resistance guys and take their weapons, go for a drive in the Jeep. Try changing some of the men to JAM soldiers, or other weapon packs.
« Last Edit: 03 Nov 2011, 15:56:07 by Walter_E_Kurtz »

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Switch between two rifles
« Reply #1 on: 28 Oct 2011, 18:47:36 »
I gave this a quick go.

Simple but effective. That said, losing a secondary weapon is a bit of a deal breaker. Take PK, switch to HK, take RPG launcher, switch to PK, switch back to HK = no RPG launcher...

Otherwise dandy :)

Walter_E_Kurtz

  • Guest
Re: Switch between two rifles
« Reply #2 on: 29 Oct 2011, 22:42:44 »
Typical. That's what I get for assuming it would be dropped in the usual way (like when you pick up a PK). And anyway, it wasn't lost, just relocated to ColdDeadHands :whistle:

Consequently I think I'll stick with BIS' rules and enforce dropping of an AT weapon when you equip a 2-slot machinegun. In my view, allowing the player to stash an AT launcher would have been too much of an exploit as then you'd be able to carry an AK-47 and RPG while having an M16 and LAW 'on a sling'. Also, the scripting would get a whole lot more complex.

I'm updating it now. There are small additions at the tops of the current (v1.05) scripts and a new one to create the array of 2-slot machineguns. When you switch to these, you will drop your secondary weapon if you have one; and also get a hint mentioning the fact. As a result, the script is now longer universal 'out of the box'. However, I will endeavour to update the array with commonly-used weapons that would otherwise cause a problem. I've just checked JAM 3 and all those machineguns are single slot; hence compatible already.

Offline Aldo15

  • OFP-addict
  • Former Staff
  • ****
    • My OFP missions
Re: Switch between two rifles
« Reply #3 on: 30 Oct 2011, 13:44:58 »
Cool, it was what I was lookinf for. Great job Walter :good:
My OFP stuff
FMF Project by Aldo15. Coming soon.
If in the high school or university, Teachers add a new subject about how to make scripts, missions, for ofp. I'd be number one of my classroom

Walter_E_Kurtz

  • Guest
Re: Switch between two rifles
« Reply #4 on: 30 Oct 2011, 18:06:05 »
It's not quite the final version, but getting closer.

Making an array of 2-slot weapons was a poor idea. The script now checks if ColdDeadHands ends up with anything in his secondary weapon slot, and if so returns it to the player's feet. A delay of more than a second is needed before the secondaryWeapon check gives the true answer. Consequently the script should be universal again, whatever weapon pack is being used.

Updating to version 1.07 1.08
      Added a stringtable for multi-language support; though I don't believe that Google Translate has done a good job in all cases.
      Changed drop RPGs to secondaryweaponholder: now they lie flat on the ground.
« Last Edit: 03 Nov 2011, 15:52:18 by Walter_E_Kurtz »