• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Count every unit owned by player

Status
Not open for further replies.
Level 4
Joined
Jun 23, 2016
Messages
64
...and make the number appear on a leaderboard. The counter should be dynamic, so if a unit dies/a new unit appears, they should be counted


Thanks in advance
 
Level 39
Joined
Feb 27, 2007
Messages
5,010
There is undoubtedly a leaderboard tutorial somewhere in the tutorials section here.

I would update the leaderboard with a periodic trigger where you loop over all players currently in the game (or maybe all non-computer players still in the game if there are cpus). Then use the ‘count living units of <player>’ which returns an integer (and leaks a group). Use this integer for the picked player’s value on the leaderboard. After all values have been updated, sort the leaderboard once with the sort action.

You will have to clean the player group leak of all living players every time the trigger runs (using call DestroyForce(udg_Force_Variable_Name)) or save this force in a global variable on map init and update it dynamically when players leave the game. You can clean the group leaks by doing set bj_wantDestroyGroup = true before counting the units.
 
Level 9
Joined
Sep 20, 2015
Messages
385
You can make 2 triggers.
Create one variable x
One trigger is to check when a unit enters the playabla map. When this happen add a condition to check the owning player, and set the variable to x + 1 and update the leaderboard with new value.
The second trigger check when a unit dies. Set x - 1 , update value on leaderboard.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,537
Variables:
UnitCount (Integer) (Array)
Leaderboard (Leaderboard) (Non-Array)
TempGroup (Unit Group) (Non-Array)
  • Leaderboard
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Leaderboard - Create a leaderboard for (All players) titled Living Units
      • Set Leaderboard = (Last created leaderboard)
      • Player Group - Pick every player in (All players) and do (Actions)
        • Loop - Actions
          • Leaderboard - Add (Picked player) to Leaderboard with label (Name of (Picked player)) and value 0
  • Enters
    • Events
      • Unit - A unit enters (Entire map)
    • Conditions
    • Actions
      • Set UnitCount[(Player number of (Triggering player))] = (UnitCount[(Player number of (Triggering player))] + 1)
      • Leaderboard - Change the value for (Triggering player) in Leaderboard to UnitCount[(Player number of (Triggering player))]
  • Dies
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • Set UnitCount[(Player number of (Triggering player))] = (UnitCount[(Player number of (Triggering player))] - 1)
      • Leaderboard - Change the value for (Triggering player) in Leaderboard to UnitCount[(Player number of (Triggering player))]
Then add conditions to Enters/Dies to filter out units that you don't want to count. For example, triggering unit is a Structure equal to false.

If you have pre-placed units in your map then you can change the Leaderboard trigger to this:
  • Leaderboard
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Leaderboard - Create a leaderboard for (All players) titled Living Units
      • Set Leaderboard = (Last created leaderboard)
      • Player Group - Pick every player in (All players) and do (Actions)
        • Loop - Actions
          • Leaderboard - Add (Picked player) to Leaderboard with label (Name of (Picked player)) and value 0
          • Set TempGroup = (Units owned by (Picked player))
          • Unit Group - Pick every unit in TempGroup and do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                • Then - Actions
                  • Set UnitCount[(Player number of (Picked player))] = (UnitCount[(Player number of (Picked player))] + 1)
                • Else - Actions
          • Leaderboard - Change the value for (Picked player) in Leaderboard to UnitCount[(Player number of (Picked player))]
          • Custom script: call DestroyGroup([udg_TempGroup])
In the If - Conditions you will want to add the conditions that you used in Enters/Dies (if you even used any). For example, Picked Unit is a Structure equal to false.
 
Last edited:
Status
Not open for further replies.
Top