Jump to content
MechSlayer

WeaponHolderSimulated spawns as ObjNull

Recommended Posts

I'm trying to spawn weapons inside each building in the map, but for some reason more than half spawns as ObjNull

Here's the code:

_buildings = nearestObjects[_worldCenter, ["building"], (worldName call BIS_fnc_mapSize)]; //Get all buildings.
{
  _buildingPositions = _x buildingPos -1;  //Get all positions inside the building.
  _localPos = selectRandom _buildingPositions; //Select a random one.
  _spawnPos = [(_localPos select 0), (_localPos select 1), ((_localPos select 2) + 1)]; //Elevate the position 1 meter.
  _holder = createVehicle ["WeaponHolderSimulated", _spawnPos, [], 0, "CAN_COLLIDE"]; //Spawn the weapon holder.
  _weapon = selectRandom _weaponsArray; //Select a random weapon.
  _holder addItemCargoGlobal [_weapon, 1]; //Add the weapon to the holder.
} forEach _buildings;

 

Share this post


Link to post
Share on other sites

Use WeaponHolderSimulated_Scripted
The normal one despawns when its empty. which it is.

 

 _spawnPos = [(_localPos select 0), (_localPos select 1), ((_localPos select 2) + 1)];

can be replaced by

_spawnPos = _localPos vectorAdd [0,0,1];

 

  • Thanks 1

Share this post


Link to post
Share on other sites

I am having the same problem even with WeaponHolderSimulated_Scripted!

 

CODE spawned every time a unit dies to convert its body to an agent:

Spoiler



vBodiesToAgents = {
	params ["_body"];
	_body disableAi "ALL";
	_body setSkill 0;
	if (isAgent teamMember _body) exitWith {}; // body Already an Agent
	sleep (5 + (random 5));
	if !(isTouchingGround _body) exitWith {}; // Body is in the air!
	private _pos = getposATL _body;
	private _dir = getDir _body;
	private _type = typeOf _body;
	private _loadout = getUnitLoadout _body;
	private _face = face _body;
	private _name = name _body;
	private _holders = [];
	_holders = nearestObjects [getPos _body, ["WeaponHolderSimulated", "GroundWeaponHolder", "WeaponHolderSimulated_Scripted"], 3];
	private _weaponsArray = [];
	if !(_holders isEqualTo []) then {
		{
			private _getWeap = weaponsItemsCargo _x;
			_weaponsArray pushBack _getWeap;
		} forEach _holders;
	};
	private _bodyAgent = createAgent [_type, [0,0,0], [], 0, "CAN_COLLIDE"];
	_bodyAgent disableAi "ALL";
	_bodyAgent setSkill 0;
	_bodyAgent setUnitLoadout _loadout;
	if (_face != "") then {_bodyAgent setFace _face};
	if (_name != "") then {_bodyAgent setName _name};
	_bodyAgent setDir _dir;
	private _anim = "deadstate";
	if (random 10 > 9) then {
		_anim = selectRandom [
		"AmovPincMstpSrasWrflDnon",
		"AadjPpneMstpSrasWpstDdown",
		"AinvPpneMstpSrasWrflDnon_Putdown",
		"AinvPpneMstpSrasWrflDnon",
		"Hepler_InjuredNon"
		];
	};
	[_bodyAgent, _anim] remoteExecCall ["switchMove",0,false];
	_bodyAgent setDammage 1;
	sleep 1;
	private _weaponHolder = objNull;
	if !(_weaponsArray isEqualTo []) then {
		if ( isNull _weaponHolder ) then {
			_weaponHolder = createVehicle ["WeaponHolderSimulated_Scripted", _pos, [], 2, "CAN_COLLIDE"];
			{
				_weaponHolder addWeaponCargoGlobal [_x,1];
			} forEach _weaponsArray;
			_weaponHolder disableCollisionWith _bodyAgent;
			_bodyAgent disableCollisionWith _weaponHolder;			
		};
	};
	_bodyAgent setVariable ["vGC_deathTime",time];
	hintsilent format ["pos holder: %1", getPos _weaponHolder];
	deleteVehicle _body;
	_bodyAgent setPos _pos;
};


 

 

Still the weapon holder with the unit's weapons do not appear next to the "new" agent body.

 

Please help!

Share this post


Link to post
Share on other sites

If _weaponsArray is populated it includes arrays, like [[["MMG_02_sand_RCO_LP_F","","acc_pointer_IR","optic_Hamr",["130Rnd_338_Mag",130],[],"bipod_01_F_snd"]]] so you're using addMagazineCargo with faulty data (passing array instead of string as weapon class)
Use addWeaponWithAttachmentsCargoGlobal instead: _weaponHolder addWeaponWithAttachmentsCargoGlobal  [_x # 0, 1];
:shrug:

  • Like 1
  • Thanks 1

Share this post


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

If _weaponsArray is populated it includes arrays, like [[["MMG_02_sand_RCO_LP_F","","acc_pointer_IR","optic_Hamr",["130Rnd_338_Mag",130],[],"bipod_01_F_snd"]]] so you're using addMagazineCargo with faulty data (passing array instead of string as weapon class)
Use addWeaponWithAttachmentsCargoGlobal instead: _weaponHolder addWeaponWithAttachmentsCargoGlobal  [_x # 0, 1];
:shrug:

 

That worked @h -!

 

Thank you!

 

I just got one instance of "Error type ANY, expected array" on that line. How can I prevent that?

Share this post


Link to post
Share on other sites

Odd, I thought that shouldn't really happen. But you just make sure the array _x refers to is not empty (if it is _x # 0 does not exist), so checking that the element is an array and has something in it should help

if (_x isEqualType [] && {count _x > 0}) then {
    _weaponHolder addWeaponWithAttachmentsCargoGlobal [_x # 0, 1];
};

 

  • Like 1
  • Thanks 1

Share this post


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

Odd, I thought that shouldn't really happen. But you just make sure the array _x refers to is not empty (if it is _x # 0 does not exist), so checking that the element is an array and has something in it should help


if (_x isEqualType [] && {count _x > 0}) then {
    _weaponHolder addWeaponWithAttachmentsCargoGlobal [_x # 0, 1];
};

 

 

That did the trick! 

 

Thank you @h -!

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

×