Jump to content
ImperialAlex

"PutWeapon" into unit's backpack.

Recommended Posts

I'm currently working on something loadout save/restore related. As part of that I've come across the issue that there aren't any script commands to add weapons with (custom) attachments into containers. I.e. you can add "arifle_MX_F" or "arifle_MX_ACO_F" into a container but there's no way (as far as I can see) of adding weaponItems to a weapon in a container.

As a workaround for that, I've written a little function that "constructs" a weapon with custom attachments on a unit and then uses the "putWeapon" action to move it into a container. The script can be seen here ( http://hastebin.com/lenulafafe.md ). [N.B: It doesn't do custom ammo-counts in the gun's magazine. Not needed for my application]

It works great when I pass in an ammo-crate as the "object" but when I pass in (unitBackpack _unit) the "dropWeapon" part freezes the game.

(Tried this by just executing a dropWeapon with unit player, into "unitBackpack player" and that freezes the game, too).

It seems like dropWeapon into the unit's own backpack doesn't work. What would be possible workarounds?

Apart from the obvious "BI implements "vehicleName addWeaponCargoGlobal [weaponName, count, weaponItems]"" ;)

I've thought about doing putBag, then putWeapon into the backpack on the ground, then addBag but it can't figure out how to putWeapon into a backpack on the ground.

(I can't find a way to reference the backpack...do I need to look through the nearby objects?)

Any help on this would be highly appreciated :)

Share this post


Link to post
Share on other sites
Have you tried backpackContainer instead of using the backpack object?

Thanks, I didn't know this existed. I'll try it and report back :)

EDIT: No luck: PutWeapon into (backpackContainer) still freezes the game :(

Share this post


Link to post
Share on other sites

I don't know how it whould be performance wise (I'd assume bad), but maybe you can locally spawn a unit, add the needed attachments with normal commands, get that weapon, put it in the backpack and then delete the temporary local unit?

Share this post


Link to post
Share on other sites
I don't know how it whould be performance wise (I'd assume bad), but maybe you can locally spawn a unit, add the needed attachments with normal commands, get that weapon, put it in the backpack and then delete the temporary local unit?

