PlayerCount

From ZDoom Wiki
Jump to navigation Jump to search

int PlayerCount (void)

Usage

Returns the number of players currently in the game. For single-player games, this will always be 1. For multi-player games, it can also return 1 when all but one of the players has quit the game.

Examples

A common issue is how to check if all players have entered a certain area. That is, if the number of players that have entered the area is equal to the result of PlayerCount.

Using “Actor enters sector” and “Actor leaves sector” things, it is possible to implement a primitive counter.

int count = 0;

// Use for Actor enters sector
script 10 (void)
{
	count++;
}

// Use for Actor leaves sector
script 11 (void)
{
	count--;
}

script 100 OPEN
{
	while (count < PlayerCount())
		Delay(35);
	
	PrintBold(s:"All players ready!");
	
	//etc.
}

This counter will keep track of the number of players currently in the sector and when script 100 finds the counter to equal PlayerCount, a special action occurs.

Note that this does not account for such events as players dying in the sector, or players disconnecting from the game in the sector. It is possible to prevent these problems by using an array of boolean variables, one for each player, and checking the total against PlayerCount. DEATH and DISCONNECT scripts can reset the particular player's state.


Another possible use of this function is to perform certain actions depending on player count.

script 12 (void)
{
       if (PlayerCount()>=6)
               Thing_Spawn(1, T_CYBERDEMON, 0, 0);
}

This script 12 would spawn a Cyberdemon only in case there are 6 or more of active players on the server.

script 13 (void)
{
	for (int i=0;i<PlayerCount();i++)
		Thing_Spawn(i+2000, T_BARON, 0, 0);
}

Script 13 would spawn as many Barons of Hell as number of active players on the server. Barons will be spawned in mapspots tagged 2000, 2001, 2002, etc so you have to create them on your map. Note that if the spawning location is blocked by heights of sectors or other objects, not all of Barons might be spawned. Consider tagging Barons with the last argument of Thing_Spawn function and using ThingCount to calculate how many of them were actually created to spawn the rest afterwards.