From ZDoom Wiki
Jump to navigation Jump to search

int ThingCount (int type, int tid)


ThingCount counts all things specified on the map. You may specify a type of monster via spawn numbers, a specific TID, or both (monsters with a specific TID).

ThingCount will not count dead monsters, even though their things still exist and can be used, for instance, by SpawnSpot.

Note that for this function to count a specific type of actor, it will need to have a spawn number assigned. If you need to count actors without a SpawnID, use the alternative ThingCountName function instead.

If the type parameter is T_NONE, ThingCount will count all actors with the given TID, regardless of their type. Inversely, if the tid parameter is 0, it will count all actors of the given type, regardless of TID.


For example let's say you have a map with 10 enemies:

Imp - tid 5
Imp - tid 5
Imp - tid 0
Baron - tid 5
Baron - tid 5
Baron - tid 4
Baron - tid 0
Demon - tid 5
Demon - tid 4
Demon - tid 0

Here are some example values:

ThingCount(T_IMP, 0) = 3
ThingCount(T_BARON, 0) = 4
ThingCount(T_DEMON, 0) = 3
ThingCount(T_NONE, 5) = 5
ThingCount(T_NONE, 4) = 2
ThingCount(T_IMP, 5) = 2
ThingCount(T_DEMON, 4) = 1
ThingCount(T_NONE, 0) = 10
ThingCount(T_IMP, 4) = 0

This script simulates the behavior of E1M8. It waits for all the Barons with tag 10 to be defeated. Once they are, it lowers a sector tagged 15 to the lowest adjacent floor.

script 1 OPEN
	While(ThingCount(T_BARON, 10) > 0)
	Floor_LowerToLowest(15, 20);

This script is open so it's run from the start of the map. Alternatively it could be modified to run upon crossing a line and made more general. The while loop just keeps executing the delay command whilst there are still Barons on the level. The reason for this is there is no need for the script to check every single frame whether the barons are dead. It delays so that it checks only once per second, which might speed things up a very small amount. Once all the Barons die, the while loop stops and the floor gets lowered.

This script shows that ThingCount can be used effectively with Spawn and SpawnSpot, as both of these allow you to add a TID to the spawned thing. It takes the TID of a teleporter spot as its parameter.

script 1 (int teletid)
	SpawnSpot("Revenant", teletid, 100, 0);
	While(ThingCount(T_REVENANT, 100) > 0)
	Print(s:"You have beaten the revenant!");

First it spawns a Revenant with a (hopefully) unique tid of 100. Afterwards it delays whilst the revenant is still alive. Once the revenant is dead, it shows the player they have beaten it (as if they wouldn't know).

This script displays a counter of how many of a group monsters you must defeat before you have won. It takes a parameter, which is the TID of the group of monsters

script 5 (int armytid)
	Thing_Hate(armytid, 0, 0);
	While (ThingCount(0, armytid) > 0)
		HudMessage(s:"Defeat ", d:ThingCount(0, armytid), s:" more monsters!";
			HUDMSG_PLAIN, 1, CR_RED, 0.5, 0.9, 2.0);
		HUDMSG_PLAIN, 1, CR_GOLD, 0.5, 0.9, 2.0);

This demonstrates the usefulness of using a monster type of 0.

See also