Detect Unit created or unit dies

Level 1
Joined
Nov 12, 2024
Messages
2
Having trouble with leadboard, I have created a leadboard listing all the players and display their current unit count. I am having trouble when any of the player creates a unit, whether it be a unit trained or summoned by hero ability, it doesn't add up the that player's unit count. But, when a unit dies, either a unit or structure, it manages to deduct it from the unit count in the leadboard.
Variables:
Leadboard - Leadboard
TempGroup - (Unit Group)
UnitCount - (Integer) (Array)

  • Leadboard
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Leaderboard - Create a leaderboard for (All players) titled Living Units
      • Set Leadboard = (Last created leaderboard)
      • Player Group - Pick every player in (All players) and do (Actions)
        • Loop - Actions
          • Leaderboard - Add (Picked player) to Leadboard 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
              • Set UnitCount[(Player number of (Picked player))] = (UnitCount[(Player number of (Picked player))] + 1)
          • Leaderboard - Change the value for (Picked player) in Leadboard to UnitCount[(Player number of (Picked player))]
          • Custom script: call DestroyGroup(udg_TempGroup)
  • Unit 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 (Picked player) in Leadboard to UnitCount[(Player number of (Picked player))]
  • Unit Dies
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • Set UnitCount[(Player number of (Owner of (Triggering unit)))] = (UnitCount[(Player number of (Owner of (Triggering unit)))] - 1)
      • Leaderboard - Change the value for (Owner of (Triggering unit)) in Leadboard to UnitCount[(Player number of (Owner of (Triggering unit)))]
  • [/TRGGER]
  • The Unit Enters trigger is not detecting if a unit is created by a player, whether it's a structure or a unit.
 
Last edited:
There's also another problem with your triggers.
It takes any unit reappearing or getting out of a transport as a new unit.
You better use a hashtable to track if the unit has been already counted, and it should be good.
However, you also have to take in account unit resurrection ; unless you don't have these kind of events in your map at all.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
There's also another problem with your triggers.
It takes any unit reappearing or getting out of a transport as a new unit.
You better use a hashtable to track if the unit has been already counted, and it should be good.
However, you also have to take in account unit resurrection ; unless you don't have these kind of events in your map at all.
That's not true, "Enters the map" only happens upon creation. Hiding/Showing a unit, which is what happens with Transports, doesn't fire the "enters" Event. Resurrection doesn't have this issue either.

There may be some way to get it to run again for a Unit but I've yet to see it.
 
That's not true, "Enters the map" only happens upon creation. Hiding/Showing a unit, which is what happens with Transports/Loading doesn't fire this Event again.

There may be some way to get it to run again for a Unit but I've yet to see it.
Was I lying to myself from all this time ?
Dayum I stand corrected.
 
Level 29
Joined
Sep 26, 2009
Messages
2,594
You are both correct. Old version triggered the 'unit enters region' event, but it no longer works that way in modern game. Dunno which version it was, but I know for a fact that I used that event to keep track of units that were loaded into/unloaded from a trebuchet unit in my map, in order to determine if trebuchet can attack or not.
 
Level 1
Joined
Nov 12, 2024
Messages
2
I was able to get it working with by changing it to 'Owner of Triggering Unit'. But now what I'm having trouble is that I have pre placed units, such as guard towers and some other structure type units. It's not being included in the initialized count of units in the Leadboard trigger.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
I was able to get it working with by changing it to 'Owner of Triggering Unit'. But now what I'm having trouble is that I have pre placed units, such as guard towers and some other structure type units. It's not being included in the initialized count of units in the Leadboard trigger.
You should already be counting them in your setup trigger at the 0.00 second mark:
  • Leadboard
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Leaderboard - Create a leaderboard for (All players) titled Living Units
      • Set Leadboard = (Last created leaderboard)
      • Player Group - Pick every player in (All players) and do (Actions)
        • Loop - Actions
          • Set PN = (Player number of (Picked player))
          • Custom script: set udg_PlayerUnits[udg_PN] = CreateGroup()
          • Set PlayerUnits[PN] = (Units owned by (Picked player) matching (((Matching unit) is alive) Equal to True))
          • Set UnitCount[PN] = (Number of units in PlayerUnits[PN])
          • Leaderboard - Add (Picked player) to Leadboard with label (Name of (Picked player)) and value UnitCount[PN]
  • Unit Enters
    • Events
      • Unit - A unit enters (Entire map)
    • Conditions
    • Actions
      • Set PN = (Player number of (Owner of (Triggering unit)))
      • Set UnitCount[PN] = (UnitCount[PN] + 1)
      • Unit Group - Add (Triggering unit) to PlayerUnits[PN]
      • Leaderboard - Change the value for (Owner of (Triggering unit))) in Leadboard to UnitCount[PN]
  • Unit Dies
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • Set PN = (Player number of (Owner of (Triggering unit)))
      • Set UnitCount[PN] = (UnitCount[PN] - 1)
      • Unit Group - Remove (Triggering unit) from PlayerUnits[PN]
      • Leaderboard - Change the value for (Owner of (Triggering unit))) in Leadboard to UnitCount[PN]
^ I added some more Actions and Variables to the triggers to allow tracking the units in their own Unit Groups. This way you can always interact with the units in an efficient manner. PN is an Integer, PlayerUnits is a Unit Group array.

If this somehow doesn't count the pre-placed units then those units must not exist at the 0.00 second mark. Also, if you "Remove" units from the game they will not fire the "A unit Dies" Event and will screw up the counting. For example, this Action bypasses the death system:
  • Actions
    • Unit - Remove (Some unit) from the game
^ Make sure you Kill it first or just never Add/count it in the first place.
 
Last edited:
Top