Counting the Number of Players in an Area - By Raeven0 |
|
This tutorial was last updated on May 27, 2005.
In the following example, we will use a trigger_multiple and a math_counter to count the number of players in an area. With that number, we will activate a game_text that displays the number on your screen. After the entity list, I will explain what all the entities do. This method can also be used to count the number of any entity in a particular entity (more on that later).
Another entity can be used to count the number of players in an area: game_zone_player. However, the math_counter method involves much less entity interaction and is less prone to failure.
Entities used:
trigger_multiple (the area in which you want to count objects)
output OnStartTouch target mc input Add parameter 1
output OnEndTouch target mc input Subtract parameter 1
math_counter
"Name" - mc
output OutValue target mc_case input InValue
logic_case
"Name" - mc_case
"Case 01" - 0
"Case 02" - 1
"Case 03" - 2
"Case 04" - 3
output OnCase01 target mc_timer_* input Disable
output OnCase02 target mc_timer_* input Disable
output OnCase03 target mc_timer_* input Disable
output OnCase04 target mc_timer_* input Disable
output OnCase01 target mc_timer_0 input Enable parameter (none) delay 0.01
output OnCase02 target mc_timer_1 input Enable parameter (none) delay 0.01
output OnCase03 target mc_timer_2 input Enable parameter (none) delay 0.01
output OnCase04 target mc_timer_3 input Enable parameter (none) delay 0.01
logic_timer
"Name" - mc_timer_0
"Refire Interval" - 0.8
output OnTimer target mc_text_0 input Display
logic_timer
"Name" - mc_timer_1
"Refire Interval" - 0.8
output OnTimer target mc_text_1 input Display
logic_timer
"Name" - mc_timer_2
"Refire Interval" - 0.8
output OnTimer target mc_text_2 input Display
logic_timer
"Name" - mc_timer_3
"Refire Interval" - 0.8
output OnTimer target mc_text_3 input Display
game_text
"Name" - mc_text_0
"Message Text" - There are no players in this area.
"Fade In Time" - 0
"Fade Out Time" - 0
"Hold Time" - 1
game_text
"Name" - mc_text_1
"Message Text" - There is 1 player in this area.
"Fade In Time" - 0
"Fade Out Time" - 0
"Hold Time" - 1
game_text
"Name" - mc_text_2
"Message Text" - There are 2 players in this area.
"Fade In Time" - 0
"Fade Out Time" - 0
"Hold Time" - 1
game_text
"Name" - mc_text_3
"Message Text" - There are 3 players in this area.
"Fade In Time" - 0
"Fade Out Time" - 0
"Hold Time" - 1
In this setup, every time a player enters the trigger_multiple, he fires its OnStartTouch output. This causes the math_counter's value to be upped by 1. Every time a player leaves the trigger_multiple, its OnEndTouch output is fired, and the math_counter's value is lowered by 1.
The math_counter's OutValue input sends its current value to a logic_case every time that value is changed. So, if the value is 1 and a player enters the trigger_multiple, the math_counter's value will be changed to 2, and it will send the message "Hey! My value is now 2!" to the logic_case.
The logic_case accepts the math_counter's new value and compares that value to one of its 16 cases. (Remember that the value is the number of players in the zone.) In this example, if the received value is 1 (meaning there is 1 player inside the trigger_multiple), then the OnCase02 output is fired.
From there, each OnCase## is linked to a logic_timer, and each logic_timer simply forces a game_text to display. The game_texts contain the message telling how many players are in the area. So, if there are no players inside the trigger_multiple and a player walks into it:
- the trigger_multiple's OnStartTouch output raises the value of the math_counter to 1;
- the math_counter sends an InValue 1 input to the logic_case;
- the logic_case compares 1 to all of its cases, and fires the OnCase## that matches;
- in this example, OnCase02 is fired, disabling all active timers and enabling mc_timer_1;
- in this example, mc_timer_1 causes mc_text_1 to display its text until the timer itself is disabled.
Note that only the first three entities--the trigger_multiple, math_counter, and logic_case--are actually relevant to counting the players. The logic_timers and game_texts were only used in this example to provide an example of how the effect could be used.
But wait! There's more!
What if you want to count, say, the number of barrels inside the trigger_multiple, or the number of grenades? Well, all you have to do is change the trigger_multiple's flags and perhaps give it an appropriate filter entity. This will cause entities besides players to trigger OnStartTouch and OnEndTouch, meaning the math_counter will not count players, but whatever activates the trigger_multiple.
Note that, by default, spectators and dead players will not be counted.
DOWNLOAD EXAMPLE MAP (17KB)
Still need help? Ask your questions in one of our HELP FORUMS
|
|
|
| Comments |
| anonymous - Jun 28, 2006 |
| awesome tutorial! i love this more advanced ones! |
|
|
|