GetDropItems

From ZDoom Wiki
Jump to navigation Jump to search

Actor

clearscope DropItem GetDropItems() const

Usage

Used to retrieve the list of actor's drop items, which can be specified through the DropItem property.

Similarly to actor's inventory in general, drop items are stored in a linked list. This function retrieves a pointer to the first drop item (represented by a DropItem struct). A FOR or a WHILE loop can then be used to iterate over all drop items.

Return values

Returns a DropItem-type pointer to the first DropItem in the list.

Examples

This example is from the RandomSpawner class which uses its DropItem property to define a list of actors it can spawn, with probability weights and amounts. It uses GetDropItems in its ChooseSpawn virtual function:

[...] // The rest of the code is omitted for brevity
	virtual Name ChooseSpawn()
	{
		DropItem di;   // di will be our drop item list iterator
		DropItem drop; // while drop stays as the reference point.
		int n = 0;
		bool nomonsters = sv_nomonsters || level.nomonsters;

		drop = di = GetDropItems();
		if (di != null)
		{
			while (di != null)
			{
				bool shouldSkip = (di.Name == 'None') || (nomonsters && IsMonster(di));
				
				if (!shouldSkip)
				{
					int amt = di.Amount;
					if (amt < 0) amt = 1; // default value is -1, we need a positive value.
					n += amt; // this is how we can weight the list.
				}

				di = di.Next;
			}
			if (n == 0)
			{ // Nothing left to spawn. They must have all been monsters, and monsters are disabled.
				return 'None';
			}
			// Then we reset the iterator to the start position...
			di = drop;
			// Take a random number...
			n = random[randomspawn](0, n-1);
			// And iterate in the array up to the random number chosen.
			while (n > -1 && di != null)
			{
				if (di.Name != 'None' &&
					(!nomonsters || !IsMonster(di)))
				{
					int amt = di.Amount;
					if (amt < 0) amt = 1;
					n -= amt;
					if ((di.Next != null) && (n > -1))
						di = di.Next;
					else
						n = -1;
				}
				else
				{
					di = di.Next;
				}
			}
			if (di == null)
			{
				return 'Unknown';
			}
			else if (random[randomspawn]() <= di.Probability)	// prob 255 = always spawn, prob 0 = almost never spawn.
			{
				return di.Name;
			}
			else
			{
				return 'None';
			}
		}
		else
		{
			return 'None';
		}
	}
[...] // The rest of the code is omitted for brevity

See also