A_DropItem

From ZDoom Wiki
Jump to navigation Jump to search

Actor

Actor A_DropItem(class<Actor> item, int dropamount = -1, int chance = 256)

Usage

The calling actor spawns the specified item as if it was dropped. This is meant to simulate how monsters "drop" items on death via their DropItem property, by giving them a bit of velocity. The item in question is not actually taken from (and doesn't have to be preset in) the calling actor's inventory.

Parameters

  • class<Actor> item
The item to drop. Despite the name, this doesn't have to be an item, and can be any actor class.
  • int dropamount
If item is indeed a valid item (i.e. based on the Inventory class) and this is non-negative, its amount will be set to this value. If this value is negative, the dropped item's amount will be equal to its default Inventory.Amount value. Note, if item is an Ammo item, its amount will be modified by the current skill level's DropAmmoFactor.
  • int chance
The probability of the drop in the 0-255 range. If negative, the drop always fails; if 255 or above, the drop is guaranteed. Default is 256.

Return value

  • Actor - a pointer to item if it was spawned successfully; otherwise it returns null.

Examples

This is an excerpt from PlayerPawn's Die override, which makes sure that the player drops their current weapon when killed:

...
else if (weap.SpawnState != NULL &&
	weap.SpawnState != GetDefaultByType('Actor').SpawnState)
{
	let weapitem = Weapon(A_DropItem (weap.GetClass(), -1, 256));
	if (weapitem)
	{
		if (weap.AmmoGive1 && weap.Ammo1)
		{
			weapitem.AmmoGive1 = weap.Ammo1.Amount;
		}
		if (weap.AmmoGive2 && weap.Ammo2)
		{
			weapitem.AmmoGive2 = weap.Ammo2.Amount;
		}
		weapitem.bIgnoreSkill = true;
	}
}
...

Most of the code is omitted for brevity. The full code can be seen on GZDoom GitHub repository.

ZScript definition

Note: The ZScript definition below is for reference and may be different in the current version of GZDoom.The most up-to-date version of this code can be found on GZDoom GitHub.
	Actor A_DropItem(class<Actor> item, int dropamount = -1, int chance = 256)
	{
		if (item != NULL && random[DropItem]() <= chance)
		{
			Actor mo;
			double spawnz = 0;

			if (!(Level.compatflags & COMPATF_NOTOSSDROPS))
			{
				int style = sv_dropstyle;
				if (style == 0)
				{
					style = gameinfo.defaultdropstyle;
				}
				if (style == 2)
				{
					spawnz = 24;
				}
				else
				{
					spawnz = Height / 2;
				}
			}
			mo = Spawn(item, pos + (0, 0, spawnz), ALLOW_REPLACE);
			if (mo != NULL)
			{
				mo.bDropped = true;
				mo.bNoGravity = false;	// [RH] Make sure it is affected by gravity
				if (!(Level.compatflags & COMPATF_NOTOSSDROPS))
				{
					mo.TossItem ();
				}
				let inv = Inventory(mo);
				if (inv)
				{
					inv.ModifyDropAmount(dropamount);
					inv.bTossed = true;
					if (inv.SpecialDropAction(self))
					{
						// The special action indicates that the item should not spawn
						inv.Destroy();
						return null;
					}
				}
				return mo;
			}
		}
		return NULL;
	}

See also