• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] GetUnitVertexColor

[Snippet] UnitVertexColor

Have you ever stumbled upon a situation in which you want to write a spell that changes the vertex color of a unit? Well, there's only one problem with that: You can't get the initial RGBA vertex color of the unit so that you could set it back at the end of the spell.

This is the solution:

JASS:
/*****************************************
*
*   UnitVertexColor
*   v2.0.1.0
*   By Magtheridon96
*
*   - Caches unit vertex color data.
*
*   Requires:
*   ---------
*
*       - UnitIndexer by Nestharus
*           - hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
*
*   API:
*   ----
*
*       - struct UnitVertexColor extends array
*
*           - readonly real red
*           - readonly real green
*           - readonly real blue
*           - readonly real alpha
*               - Vertex color data retrieval.
*
*           - static method operator [] takes unit whichUnit returns thistype
*               - Returns an instance of this struct given a unit.
*
*****************************************/
library UnitVertexColor requires UnitIndexer
    
    private module Init
        private static method onInit takes nothing returns nothing
            call RegisterUnitIndexEvent(Condition(function thistype.index), UnitIndexer.INDEX)
        endmethod
    endmodule
    
    struct UnitVertexColor extends array
        
        readonly real red
        readonly real green
        readonly real blue
        readonly real alpha
        
        static method operator [] takes unit u returns thistype
            return GetUnitUserData(u)
        endmethod
        
        private static method update takes unit u, real r, real g, real b, real a returns nothing
            local thistype this = GetUnitUserData(u)
            set this.red = r
            set this.green = g
            set this.blue = b
            set this.alpha = a
        endmethod
        
        private static method index takes nothing returns boolean
            set thistype(GetIndexedUnitId()).red = 255
            set thistype(GetIndexedUnitId()).green = 255
            set thistype(GetIndexedUnitId()).blue = 255
            set thistype(GetIndexedUnitId()).alpha = 255
            return false
        endmethod
        
        implement Init
    endstruct
    
    hook SetUnitVertexColor UnitVertexColor.update
    
endlibrary

Demo Code:

JASS:
struct DemoCode extends array
    
    private static method onInit takes nothing returns nothing
        local unit u = CreateUnit(Player(0), 'hpea', 0, 0, 0)
        local UnitVertexColor uvc = UnitVertexColor[u]
        
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Red   = " + R2S(uvc.red))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Green = " + R2S(uvc.green))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Blue  = " + R2S(uvc.blue))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Alpha = " + R2S(uvc.alpha))
        
        call SetUnitVertexColor(u, 2, 2, 2, 2)
        
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Red   = " + R2S(uvc.red))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Green = " + R2S(uvc.green))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Blue  = " + R2S(uvc.blue))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Alpha = " + R2S(uvc.alpha))
        
        set u = null
    endmethod
    
endstruct

It may bug when you have Object-editor preset vertex colors.

Feel free to comment..
 

Attachments

  • UnitVertexColor Testmap.w3x
    25.2 KB · Views: 117
Last edited:
The color returns zero...this is v1.0...
  • Untitled Trigger 002
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
    • Actions
      • Custom script: call BJDebugMsg(R2S(GetUnitVertexColorRed(GetTriggerUnit())))
      • Custom script: call BJDebugMsg(R2S(GetUnitVertexColorGreen(GetTriggerUnit())))
      • Custom script: call BJDebugMsg(R2S(GetUnitVertexColorBlue(GetTriggerUnit())))
      • Custom script: call BJDebugMsg(R2S(GetUnitVertexColorAlpha(GetTriggerUnit())))
 
1 Do you have Unit Indexer correctly implemented? :eek:
2 With the undefend ability and all? :eek:
3 Updated this to v2.0.0.0
- New struct API

1) Yes ofc, it's a requirement...
2) Its just a test...
3) I'll try...

EDIT:
JASS:
local unit u = CreateUnit(Player(0), 'hkni', 0, 0, 0) //this works
//local unit u = GetTriggerUnit() //this doesnt, it always returns 0
local UnitVertexColor ver = UnitVertexColor[u]
call BJDebugMsg(R2S(ver.red))
call BJDebugMsg(R2S(ver.blue))
call BJDebugMsg(R2S(ver.green))
call BJDebugMsg(R2S(ver.alpha))
 
Well, that's because there is no triggering unit in the Demo code :p
Yes there is, the full code is...
JASS:
function Trig_Untitled_Trigger_002_Actions takes nothing returns nothing
    //local unit u = CreateUnit(Player(0), 'hpea', 0, 0, 0) //this works
    local unit u = GetTriggerUnit() //this doesnt, it always returns 0
    local UnitVertexColor ver = UnitVertexColor[u]
    call BJDebugMsg("red ="+R2S(ver.red))
    call BJDebugMsg("blue ="+R2S(ver.blue))
    call BJDebugMsg("green ="+R2S(ver.green))
    call BJDebugMsg("alpha ="+R2S(ver.alpha))
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_002 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_002 = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Untitled_Trigger_002, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction( gg_trg_Untitled_Trigger_002, function Trig_Untitled_Trigger_002_Actions )
endfunction

See also the image...
 

Attachments

  • vert.jpg
    vert.jpg
    36.9 KB · Views: 158
I found the bug.
I need Nestharus to update UnitIndexer to get rid of a nasty problem with the events >_>

edit
Updated to a more stable version for the current setup.
This isn't a final update.
I'm still working on the new features.

edit
Meh, I guess I should've read his documentation:

JASS:
*       static constant Event UnitIndexer.INDEX
*       static constant Event UnitIndexer.DEINDEX
*           -   @Don't register functions and triggers directly to the events. Register them via
*           -   RegisterUnitIndexEvent and TriggerRegisterUnitIndexEvent.
 
Top