Snippet Damage Counter

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Damage Counter
By Gtam

This is a nice little snippet.

Requires:
NewGen
AIDS made by Jesus4Lyf
Damage made by Jesus4Lyf
Event made by Jesus4Lyf

This snippet can detect how many damage a unit has taken over a period of time.

Use the function DamageCountAddUnit(whichunit) to start counting all the damage the unit took
Use the function GetDamageCount(whichunit) to get the amount whichunit has taken for adding to the function call
Use the function ResetDamageCount(whichunit) to set the damagecount of a unit to back to 0.

Here is the snippet

JASS:
library DamageCounter initializer DC_Init requires AIDS, Damage, Event
//======================================================================================\\
//======================================================================================\\
//== Damage Counter v1.06                                                             ==\\
//==             Created by Gtam                                                      ==\\
//== Functions                                                                        ==\\
//==   DamageCountAddUnit() takes unit.                                               ==\\
//==     Use this function to add a unit and start counting the taken damage          ==\\
//==   ResetDamageCount() takes unit.                                                 ==\\
//==     Use this to set the damage counter of a unit to 0                            ==\\
//==   GetDamageCount() takes unit returns real                                       ==\\
//==     Use this to get the current damage taken by a unit                           ==\\
//==   DamagecountRemoveUnit() takes unit                                             ==\\
//==     Use this to remove a unit from counting damage                               ==\\
//======================================================================================\\
//======================================================================================\\
//== Dont Touch
globals
    group DCG = CreateGroup()
    trigger DCT = CreateTrigger()
endglobals

    struct DC extends array
        //! runtextmacro AIDS()
        
        real Damage
        
        method AIDS_onCreate takes nothing returns nothing
            set this.Damage = 0
        endmethod
    endstruct
    
    private function DC_Actions takes nothing returns nothing
        local DC d = DC[GetTriggerUnit()]
        if IsUnitInGroup( GetTriggerUnit(), DCG) == true then
            set d.Damage = d.Damage + GetEventDamage()
        endif
    endfunction
    
    private function DC_Init takes nothing returns nothing
        call Damage_RegisterEvent(DCT)
        call TriggerAddAction( DCT, function DC_Actions)
    endfunction

    function DamageCountAddUnit takes unit added returns nothing
        if IsUnitInGroup(added, DCG) == false then
            call GroupAddUnit(DCG, added)
        endif
    endfunction

    function GetDamageCount takes unit damage returns real
        return DC[damage].Damage
    endfunction
    
    function ResetDamageCount takes unit reset returns nothing
        set DC[reset].Damage = 0
    endfunction
    
    function DamageCountRemoveUnit takes unit remove returns nothing
        if IsUnitInGroup(remove, DCG) == true then
            call GroupRemoveUnit(DCG, remove)
        endif
    endfunction
endlibrary



Hope its help full.
Credits not needed incase you wanted to know
Code:
Changelog
v1.00 Released
v1.01 Spell target added
v1.02 Complete revamp
v1.03 Uses only 1 trigger now
v1.04 Leakless
v1.05 AIDS Struct
v1.06 Little update
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
I'm not sure, but I think it is useless to put you static methods private, since they are already in a private struct.

Also, GetUnitIndex comes from where?!:questionmark:

If it comes from AIDS, PUI, IRIS, then you must add it in your requirements, for example;
JASS:

library Helloworld requires IRIS
endlibrary
/*or*/
library Hellwolrd initializer whateverman requires AIDS
endlibrary


EDIT: Didn't see it was using AIDS

In that case your library should be named...

JASS:

library DamageCounter requires AIDS


BUT your code shouldn't work now, it's initializer is not even set. I think your lib's name should now be

JASS:

library DamageCounter initializer onInit requires AIDS


Did you test your parse before putting it here?!:questionmark:

EDIT(2):
Could you add a way to have multiple damage calc. on the same unit? Could you also add a way to destroy a damage storer, not only stop it?

WHY don't you create a new handle type, like damagecalc. (etc...)

So many questions...
 

Jesus4Lyf

Good Idea™
Reaction score
397
>Coz editing is fun
Lol. :p

Unfortunately, your struct is not a struct. So let it be structless!

Good to see AIDS in use for unit attaching.

I'm not sure that this snippet serves a purpose. Seems very vague and concentrated (obscure purpose?), and if you used Damage and an AIDS struct it could be accomplished in maybe 20 lines. :confused:
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
i dont just create and release i also test it in my snippet map. Why would you not just want to stop it when you add the unit again it starts at 0. Dont see point of destroying. The purpose is if you want to deal extra damage over damage took in 10 seconds this would be used
 

Flare

Stops copies me!
Reaction score
662
This snippet can detect how many damage a unit has taken over a period of time.
No it can't. The best that it can do is allow you to approximate the damage dealt by physical attacks (although, varying attack damage and armor figures will make your approximation wrong, more than likely), but that would be in an ideal situation where every execution of EVENT_PLAYER_UNIT_ATTACKED resulted in a unit being hit by a physical attack (no misses, no damage reductions, no stopping the animation before the attack was carried out)

The purpose is if you want to deal extra damage over damage took in 10 seconds this would be used
Slight problem here though - the system only tracks the number of times the tracked unit might have been the victim of a physical attack (spells aren't tracked unless they are autocast attack modifiers, but they aren't proper spells anyway :D). I stress the 'might' because EVENT_PLAYER_UNIT_ATTACKED is registered when the attack animation begins (or approximately that point).

If a player gets out of range of the attacker quickly, the attack will stop and the attacker will follow the victim. That gives us +1 to our counter, but (1) no attack was made and, more importantly, (2) no damage was taken.

Second situation - I'm attacking a target, and I bash the S key into oblivion. My unit registers the event, but none of the attacks are being followed through with (since I'm cancelling them half-way). This leaves us with a big increase to the counter, but no damage done.


If this was updated to actually track the damage done, it might have some use but, until then, I can't think of a single practical use. If/when this is done, the addition of a Reset function (which would just reset the counter to 0, without removing the unit from tracking) would be useful - as it is now, you have to do
JASS:
call DamageCountRemoveUnit (myUnit)
call DamageCountAddUnit (myUnit)

when you could simply add another function that does nothing more than set the counter to 0 (and should be inline-friendly, me thinks)
JASS:
function DamageCountResetUnit takes unit u returns nothing
  set DamageCount[GetUnitIndex (u)] = 0
endfunction

which would leave you with
JASS:
call DamageCountResetUnit (myUnit)
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
I tested it and it did what i said it does
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Are you sure that the right code is posted here?

From what I see right now, the code shawn here couldn't even be saved with jasshelper... :thdown:
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Well mine saves fine what error do you get

Do you have AIDS? by Jesus4Lyf
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Well maybe it work but Technicly, when you use another system inside of a library, you must tell it.
JASS:
library DamageCounter initializer onInit requires AIDS


EDIT: Why didn't you change the code with what we proposed?
It shouldn't even work ingame, your initializer function is not even told.
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Well maybe it work but Technicly, when you use another system inside of a library, you must tell it.
JASS:
library DamageCounter initializer onInit requires AIDS

Why when it works fine and why the initializer onInit?

Edit: Change What?
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Well, you need to tell Jasshelper which function to run on initialization. So you put initializer *A name*

Jasshelper will search for this function or method and make it run on initialization.
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Thats why its a method and to anwser your questions

>Slight problem here though - the system only tracks the number of times the tracked unit might have been the victim of a physical attack (spells aren't tracked unless they are autocast attack modifiers, but they aren't proper spells anyway ). I stress the 'might' because EVENT_PLAYER_UNIT_ATTACKED is registered when the attack animation begins (or approximately that point).

Thats why i have the event Unit Damaged

>If a player gets out of range of the attacker quickly, the attack will stop and the attacker will follow the victim. That gives us +1 to our counter, but (1) no attack was made and, more importantly, (2) no damage was taken.

Again the Event Unit Damaged

>Second situation - I'm attacking a target, and I bash the S key into oblivion. My unit registers the event, but none of the attacks are being followed through with (since I'm cancelling them half-way). This leaves us with a big increase to the counter, but no damage done.

Again the Event Unit Damaged and i only add The Damage Taken and the Unit Attaked Event will return 0 so it will add 0.

>Why didn't you change the code with what we proposed?
It shouldn't even work ingame, your initializer function is not even told.

I Think it work better the way it is.
 

Flare

Stops copies me!
Reaction score
662
Well, you need to tell Jasshelper which function to run on initialization. So you put initializer *A name*

Jasshelper will search for this function or method and make it run on initialization.
Scope/library initializers are purely optional. The group/trigger are being initialized within the onInit method (which works like a scope/library initializer, other than the fact that it's a pre-existing method that all structs have, which the end user would override if they wish to make use of it. The manual has some clear examples of libraries with and without initializers

Thats why its a method and to anwser your questions

>Slight problem here though - the system only tracks the number of times the tracked unit might have been the victim of a physical attack (spells aren't tracked unless they are autocast attack modifiers, but they aren't proper spells anyway ). I stress the 'might' because EVENT_PLAYER_UNIT_ATTACKED is registered when the attack animation begins (or approximately that point).

Thats why i have the event Unit Damaged

>If a player gets out of range of the attacker quickly, the attack will stop and the attacker will follow the victim. That gives us +1 to our counter, but (1) no attack was made and, more importantly, (2) no damage was taken.

Again the Event Unit Damaged

>Second situation - I'm attacking a target, and I bash the S key into oblivion. My unit registers the event, but none of the attacks are being followed through with (since I'm cancelling them half-way). This leaves us with a big increase to the counter, but no damage done.

Again the Event Unit Damaged and i only add The Damage Taken and the Unit Attaked Event will return 0 so it will add 0.

>Why didn't you change the code with what we proposed?
It shouldn't even work ingame, your initializer function is not even told.

I Think it work better the way it is.
Well, when I examined the code, I'm 99% sure that I saw EVENT_PLAYER_UNIT_ATTACKED... either it was editted while I was typing the post (or after), or I'm losing it xD

EDIT: Ah, the EVENT_PLAYER_UNIT_ATTACKED event is being used, just not for tracking the hits - my apologies for making that mistake :eek:
Although, I would suggest that units should not be automatically registered once they are attacked - IMO, registration through the event should require a unit to fulfill certain conditions (say, being a hero), and put this filter towards the top of the script, where end-users can easily alter it to suit their purposes. Then, if other units need to be added (for whatever reason, perhaps they were targeted by some spell), it can be done manually.

Automated registration (without any restrictions) will result in a bunch of unnecessary events when units die and their corpses decay.
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
Automated registration (without any restrictions) will result in a bunch of unnecessary events when units die and their corpses decay.
In all fairness though, it's also been argued that event leaks are so negligible that using dynamic triggers probably degrades performance more under most practical conditions.

In my experience with that: it really doesn't matter one bit either way.


The big issue with the event registration here, actually, has more to do with the fact that a unit has to be attacked in order for it to be registered. In other words, if you pound a unit with 10 MILLION FIREBALL SPELLS OF DOOOOOM, but never attack it, it won't register any damage.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Why don't you just GroupEnumUnitsInRect(Gtam, bj_mapInitialPlayableArea)
and when a unit enters the map... GroupAddUnit(Gtam, GetTriggerUnit())
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
In all fairness though, it's also been argued that event leaks are so negligible that using dynamic triggers probably degrades performance more under most practical conditions.

In my experience with that: it really doesn't matter one bit either way.


The big issue with the event registration here, actually, has more to do with the fact that a unit has to be attacked in order for it to be registered. In other words, if you pound a unit with 10 MILLION FIREBALL SPELLS OF DOOOOOM, but never attack it, it won't register any damage.

But when he is attacked the all spells after that registers as damage taken so if a unit is attacked the first time all the spells after register only the spells casted before the first time and the is only one the second will register
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
So you're saying that you're okay with a gaping functionality hole because the bug won't occur on the majority of cases?

This wouldn't even be an issue if you just used an AIDS struct.
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Im not saying that im busy fixing it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top