- Joined
- Dec 12, 2008
- Messages
- 7,385
[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:
Demo Code:
It may bug when you have Object-editor preset vertex colors.
Feel free to comment..
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
Last edited: