Snippet Show Damage Text (Extra options)

Exfyre

hmm...
Reaction score
60
What it does:
When a unit is damaged, this will show the damage (if it is greater than or equal to one). Setting the options will alow you to select which damage the player will be able to see.

Notes:
Jass Newgen Pack is required (or a similar vJass compiler)
Works for both units already placed on the map and units added to the map via triggers
Works with 1.24 patch

Instructions on use:
1. copy the code below
2. create a trigger in your map
3. convert the trigger to custom text
4. erase everything in the trigger
5. paste this code there
6. customize options

Options:
boolean showToAllPlayers = true (When set to false, text will only show to the player dealing the damage)

boolean showToSelected = false (When set to true, only units who are selected by the player will show their damage and showToAllPlayers must be false)
^-boolean bothWays = false (When set to true, AND showToSelected is true, this will show both the damage received and the damage dealt by the selected unit. (default of false shows only damage dealt))

Old code: (standalone but leaky)
JASS:
scope DDetector initializer Init
globals
    public boolean showToAllPlayers = true
    //showToAllPlayers: When set to false, text will only show to the player dealing the damage
    public boolean showToSelected = false
    //showToSelected: When set to true, only units who are selected by the player will show their damage
    //                and showToAllPlayers must be false
    public boolean bothWays = false
    //bothWays: When set to true, AND showToSelected is true, this will show both the damage received and
    //          the damage dealt by the selected unit.  (default of false shows only damage dealt)
    private constant integer red = 255
    private constant integer green = 0
    private constant integer blue = 0
    //red, green, and blue:  Set out of 255 for text color
    //CUSTOM AREA OVER
    private integer i
endglobals

private function cc takes nothing returns boolean
    local unit u = GetFilterUnit()
    local boolean b = IsUnitSelected(u, Player(i))
    set u = null
    return b
endfunction

private function Act takes nothing returns nothing
    local texttag t = CreateTextTag()
    local group g
    local unit source = GetEventDamageSource()
    local unit hurt = GetTriggerUnit()
    if GetEventDamage() >= 1 then
        set i = 0
        loop
            set g = CreateGroup()
            call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, Condition(function cc))
            if ((GetLocalPlayer() == GetOwningPlayer(source) and showToSelected == false) or showToAllPlayers == true) or (showToSelected == true and IsUnitInGroup(source, g)) or (bothWays == true and showToSelected == true and (IsUnitInGroup(hurt, g) or IsUnitInGroup(source, g))) then
                call SetTextTagText(t, I2S(R2I(GetEventDamage())), .023)
                call SetTextTagPos(t, GetUnitX(hurt), GetUnitY(hurt), 0)
                call SetTextTagColor(t, red, green, blue, 255)
                call SetTextTagFadepoint(t, 2)
                call SetTextTagLifespan(t, 3)
                call SetTextTagPermanent(t, false)
                call SetTextTagVelocity(t, 0, 0.071 / 2)
            endif
            call DestroyGroup(g)
            exitwhen i == 7
            set i = i+1
        endloop
    endif
    set t = null
    set source = null
    set hurt = null
    set g = null
endfunction

private function Actions takes nothing returns nothing
    local trigger tt = CreateTrigger()
    call TriggerRegisterUnitEvent(tt, GetTriggerUnit(), EVENT_UNIT_DAMAGED)
    call TriggerAddAction(tt, function Act)
endfunction

private function z2 takes unit u returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterUnitEvent(t, u, EVENT_UNIT_DAMAGED)
    call TriggerAddAction(t, function Act)
    set u = null
    return
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local trigger tt = CreateTrigger()
    local group g = CreateGroup()
    local unit u
    local region rectRegion = CreateRegion()
    call RegionAddRect(rectRegion, bj_mapInitialPlayableArea)
    call TriggerRegisterEnterRegion(tt, rectRegion, null)
    call TriggerAddAction(tt, function Actions)
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)
        call z2(u)
    endloop
    call DestroyGroup(g)
    set g = null
    set u = null
endfunction

endscope

New Code: Uses Jesus4lyf's Damage
Required:
Jass NewGen
Damage
AIDS (link is on Damage post)
Event (link is on Damage post)
JASS:
scope DDetector initializer Init
globals
    public boolean showToAllPlayers = true
    //showToAllPlayers: When set to false, text will only show to the player dealing the damage
    public boolean showToSelected = false
    //showToSelected: When set to true, only units who are selected by the player will show their damage
    //                and showToAllPlayers must be false
    public boolean bothWays = false
    //bothWays: When set to true, AND showToSelected is true, this will show both the damage received and
    //          the damage dealt by the selected unit.  (default of false shows only damage dealt)
    private constant string physicalColor = "|cffff0000"
    //physicalColor:  Hex color code for physical damage
    private constant string spellColor = "|cff7777aa"
    //physicalColor:  Hex color code for spell damage
    private constant real fadepoint = 2
    private constant real lifespan = 3
    private constant real size = 10
    //CUSTOM AREA OVER
    private integer i
    private group g = CreateGroup()
endglobals

private function cc takes nothing returns boolean
    return IsUnitSelected(GetFilterUnit(), Player(i))
endfunction