I'll see if "PutWeapon"-ing into another unit's backpack works once I get home. If that works (and I'm not too confident right now) then I'll probably do it as you suggested.

@BI: Please please please give us a script command to properly add WeaponItem'd weapons!

Share this post


Link to post
Share on other sites
Just use virtual arsenal.

people make everything too hard.

export

Ironically I'm doing this because custom-attachments-on-weapons-in-containers aren't supported in the arsenal. The arsenal is great but it does have a few issues, this being one of them. (Which, admittedly is more of an issue with BI's lack of script commands for this)

Share this post


Link to post
Share on other sites

check my signature for a feedback tracker ticket. made it months ago and even asked for an update...radio silence

Share this post


Link to post
Share on other sites
check my signature for a feedback tracker ticket. made it months ago and even asked for an update...radio silence

Thanks for the link, looks like you tried the exact same route? Did you ever manage to get a work-around working?

Share this post


Link to post
Share on other sites

If I remember correctly, an addon called "Universal Soldier" had the ability to load weapons with attachments into a backpack. Might have been removed in latest versions.

Could still be worth taking a look & then contacting the author.

Share this post


Link to post
Share on other sites
Thanks for the link, looks like you tried the exact same route? Did you ever manage to get a work-around working?

I thought about doing the same thing as you, spawning a unit nearby and making him invisible, then having the unit drop his weapon into your backpack, but I didn't want to go through the trouble of actually doing it. Instead I just put that project on hold for the time being lol. I'm not sure how they did it but I played A3Epoch and their saving/loading scripts seem to work pretty good, you may want to look into them. I've also seen some scripts that actually just save the attachments themselves and then add the base guns with extra attachments into the players inventory, kinda dirty but it works.

I also experimented with saving the actual string names for each attachment and then re-adding the weapon by using a classname, but it seems that there are a lot of combinations that don't exist for some reason so that fell apart pretty quickly. Maybe someday in my lifetime a command will exist

Share this post


Link to post
Share on other sites

As per 654wak654's suggestion..

// [ primaryWeapon player, [ "muzzle_snds_H" ], backpackContainer player ] spawn fnc_addWeaponContainerAtt;
// [ "arifle_TRG20_F", [ "optic_Nightstalker", "muzzle_snds_M", "acc_flashlight" ], backpackContainer player ] spawn fnc_addWeaponContainerAtt;
fnc_addWeaponContainerAtt = {

_weapon = [ _this, 0, "", [ "" ] ] call BIS_fnc_param;
_attachments = [ _this, 1, [], [ [] ], [ 1, 2, 3 ]] call BIS_fnc_param;
_container = [ _this, 2, objNull, [ objNull ] ] call BIS_fnc_param;

if ( isNull _container ) exitWith { "No container specified" call BIS_fnc_error };

_weaponType = [
	"",
	"primary",
	"handgun",
	"", 
	"secondary"
] select ( getNumber ( configFile >> "CfgWeapons" >> _weapon >> "type" ) );


if (_weaponType != "") then {

	_group = createGroup west;
	_unit = _group createUnit [ "B_Soldier_F", [ 0, 0, 0 ], [], 0, "CAN_COLLIDE" ];

	waitUntil { !isNull _unit };

	hideObjectGlobal _unit;

	removeAllWeapons _unit;
	_unit addWeapon _weapon;

	{
		call compile format [ "_unit add%1WeaponItem _x", _weaponType ];
		waitUntil { _x in ( _unit weaponAccessories _weapon ) };
	}forEach _attachments;

	_unit action [ "DropWeapon", _container, _weapon ];

	sleep 2;
	deleteVehicle _unit;
	deleteGroup _group;

};

true
};

Works ok. TBH if you are just doing this for loading out units then i would just create the unit once, once finished delete if needed.

Edited by Larrow

Share this post


Link to post
Share on other sites

You must note that unit is playing an animation when using actions. Only after it's done will the weapon be available.

So it's far from instant and moreover nasty things can happen in the meantime (either units die,etc.)

I don't know the script it will be used in though.

Share this post


Link to post
Share on other sites

@LarrowTrying to get your code running - without success so far. Can you provide a full mission file? Would be great!

I'm interested to use this "spawn a temporary unit to add weapon attachments" method to add fully equipped weapons to vehicle loadouts so one can directly grab the weapon from there.

Share this post


Link to post
Share on other sites
3 hours ago, Fastfood4Bigfoot said:

@LarrowTrying to get your code running - without success so far. Can you provide a full mission file? Would be great!

I'm interested to use this "spawn a temporary unit to add weapon attachments" method to add fully equipped weapons to vehicle loadouts so one can directly grab the weapon from there.

The reason @Larrow's code might not work is if unit is under water it fails. I am guessing he used VR for testing, so there is no water at [0,0,0]

Share this post


Link to post
Share on other sites
1 minute ago, killzone_kid said:

I am guessing he used VR for testing, so there is no water at [0,0,0]

More than likely :icon_biggrin: 90% of my missions are VR test pieces.

Share this post


Link to post
Share on other sites
4 minutes ago, Larrow said:

More than likely :icon_biggrin: 90% of my missions are VR test pieces.

Actually it is not 100% that it will fail under water but more like it MIGHT fail. Testing it now and it is hit and miss. Not even sure if the water is the cause. But the only reliable way it seems is to have it next to player

Share this post


Link to post
Share on other sites
TAG_fnc_addWeaponContainerAtt = {
	params[
		[ "_weapon", "", [ "" ] ], 
		[ "_attachments", [] ], 
		[ "_container", objNull ]
	];

	if ( isNull _container ) exitWith {
		"No container specified" call BIS_fnc_error;
		false
	};
	
	if !( _container canAdd _weapon ) exitWith {
		format[ "Weapon %1 can not be added to %2", _weapon, _container ] call BIS_fnc_error;
		false
	};

	_weaponType = [
		"",
		"primary",
		"handgun",
		"", 
		"secondary"
	] select ( getNumber ( configFile >> "CfgWeapons" >> _weapon >> "type" ) );


	if ( _weaponType == "" ) exitWith {
		format[ "WeaponType not found for %1", _weapon ] call BIS_fnc_error;
		false
	};
	
	_group = createGroup west;
	_unit = _group createUnit [ "B_Soldier_F", [] call BIS_fnc_randomPos, [], 0, "CAN_COLLIDE" ];

	waitUntil { !isNull _unit };

	_unit addEventHandler [ "HandleDamage", { 0 }];
	hideObjectGlobal _unit;

	removeAllWeapons _unit;
	_unit addWeapon _weapon;

	{
		call compile format [ "_unit add%1WeaponItem _x", _weaponType ];
		waitUntil { _x in ( _unit weaponAccessories _weapon ) };
	}forEach _attachments;

	_unit action [ "DropWeapon", _container, _weapon ];
	
	_unit addEventHandler [ "AnimDone", {
		params[ "_unit", "_anim" ];
		
		if ( _anim find "putdown" > -1 && weapons _unit isEqualTo [] ) then {
			_grp = group _unit;
			deleteVehicle _unit;
			deleteGroup _grp;
		};
			
	}];

	true
};

[ "arifle_TRG20_F", [ "optic_Nightstalker", "muzzle_snds_M", "acc_flashlight" ], backpackContainer player ] spawn TAG_fnc_addWeaponContainerAtt;

 

  • Thanks 1

Share this post


Link to post
Share on other sites
15 hours ago, killzone_kid said:

The reason @Larrow's code might not work is if unit is under water it fails. I am guessing he used VR for testing, so there is no water at [0,0,0]

Read already in the meantime about that issue at https://community.bistudio.com/wiki/action:

Quote

Some of the actions (such as "DropWeapon") do not always execute, and there should be a set of conditions for them to run. The unit must be able to perform the dropping animation in order for the action to properly execute. So, if for example we spawn a unit at [0,0,0] and make it perform a "DropWeapon" action - it won't work if [0,0,0] is water (which it is, if the current map is an island) because the unit will be swimming.

 

This is was what I came up in the meantime, I will test your new revision soon. Looks like you added some checks to verify the animation is over etc. Thanks a lot, @Larrow!

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

×