• 🏆 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!

GUI-Friendly Damage Detection v1.2.1

GUI-Friendly Damage Detection
by Weep
Version 1.2.1

-- General Information --
This system provides a leak-free, GUI-friendly implementation of an "any unit takes damage" event. It requires no JASS knowledge to use, nor any other systems, nor any software other than the World Editor. It will not interfere with other systems, either.

It uses the Game - Value Of Real Variable event as its method of activating other triggers, and passes the event responses through a few globals.

Before you copy triggers that use GDD into a new map, you need to copy over GDD with its GDD Variable Creator trigger, or the variables won't be automatically created correctly.
If you pasted GDD-using triggers before pasting GDD, to fix it, change the type of the variable GDD_Event to be a Real in the Trigger Editor's Variables window, and then find the triggers that became disabled and change their event to use the variable GDD_Event.


-- How To Implement --
  1. Be sure "Automatically create unknown variables while pasting trigger data" is enabled in the World Editor general preferences.
  2. Copy the trigger category "GDD" from the demo map and paste it into your map. (Alternately: create the variables listed in the globals block below, create a trigger named GUI Friendly Damage Detection", and paste in this entire text.)
  3. Create your damage triggers using Game - Value Of Real Variable, select GDD_Event as the variable, and leave the rest of the settings to the default "becomes Equal to 0.00".
The event responses are the following variables:
  • GDD_Damage is the amount of damage, replacing Event Response - Damage Taken.
  • GDD_DamagedUnit is the damaged unit, replacing Event Response - Triggering Unit.
    • However, Triggering Unit can still be used, if you need to use waits. Read the Notes section below for more info.
  • GDD_DamageSource is the damaging unit, replacing Event Response - Damage Source.

-- Example Usage --
  • Display Damage
    • Events
      • Game - GDD_Event becomes Equal to 0.00
    • Conditions
    • Actions
      • Game - Display to (All players) for 1.00 seconds the text: ((((Name of GDD_DamageSource) + damaged ) + (Name of GDD_DamagedUnit)) + ( for + ((String(GDD_Damage)) + damage.)))
-- System Code --
JASS:
// GUI-Friendly Damage Detection -- v1.2.1 -- by Weep
//    http://www.thehelper.net/forums/showthread.php?t=137957
//
//    Requires: only this trigger and its variables.
//
// -- What? --
//    This system provides a leak-free, GUI-friendly implementation of an "any unit takes
//    damage" event.  It requires no JASS knowledge to use.
//
//    It uses the Game - Value Of Real Variable event as its method of activating other
//    triggers, and passes the event responses through a few globals.
//
// -- Why? --
//    The traditional GUI method of setting up a trigger than runs when any unit is damaged
//    leaks trigger events.  This system is easy to implement and removes the need to do
//    you own GUI damage detection setup.
//
// -- How To Implement --
//    0. Before you copy triggers that use GDD into a new map, you need to copy over GDD
//       with its GDD Variable Creator trigger, or there will be a problem: the variables
//       won't be automatically created correctly.
//
//    1. Be sure "Automatically create unknown variables while pasting trigger data" is
//       enabled in the World Editor general preferences.
//    2. Copy this trigger category ("GDD") and paste it into your map.
//       (Alternately: create the variables listed in the globals block below, create a
//       trigger named "GUI Friendly Damage Detection", and paste in this entire text.)
//    3. Create your damage triggers using Game - Value Of Real Variable as the event,
//       select GDD_Event as the variable, and leave the rest of the settings to the default
//       "becomes Equal to 0.00".
//       The event responses are the following variables:
//          GDD_Damage is the amount of damage, replacing Event Response - Damage Taken.
//          GDD_DamagedUnit is the damaged unit, replacing Event Response - Triggering Unit.
//              Triggering Unit may still be used, if you need to use waits.
//              Read the -- Notes -- section below for more info.
//          GDD_DamageSource is the damaging unit, replacing Event Response - Damage Source.
//
// -- Notes --
//    GDD's event response variables are not wait-safe; you can't use them after a wait in
//    a trigger.  If you need to use waits, Triggering Unit (a.k.a. GetTriggerUnit()) can
//    be used in place of GDD_DamageSource.  There is no usable wait-safe equivalent to
//    Event Damage or Damage Source; you'll need to save the values yourself.
//
//    Don't write any values to the variables used as the event responses, or it will mess
//    up any other triggers using this system for their triggering.  Only use their values.
//
//    This uses arrays, so can detect damage for a maximum of 8190 units at a time, and
//    cleans up data at a rate of 33.33 per second, by default.  This should be enough for
//    most maps, but if you want to change the rate, change the value returned in the
//    GDD_RecycleRate function at the top of the code, below.
//
//    By default, GDD will not register units that have Locust at the moment of their
//    entering the game, and will not recognize when they take damage (which can only
//    happen if the Locust ability is later removed from the unit.)  To allow a unit to have
//    Locust yet still cause GDD damage events if Locust is removed, you can either design
//    the unit to not have Locust by default and add it via triggers after creation, or
//    edit the GDD_Filter function at the top of the code, below.
//
// -- Credits --
//    Captain Griffin on triggerc.net for the research and concept of GroupRefresh.
//
//    Credit in your map not needed, but please include this README.
//
// -- Version History --
//    1.2.1: Minor code cleaning.  Added configuration functions.  Updated documentation.
//    1.2.0: Made this system work properly with recursive damage.
//    1.1.1: Added a check in order to not index units with the Locust ability (dummy units).
//           If you wish to check for damage taken by a unit that is unselectable, do not
//           give the unit-type Locust in the object editor; instead, add the Locust ability
//           'Aloc' via a trigger after its creation, then remove it.
//    1.1.0: Added a check in case a unit gets moved out of the map and back.
//    1.0.0: First release.


//===================================================================
// Configurables.
function GDD_RecycleRate takes nothing returns real //The rate at which the system checks units to see if they've been removed from the game
    return 0.03
endfunction

function GDD_Filter takes unit u returns boolean //The condition a unit has to pass to have it registered for damage detection
    return GetUnitAbilityLevel(u, 'Aloc') == 0 //By default, the system ignores Locust units, because they normally can't take damage anyway
endfunction

//===================================================================
// This is just for reference.
// If you use JassHelper, you could uncomment this section instead of creating the variables in the trigger editor.

// globals
//  real udg_GDD_Event = 0.
//  real udg_GDD_Damage = 0.
//  unit udg_GDD_DamagedUnit
//  unit udg_GDD_DamageSource
//  trigger array udg_GDD__TriggerArray
//  integer array udg_GDD__Integers
//  unit array udg_GDD__UnitArray
//  group udg_GDD__LeftMapGroup = CreateGroup()
// endglobals

//===================================================================
// System code follows.  Don't touch!
function GDD_Event takes nothing returns boolean
    local unit damagedcache = udg_GDD_DamagedUnit
    local unit damagingcache = udg_GDD_DamageSource
    local real damagecache = udg_GDD_Damage
    set udg_GDD_DamagedUnit = GetTriggerUnit()
    set udg_GDD_DamageSource = GetEventDamageSource()
    set udg_GDD_Damage = GetEventDamage()
    set udg_GDD_Event = 1.
    set udg_GDD_Event = 0.
    set udg_GDD_DamagedUnit = damagedcache
    set udg_GDD_DamageSource = damagingcache
    set udg_GDD_Damage = damagecache
    set damagedcache = null
    set damagingcache = null
    return false
endfunction

function GDD_AddDetection takes nothing returns boolean
//  if(udg_GDD__Integers[0] > 8190) then
//      call BJDebugMsg("GDD: Too many damage events!  Decrease number of units present in the map or increase recycle rate.")
//      ***Recycle rate is specified in the GDD_RecycleRate function at the top of the code.  Smaller is faster.***
//      return
//  endif
    if(IsUnitInGroup(GetFilterUnit(), udg_GDD__LeftMapGroup)) then
        call GroupRemoveUnit(udg_GDD__LeftMapGroup, GetFilterUnit())
    elseif(GDD_Filter(GetFilterUnit())) then
        set udg_GDD__Integers[0] = udg_GDD__Integers[0]+1
        set udg_GDD__UnitArray[udg_GDD__Integers[0]] = GetFilterUnit()
        set udg_GDD__TriggerArray[udg_GDD__Integers[0]] = CreateTrigger()
        call TriggerRegisterUnitEvent(udg_GDD__TriggerArray[udg_GDD__Integers[0]], udg_GDD__UnitArray[udg_GDD__Integers[0]], EVENT_UNIT_DAMAGED)
        call TriggerAddCondition(udg_GDD__TriggerArray[udg_GDD__Integers[0]], Condition(function GDD_Event))
    endif
    return false
endfunction

function GDD_PreplacedDetection takes nothing returns nothing
    local group g = CreateGroup()
    local integer i = 0
    loop
        call GroupEnumUnitsOfPlayer(g, Player(i), Condition(function GDD_AddDetection))
        set i = i+1
        exitwhen i == bj_MAX_PLAYER_SLOTS
    endloop
    call DestroyGroup(g)
    set g = null
endfunction

function GDD_GroupRefresh takes nothing returns nothing
// Based on GroupRefresh by Captain Griffen on triggerc.net
    if (bj_slotControlUsed[5063] == true) then
        call GroupClear(udg_GDD__LeftMapGroup)
        set bj_slotControlUsed[5063] = false
    endif
    call GroupAddUnit(udg_GDD__LeftMapGroup, GetEnumUnit())
endfunction

function GDD_Recycle takes nothing returns nothing
    if(udg_GDD__Integers[0] <= 0) then
        return
    elseif(udg_GDD__Integers[1] <= 0) then
        set udg_GDD__Integers[1] = udg_GDD__Integers[0]
    endif
    if(GetUnitTypeId(udg_GDD__UnitArray[udg_GDD__Integers[1]]) == 0) then
        call DestroyTrigger(udg_GDD__TriggerArray[udg_GDD__Integers[1]])
        set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = null
        set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = udg_GDD__TriggerArray[udg_GDD__Integers[0]]
        set udg_GDD__UnitArray[udg_GDD__Integers[1]] = udg_GDD__UnitArray[udg_GDD__Integers[0]]
        set udg_GDD__UnitArray[udg_GDD__Integers[0]] = null
        set udg_GDD__Integers[0] = udg_GDD__Integers[0]-1
    endif
    set udg_GDD__Integers[1] = udg_GDD__Integers[1]-1
endfunction

function GDD_LeaveMap takes nothing returns boolean
    local boolean cached = bj_slotControlUsed[5063]
    if(udg_GDD__Integers[2] < 64) then
        set udg_GDD__Integers[2] = udg_GDD__Integers[2]+1
    else
        set bj_slotControlUsed[5063] = true
        call ForGroup(udg_GDD__LeftMapGroup, function GDD_GroupRefresh)
        set udg_GDD__Integers[2] = 0
    endif
    call GroupAddUnit(udg_GDD__LeftMapGroup, GetFilterUnit())
    set bj_slotControlUsed[5063] = cached
    return false
endfunction

// ===========================================================================
function InitTrig_GUI_Friendly_Damage_Detection takes nothing returns nothing
    local region r = CreateRegion()
    call RegionAddRect(r, GetWorldBounds())
    call TriggerRegisterEnterRegion(CreateTrigger(), r, Condition(function GDD_AddDetection))
    call TriggerRegisterLeaveRegion(CreateTrigger(), r, Condition(function GDD_LeaveMap))
    call GDD_PreplacedDetection()
    call TimerStart(CreateTimer(), GDD_RecycleRate(), true, function GDD_Recycle)
    set r = null
endfunction

-- Notes --
GDD's event response variables are not wait-safe; you can't use them after a wait in a trigger. If you need to use waits, Triggering Unit (a.k.a. GetTriggerUnit()) can be used in place of GDD_DamageSource. There is no usable wait-safe equivalent to GDD_Damage or GDD_DamageSource; you'll need to save the values yourself.

A common problem when using any damage detection system occurs when you try to cause the damage source to damage the damaged unit from a damage event - for example, if triggering bonus damage. This causes the unit to repeatedly damage the target, because dealing damage in the trigger causes that trigger to run again, and again in turn, infinitely. One solution is to add the action Turn off (This trigger) before dealing damage and add the action Turn on (This trigger) afterward.

Don't write any values to the variables used as the event responses, or it will mess up any other triggers using this system for their triggering. Only use their values.

This uses arrays, so can detect damage for a maximum of 8190 units at a time, and cleans up data at a rate of 33.33 per second, by default. This should be enough for most maps, but if you want to change the rate, change the value returned in the GDD_RecycleRate function at the top of the code.

By default, GDD will not register units that have Locust at the moment of their entering the game, and will not recognize when they take damage (which can only happen if the Locust ability is later removed from the unit.) To allow a unit to have Locust yet still cause GDD damage events if Locust is removed, you can either design the unit to not have Locust by default and add it via triggers after creation, or edit the GDD_Filter function at the top of the code.

-- Credits --
Captain Griffin on triggerc.net for the research and concept of GroupRefresh.

Credit in your map not needed, but please include the README.

-- Version History --
1.2.1: Minor code cleaning. Added configuration functions. Updated documentation.
1.2.0: Made this system work properly with recursive damage.
1.1.1: Added a check in order to not index units with the Locust ability (dummy units). If you wish to check for damage taken by a unit that is unselectable, do not give the unit-type Locust in the object editor; instead, add the Locust ability 'Aloc' via a trigger after its creation, then remove it.
1.1.0: Added a check in case a unit gets moved out of the map and back.
1.0.0: First release.


Keywords:
gui, friendly, damage, detection, detect
Contents

GUI-Friendly Damage Detection (Map)

Reviews
17:29, 16th Jan 2010 TriggerHappy: Very easy to use for GUI users and a fairly unique implementation.
Level 3
Joined
May 15, 2007
Messages
20
hey man ive been using this DDS forever as its simple and fills the most of my needs however i always had a problem that when im attacking a unit and the floating text pops (as it should) then i have a seperate trigger that also deals damage like(critical strike passive) the two floating texts gets spawned at the same time overlapping eachother, any ideas on how to change that ? either combine them two or create one with Z offset or something ?
 
Level 18
Joined
Oct 17, 2012
Messages
820
A central system for floating text would do wonders. This system would attach the floating text to the unit. If the unit currently has a floating text, the system would then increase the height of that floating text or let it fade away, making room for new ones.
 
Level 17
Joined
Jun 2, 2009
Messages
1,125
Hello my friend. Your system is great but i think there is something important missing. How can i tell the trigger that "it is a normal attack?"
For example my hero not only deal damage with auto attacks. All the items and other abilities works when i try to backstab enemy.

  • Events
    • Game - GDD_Event becomes Equal to 0.00
  • Conditions
    • (Facing of GDD_DamageSource) Greater than or equal to ((Facing of GDD_DamagedUnit) - 55.00)
    • (Facing of GDD_DamageSource) Less than or equal to ((Facing of GDD_DamagedUnit) + 55.00)
  • Actions
    • Unit - Cause GDD_DamageSource to damage GDD_DamagedUnit, dealing 200 damage of attack type Spells and damage type
Or is there any function that solves this issue?
 
Level 39
Joined
Feb 27, 2007
Messages
4,994
Your question is not clear, JFAMAP. Could you elaborate? Adiktuz meant that you can have specific units deal damage that you want to ignore, then check to see what unit dealt the damage to see if it should be ignored or not. The simplest example is putting units into a group when you want to ignore their outgoing or incoming damage:
  • -------- doing something --------
  • Unit Group - Add (Triggering Unit) to NO_DAMAGE_EVENTS_GROUP
  • Wait 20.00 seconds
  • Unit Group - Remove (Triggering Unit) from NO_DAMAGE_EVENTS_GROUP
  • Events
    • Game - GDD_Event becomes Equal to 0.00
  • Conditions
    • (GDD_DamageSource is in NO_DAMAGE_EVENTS_GROUP) equal to False
    • (GDD_DamagdUnit is in NO_DAMAGE_EVENTS_GROUP) equal to False
  • Actions
    • -------- will only run if both the damager and the damaged unit are NOT in the unit group --------
You can also do something like this by checking the unit-type of the damage source, and then always using a specific unit type to deal 'undetectable' damage. Usually this would be a dummy unit of some kind.
 
Level 17
Joined
Jun 2, 2009
Messages
1,125
Your question is not clear, JFAMAP. Could you elaborate? Adiktuz meant that you can have specific units deal damage that you want to ignore, then check to see what unit dealt the damage to see if it should be ignored or not. The simplest example is putting units into a group when you want to ignore their outgoing or incoming damage:
  • -------- doing something --------
  • Unit Group - Add (Triggering Unit) to NO_DAMAGE_EVENTS_GROUP
  • Wait 20.00 seconds
  • Unit Group - Remove (Triggering Unit) from NO_DAMAGE_EVENTS_GROUP
  • Events
    • Game - GDD_Event becomes Equal to 0.00
  • Conditions
    • (GDD_DamageSource is in NO_DAMAGE_EVENTS_GROUP) equal to False
    • (GDD_DamagdUnit is in NO_DAMAGE_EVENTS_GROUP) equal to False
  • Actions
    • -------- will only run if both the damager and the damaged unit are NOT in the unit group --------
You can also do something like this by checking the unit-type of the damage source, and then always using a specific unit type to deal 'undetectable' damage. Usually this would be a dummy unit of some kind.
Ok ok let me give you the details.

You have a 1 Hero and you bought item that gives this ability

"your normal attacks (Your hero raises your weapon and hits the enemy. Only works for normal attack. Except for damage done by abilities, skills, spells, other items with any effect like immolation or poison etc etc) deals additional 1.0x agility damage.

Yesterday i have experimented on the GDD system and i have used Mountain King during the tests.
When i cast Storm Bolt system not detects it with this trigger. But...

  • YeniAgiItem Copy 2
    • Events
      • Game - GDD_Event becomes Equal to 0.00
    • Conditions
      • (Current order of GDD_DamageSource) Not equal to (Order(spell))
    • Actions
      • Trigger - Turn off (This trigger)
      • Unit - Cause GDD_DamageSource to damage GDD_DamagedUnit, dealing (1.00 x (Real((Agility of GDD_DamageSource (Include bonuses))))) damage of attack type Chaos and damage type Unknown
      • Trigger - Turn on (This trigger)

When i cast Storm Bolt, this trigger works as i wanted. It is not working with Storm Bolt.
Mountain King damaged peasant for 100 damage OK

But when i cast and move when projectile is on the way, it deals damages like this

Mountain King damaged peasant for 11 damage (my agility) NO
Mountain King damaged peasant for 100 damage OK
Mountain King damaged peasant for 11 damage (my agility) NO


When i will add you to the unit group, this trigger not works for each attack.

I believe we can solve this with our knowledge. This is why i am stubborn with this one.
Yesterday i was downloaded Damage Engine previous version and today i will check it out.
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Fantastic! The only flaw - it has issues with orb effects but in my case it's not a big problem
What specific Orb effect problem are you referring to? Most use Damage Engine these days, and it works perfectly if you copy the 1-2 abilities (depending in if you want spell damage detection). If your map is integrated tightly with this already, Damage Engine also has a plugin to enable compatibility.
 
Level 4
Joined
Sep 21, 2022
Messages
30
What specific Orb effect problem are you referring to? Most use Damage Engine these days, and it works perfectly if you copy the 1-2 abilities (depending in if you want spell damage detection). If your map is integrated tightly with this already, Damage Engine also has a plugin to enable compatibility.
I mean when unit takes some Orb, for example Orb of Fire, Frost etc. if he attacks someone, Orb deals it's own damage. But for me it's not critical - I'll modify all the orbs so they wont work the usual way in the project. I don't know Jass and GUI but modified your trigger so now it creates a floating text near damaged unit that shows how much is it damaged, and it's really cool. I want to make some kind of RPG-like game with dynamic gameplay and smashing hordes of enemies and bosses with controllling of only one hero
 
Top