private function Act takes nothing returns nothing
    local texttag t = CreateTextTag()
    local unit source = GetEventDamageSource()
    local unit hurt = GetTriggerUnit()
    local string color
    if Damage_IsPhysical() then
        set color = physicalColor
    elseif Damage_IsSpell() then
        set color = spellColor
    endif
    if GetEventDamage() >= 1 then
        set i = 0
        loop
            call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, Condition(function cc))
            if ((GetLocalPlayer() == GetOwningPlayer(source) and showToSelected == false) or showToAllPlayers == true) or (showToSelected == true and IsUnitInGroup(source, g)) or (bothWays == true and showToSelected == true and (IsUnitInGroup(hurt, g) or IsUnitInGroup(source, g))) then
                call SetTextTagText(t, color + I2S(R2I(GetEventDamage())), size * 0.023 / 10)
                call SetTextTagPos(t, GetUnitX(hurt), GetUnitY(hurt), 0)
                call SetTextTagFadepoint(t, fadepoint)
                call SetTextTagLifespan(t, lifespan)
                call SetTextTagPermanent(t, false)
                call SetTextTagVelocity(t, 0, 0.071 / 2)
            endif
            exitwhen i == 7
            set i = i+1
        endloop
    endif
    set t = null
    set source = null
    set hurt = null
    set g = null
endfunction

private function Init takes nothing returns nothing
    local trigger tt = CreateTrigger()
    call TriggerAddAction(tt, function Act)
    call Damage_RegisterEvent(tt)
endfunction

endscope
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
I already created this system but with Special effects on attacks like blood and that that could easily be disabled
 

RaiJin

New Member
Reaction score
40
um... first of all i would say, use a global group for all group actions, use a global unit and, this leaks events like Jesus4lyf stated in my Quick Damage Detection system..

EDIT: wow lol your creating a lot of dynamic triggers there...
 

Exfyre

hmm...
Reaction score
60
um... first of all i would say, use a global group for all group actions, use a global unit
why? groups and units are destroyed & nulled later anyway

and, this leaks events like Jesus4lyf stated in my Quick Damage Detection system..
how so?

EDIT: wow lol your creating a lot of dynamic triggers there...
yeah. should I use a single global trigger and just add events for each unit?
 

Exfyre

hmm...
Reaction score
60
<3 your code Jesus4lyf. Wish I'd had this a few days ago. ty

EDIT: I changed my snippet to use your system
 
Reaction score
341
Here it goes;

  • Use a global group, as of now your dynamically creating them which is defiantly slower than re-using a global. Not to mention is saves you three lines.
  • Your filter function is rubbish, inline it.
    JASS:
    private function cc takes nothing returns boolean
        return IsUnitSelected(GetFilterUnit(), Player(i))
    endfunction
  • Why do you have three configurables public?
  • You should add a configurable for texttag size as well as transparency (if texttags support that).
  • Make the fadepoint configurable too.
  • Just use R2S..
    JASS:
    call SetTextTagText(t, I2S(R2I(GetEventDamage())), .023) // bad
    call SetTextTagText(t, R2S(GetEventDamage(), .023) // good

 

Exfyre

hmm...
Reaction score
60
Use a global group, as of now your dynamically creating them which is defiantly slower than re-using a global. Not to mention is saves you three lines.
Will do
Your filter function is rubbish, inline it.
JASS:

private function cc takes nothing returns boolean
    return IsUnitSelected(GetFilterUnit(), Player(i))
endfunction
ahh, k
Why do you have three configurables public?
so they can be changed while in-game
You should add a configurable for texttag size as well as transparency (if texttags support that).
Make the fadepoint configurable too.
Alright

Just use R2S..
JASS:

call SetTextTagText(t, I2S(R2I(GetEventDamage())), .023) // bad
call SetTextTagText(t, R2S(GetEventDamage(), .023) // good
not so. casting a real to a string also leaves behind the decimal points, which make for messy text, by casting it to an int first, it truncates the decimal portion.
 

Jesus4Lyf

Good Idea™
Reaction score
397
JASS:
    if Damage_IsPhysical() then
        set color = physicalColor
    elseif Damage_IsSpell() then
        set color = spellColor
    endif

Just thought I'd mention that this will only work if people use Damage's dealing damage functions and such. But nice addition all the same. ^_^ (It defaults to physical otherwise, so no worries.)
 

Exfyre

hmm...
Reaction score
60
Ohh. I see. I wondered why my current spells didnt show different colored text. Thanks for the tip.
 
Reaction score
341
You still haven't inlined the filter and your using the global group wrong. Theres no need to keep creating/destroying/nulling the group.

Just re-use it.
 

quraji

zap
Reaction score
144
It's not inlined :nuts:

And it can't be stored in a global the values change.

It can't be inlined, a boolexpr needs a function to be created (condition/filterfunc/boolexpr are all the same thing).

And it does return the same value every time...not that the difference would be noticeable..but since we're all speed freaks around here apparently..
 

Exfyre

hmm...
Reaction score
60
You still haven't inlined the filter and your using the global group wrong. Theres no need to keep creating/destroying/nulling the group.

Just re-use it.

Oh hehe. I just forgot about that first thing. About the global group, do I need to destroy it eventually or not?
 
Reaction score
341
Nope. Just keep re-using it.I don't know if enumerating units is a group clears the old units in it/

If not you gotta clear the group each time.
 

quraji

zap
Reaction score
144
Inlining is actually what TriggerHappy said.

But it is also what JassHelper does.

Since when is inlining reducing the amount of lines...:p
That function can't be inlined because it is a filter function (to be made into a boolexpr, which needs a function to be created).

(optimizing a function to work in one line instead of several is not inlining [although in vJASS it is a requirement of inlining])
 
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