Jump to content
Exxor

How do I delete a vehical or an ammo crate if its inventory is empty?

Recommended Posts

Hello there, I have been trying to delete an ammo crate with sqf if its inventory is empty (the one I'm using is  "Box_NATO_Wps_F"). But I can't seem to figure it out. Any and all help is appreciated thanks :). I have tried things such as using getWeaponCargo and counting it to see if I can check if the array is empty but it always stays at 2 even if there is nothing inside the box.

Share this post


Link to post
Share on other sites

@Schatten

That didn't work. The box spawns with a weapon as I want it to when it first appears. However, I get errors when doing it the way you suggested. zTL89CJ.png

 

Here is what my code looks like.

	_spawnedCrate = "Box_NATO_Wps_F" createVehicle _cratePosition;
	_items = true;

	_crateContents = getItemCargo _spawnedCrate;

	clearWeaponCargoGlobal _spawnedCrate;
	clearMagazineCargo _spawnedCrate;
	
	_spawnedCrate addWeaponCargo [_selectedWeapon, 1];

// ERROR IN THIS AREA
while {_items} do
{
	hint "Waiting for crate to be empty";
	sleep 1;
	if(_crateContents == 0) then
	{
		hint "in if";
		sleep 1;
		deleteVehicle _spawnedCrate;
		_items = false;
	};
};

	hint format["The weapon you got is %1", _randomNumber];
};

 

 

Share this post


Link to post
Share on other sites

@Exxor, sure, you incorrectly use equality operator. Use this code:

_crateContents isEqualTo []

 

Share this post


Link to post
Share on other sites

And use the containerClosed EH instead of the while loop.

_spawnedCrate = "Box_NATO_Wps_F" createVehicle _cratePosition;
	
_crateContents = getItemCargo _spawnedCrate;

clearWeaponCargoGlobal _spawnedCrate;
clearMagazineCargo _spawnedCrate;
	
_spawnedCrate addWeaponCargo [ _selectedWeapon, 1 ];
_spawnedCrate setVariable[ "rndWeapon", _selectedWeapon ];

_spawnedCrate addEventHandler [ "ContainerClosed", {
	params[ "_container" ];
	
	_contents = magazineCargo _container + weaponCargo _container + itemCargo _container + backpackCargo _container;
	if ( _contents isEqualTo [] ) then {
		hint format[ "The weapon you got is %1", _container getVariable "rndWeapon" ];
	};
}];

 

Share this post


Link to post
Share on other sites

@Larrow Thanks, what you suggested has worked I appreciate it. However, now I get this error msg when trying to use the sleep command inside of the eventHandler.  qb1M4wl.png

 

This is what the amended code looks like.

	_selectedWeapon = [_randomNumber, _commonWeps, _uncommonWeps, _rareWeps] call fn_SelectWeapon; //has to call instead of spawn becasue we are returning a value
	_spawnedCrate = "Box_NATO_Wps_F" createVehicle _cratePosition;

	clearWeaponCargoGlobal _spawnedCrate;
	clearMagazineCargo _spawnedCrate;
	
	_spawnedCrate addWeaponCargo [_selectedWeapon, 1];

	_crateContents = getItemCargo _spawnedCrate;



_spawnedCrate addEventHandler["ContainerClosed", {
	params["_container"]; //object that the eventhandler is being called on

	_contents = itemCargo _container + weaponCargo _container + magazineCargo _container + backpackCargo _container;

	if(_contents isEqualTo []) then
	{
		sleep 3;
		deleteVehicle _container;
	};

}];

	hint format["The weapon you got is %1", _randomNumber];
};

Also, if you could explain why I can't use _contents == 0 and why I have to use _contents isEqualTo [], then that would be greatly appreciated.

Share this post


Link to post
Share on other sites

You can't use sleep inside the event handler. Need to call a new script then:

_spawnedCrate = "Box_NATO_Wps_F" createVehicle _cratePosition;
	
_crateContents = getItemCargo _spawnedCrate;

clearWeaponCargoGlobal _spawnedCrate;
clearMagazineCargo _spawnedCrate;
	
_spawnedCrate addWeaponCargo [ _selectedWeapon, 1 ];
_spawnedCrate setVariable[ "rndWeapon", _selectedWeapon ];

_spawnedCrate addEventHandler [ "ContainerClosed", {
	params[ "_container" ];
	
	_contents = magazineCargo _container + weaponCargo _container + itemCargo _container + backpackCargo _container;
	if ( _contents isEqualTo [] ) then {
		[_spawnedCrate] spawn {
			_spawnedCrate = _this select 0;
			
			hint "I am empty and ready to get deleted from this world.";
			sleep 999;
			deleteVehicle _spawnedCrate;
		};
	};
}];

 

Quote

Also, if you could explain why I can't use _contents == 0 and why I have to use _contents isEqualTo [], then that would be greatly appreciated.

 

Because your _contents is an array, and you want to know if your array is empty, while "" means you're checking a string and 123, etc. is checking for a number.

 

You CAN compare with a number, but then you have to count the content of your array, e.g.

if (count _contents == 0) then ...

 

Share this post


Link to post
Share on other sites
42 minutes ago, Exxor said:

However, now I get this error msg when trying to use the sleep command inside of the eventHandler.

Suspensions are not allowed in event handlers. Use spawn command to launch piece of code with suspension.

 

42 minutes ago, Exxor said:

why I can't use _contents == 0 and why I have to use _contents isEqualTo []

As you can see, _contents is array. It is not allowed to use equality operator with arrays.

  • 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

×