Snippet String2PlayerColoredString

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, so can this maybe get reviewed ? :eek:

Or, otherwise, maybe someone can come up with some more features ? ;)
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
What about a useless feature like [lJASS]GetPlayerByColor(PLAYER_COLOR_BLUE)[/lJASS]

Returns Player with the color blue in the index, multiple if more than one, but returns null if none have it.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Why not just simply use Blue or something...
instead of PLAYER_COLOR_CLUE ? :eek:
 

Azlier

Old World Ghost
Reaction score
461
Add a more efficient GetPlayerColor that runs on pure magic :D.

(Hint: It's 1 function that returns an array value.)
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
That defeats the purpose then, as I said. It's most likely a useless feature.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
I'd personally like it, since you can dynamically track who has what colour. In the event they started with colours not their own, in maps that allow thing.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Well, I'd like it, but not sure about others. I'd ask others if it's viable first or not.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, then :D

Well, is there eother who might want this feature in the system ? :eek:

What about a useless feature like GetPlayerByColor(PLAYER_COLOR_BLUE)

Returns Player with the color blue in the index, multiple if more than one, but returns null if none have it.

:D:D

And also, how would I go about checking if the inputted string is, for example blue (That means that you should be able to write BlUe, or BLUe or BLue :D) so it can be used ? :eek:
 

quraji

zap
Reaction score
144
And also, how would I go about checking if the inputted string is, for example blue (That means that you should be able to write BlUe, or BLUe or BLue :D) so it can be used ? :eek:

Well, you'd want to store the string in an array parallel to the colors array. Make the strings lowercase like "blue", "red", etc. Then, convert whatever the user puts in to lowercase (that will allow it to match when they enter "Blue" or "BLUE", and so on). You could also trim spaces and unneeded character. I think I submitted a function to do that along with some other string functions...I don't remember.. :p
 

Komaqtion

You can change this now in User CP.
Reaction score
469
EDIT: Ok, I've now gotten it to work :D
But, this test trigger here only shows 3 "Komaqtion"s (That's my offline and online name :p) when it's supposed to show 4 :(
And I've figured out that it's the last action which isn't working (Which I thought even before testing XD).
Then problem is why it isn't working...

Here's the test trigger:
Trigger:
  • Test Player By Color
    • Events
      • Player - Player 1 (Red) types a chat message containing player as An exact match
    • Conditions
    • Actions
      • Custom script: call BJDebugMsg( GetPlayerName( GetPlayerByColor("red")))
      • Custom script: call BJDebugMsg( GetPlayerName( GetPlayerByColor("rEd")))
      • Custom script: call BJDebugMsg( GetPlayerName( GetPlayerByColor("REd")))
      • Custom script: call BJDebugMsg( GetPlayerName( GetPlayerByColor(".. R.,,e D")))


And here's the whole script:
JASS:
library PlayerColors initializer Init


///////////////////////////////////////////////////////////////////////////////////////////////////
//                                 *//PlayerColoredString\\*                                     //
//                                 *//Made by , Komaqtion\\*                                      //
//                                                                                               //
//                                                                                               //
//                                        Purpose:                                               //
//                                                                                               //
//             ¤ This snippet is supposed to help people to, with ease, convert                  //
//               a string to use a desired player's own color!                                   //
//                                                                                               //
//                                         Usage:                                                //
//                                                                                               //
//             ¤ To use this snippet, simply type "call PlayerColoredString(playerid, string)"   //
//               where "string" is the string you want to colorize, and "playerid"               //
//               is the player number of the player, whose color you wan to use.                 //
//               Note: This uses JASS' player number range, meaning that                         //
//               Player 1(Red)'s number is 0, and Player 2(Blue)'s number is 1,                  //
//               and so on...                                                                    //
//               You can also get only the players color-string for own usage...                 //
//               This is accomplished by using the function "GetPlayerColorString"               //
//               which takes the player to get the string, or you can also use                   //
//               the GetPlayerColorStringById function, which takes the player's                 //
//               Id, or number instead <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                                        //
//               You can also change the color of texttags using this snippet...                 //
//               The function is called &quot;PlayerColoredTextTag&quot; and takes integer alpha           //
//               which is transparency, player p, the playercolor you which to use, and          //
//               texttag tag which is the texttag you which to change the color of !             //
//                                                                                               //
//                                                                                               //
//                                    Requirements:                                              //
//                                                                                               //
//             ¤ This snippet&#039;s only requirement is vJASS compilement, which is                  //
//               easiest achieved by downloading JASS Newgen Pack, at                            //
//               <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a>                          //
//               You&#039;ll also have to update JASS Helper to the latest version...                 //
//                                                                                               //
//                                                                                               //
//                                        Credits:                                               //
//                                                                                               //
//             ¤ Azlier, for helpong me out alot with several bugfixes and tweaking !            //
//               Jesus4Lyf, for also helping me out alot ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                    //
//                                                                                               //
//               And credits, if you use this that is, is not needed to give me                  //
//               though it&#039;s always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                   //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////


    globals
        private integer array Red
        private integer array Green
        private integer array Blue
        private integer array RedString
        private integer array GreenString
        private integer array BlueString
        private string array PlayerColor
        private string array PlayerColorString
        private string array PlayerColorWord
        private string array PlayerColorWordString
        
        private constant string EndString = &quot;|r&quot;
    endglobals
    
    function PlayerColoredString takes integer playerId, string s returns string
        return PlayerColor[playerId] + s + EndString
    endfunction
    
    function PlayerColoredTextTag takes integer alpha, player p, texttag tag returns nothing
        call SetTextTagColor( tag, Red[GetPlayerId( p )], Green[GetPlayerId( p )], Blue[GetPlayerId( p )], alpha )
    endfunction
    
    function PlayerColoredTextTagById takes integer alpha, integer playerId, texttag tag returns nothing
        call SetTextTagColor( tag, Red[playerId], Green[playerId], Blue[playerId], alpha )
    endfunction
    
    function GetPlayerColorString takes player p returns string
        return PlayerColor[GetPlayerId( p )]
    endfunction
    
    function GetPlayerColorStringById takes integer playerId returns string
        return PlayerColor[playerId]
    endfunction
    
    function CompressString takes string compstring returns string
        local integer w = StringLength( compstring )
        local integer i = 0
        local string s = &quot;&quot;
        
        loop
        
                if not( SubString( compstring, i, i + 1 ) == &quot; &quot; or SubString( compstring, i, i + 1 ) == &quot;.&quot; or SubString( compstring, i, i + 1 ) == &quot;,&quot; ) then
                    set s = s + SubString( compstring, i, i + 1 )
                endif
                
            set i = i + 1
            exitwhen i &gt; w
            
        endloop
        
        return s
    endfunction
    
    function GetPlayerByColor takes string color returns player
        local string s = StringCase( color, false )
        local integer i = 0
        
        call CompressString( s )
        call StringCase( s, false )
        
        loop
        
            if s == PlayerColorWordString<i> then
                return Player( i )
            endif
            
        endloop
        
        return Player( -1 )
    endfunction
    
    private function OnColorChange takes player whichplayer, playercolor color returns nothing
        local integer id = GetPlayerId( whichplayer )
        local integer pc = GetHandleId( color )

        set PlayerColor[id] = PlayerColorString[pc]
        set Red[id] = RedString[pc]
        set Green[id] = GreenString[pc]
        set Blue[id] = BlueString[pc]
        set PlayerColorWord[id] = PlayerColorWordString[pc]
    endfunction
    
    hook SetPlayerColor OnColorChange
    
    private function Init takes nothing returns nothing
        local integer i = 12
        local integer i2
    
        set PlayerColorString[0] = &quot;|c00ff0202&quot;
        set RedString[0] = 0xff
        set GreenString[0] = 0x02
        set BlueString[0] = 0x02
        set PlayerColorWordString[0] = &quot;red&quot;
        set PlayerColorString[1] = &quot;|c000041ff&quot;
        set RedString[1] = 0x00
        set GreenString[1] = 0x41
        set BlueString[1] = 0xff
        set PlayerColorWordString[1] = &quot;blue&quot;
        set PlayerColorString[2] = &quot;|c001be5b8&quot;
        set RedString[2] = 0x1b
        set GreenString[2] = 0xe5
        set BlueString[2] = 0xb8
        set PlayerColorWordString[2] = &quot;teal&quot;
        set PlayerColorString[3] = &quot;|c00530080&quot;
        set RedString[3] = 0x53
        set GreenString[3] = 0x00
        set BlueString[3] = 0x80
        set PlayerColorWordString[3] = &quot;purple&quot;
        set PlayerColorString[4] = &quot;|c00fffc00&quot;
        set RedString[4] = 0xff
        set GreenString[4] = 0xfc
        set BlueString[4] = 0x00
        set PlayerColorWordString[4] = &quot;yellow&quot;
        set PlayerColorString[5] = &quot;|c00fe890d&quot;
        set RedString[5] = 0xfe
        set GreenString[5] = 0x89
        set BlueString[5] = 0x0d
        set PlayerColorWordString[5] = &quot;orange&quot;
        set PlayerColorString[6] = &quot;|c001fbf00&quot;
        set RedString[6] = 0x1f
        set GreenString[6] = 0xbf
        set BlueString[6] = 0x00
        set PlayerColorWordString[6] = &quot;green&quot;
        set PlayerColorString[7] = &quot;|c00e45aaf&quot;
        set RedString[7] = 0xe4
        set GreenString[7] = 0x5a
        set BlueString[7] = 0xaf
        set PlayerColorWordString[7] = &quot;pink&quot;
        set PlayerColorString[8] = &quot;|c00949596&quot;
        set RedString[8] = 0x94
        set GreenString[8] = 0x95
        set BlueString[8] = 0x96
        set PlayerColorWordString[8] = &quot;grey&quot;
        set PlayerColorString[9] = &quot;|c007dbef1&quot;
        set RedString[9] = 0x7d
        set GreenString[9] = 0xbe
        set BlueString[9] = 0xf1
        set PlayerColorWordString[9] = &quot;lightblue&quot;
        set PlayerColorString[10] = &quot;|c000f6145&quot;
        set RedString[10] = 0x0f
        set GreenString[10] = 0x61
        set BlueString[10] = 0x45
        set PlayerColorWordString[10] = &quot;darkgreen&quot;
        set PlayerColorString[11] = &quot;|c004d2903&quot;
        set RedString[11] = 0x4d
        set GreenString[11] = 0x29
        set BlueString[11] = 0x03
        set PlayerColorWordString[11] = &quot;brown&quot;

        set PlayerColorString[12] = &quot;|c001f1f1f&quot;
        set RedString[12] = 0x1f
        set GreenString[12] = 0x1f
        set BlueString[12] = 0x1f
        set PlayerColorWordString[12] = &quot;darkgrey&quot;
        
        set PlayerColorWordString[13] = &quot;gray&quot;
        set PlayerColorWordString[14] = &quot;aqua&quot;
        set PlayerColorWordString[15] = &quot;cyan&quot;
    
        loop
            exitwhen i &lt; 0
            set i2 = GetHandleId( GetPlayerColor( Player( i ) ) )
            set PlayerColor<i> = PlayerColorString[i2]
            set Red<i> = RedString[i2]
            set Green<i> = GreenString[i2]
            set Blue<i> = BlueString[i2]
            set PlayerColorWord<i> = PlayerColorWordString<i>
            set i = i - 1
        endloop
        
    endfunction
    
endlibrary</i></i></i></i></i></i></i>
 

quraji

zap
Reaction score
144
Well, a couple things:
JASS:
    function GetPlayerByColor takes string color returns player
        local string s = StringCase( color, false )
        local integer i = 0
        
        call CompressString( s ) // set s = CompressString(s)
        call StringCase( s, false ) // need this?
        
        loop 
        
            if s == PlayerColorWordString<i> then
                return Player( i )
            endif
            
            // i = i + 1
            // exitwhen (i==12) //12 or whatever
        endloop
        
        return Player( -1 ) // I heard Player(x) where x isn&#039;t a valid number will crash the thread..
    endfunction
</i>


Edit: Turns out Player(-1) won't crash the thread. It'll crash the game instead. Anyways, the main problem is that you didn't set s to the compressed string and the possible infinite loop. Should work after those fixes.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Well, I trie to compress "asf" string, but it didn't crash the game for me ...

But anyways... Will test that then :D
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, then :D
Thanks !!!!
Works now :D

Update ;)
JASS:
library PlayerColors initializer Init


///////////////////////////////////////////////////////////////////////////////////////////////////
//                                 *//PlayerColoredString\\*                                     //
//                                 *//Made by , Komaqtion\\*                                      //
//                                                                                               //
//                                                                                               //
//                                        Purpose:                                               //
//                                                                                               //
//             ¤ This snippet is supposed to help people to, with ease, convert                  //
//               a string to use a desired player&#039;s own color!                                   //
//                                                                                               //
//                                         Usage:                                                //
//                                                                                               //
//             ¤ To use this snippet, simply type &quot;call PlayerColoredString(playerid, string)&quot;   //
//               where &quot;string&quot; is the string you want to colorize, and &quot;playerid&quot;               //
//               is the player number of the player, whose color you wan to use.                 //
//               Note: This uses JASS&#039; player number range, meaning that                         //
//               Player 1(Red)&#039;s number is 0, and Player 2(Blue)&#039;s number is 1,                  //
//               and so on...                                                                    //
//               You can also get only the players color-string for own usage...                 //
//               This is accomplished by using the function &quot;GetPlayerColorString&quot;               //
//               which takes the player to get the string, or you can also use                   //
//               the GetPlayerColorStringById function, which takes the player&#039;s                 //
//               Id, or number instead <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                                        //
//               You can also change the color of texttags using this snippet...                 //
//               The function is called &quot;PlayerColoredTextTag&quot; and takes integer alpha           //
//               which is transparency, player p, the playercolor you which to use, and          //
//               texttag tag which is the texttag you which to change the color of !             //
//                                                                                               //
//                                                                                               //
//                                    Requirements:                                              //
//                                                                                               //
//             ¤ This snippet&#039;s only requirement is vJASS compilement, which is                  //
//               easiest achieved by downloading JASS Newgen Pack, at                            //
//               <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a>                          //
//               You&#039;ll also have to update JASS Helper to the latest version...                 //
//                                                                                               //
//                                                                                               //
//                                        Credits:                                               //
//                                                                                               //
//             ¤ Azlier, for helpong me out alot with several bugfixes and tweaking !            //
//               Jesus4Lyf, for also helping me out alot ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                    //
//                                                                                               //
//               And credits, if you use this that is, is not needed to give me                  //
//               though it&#039;s always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                   //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////


    globals
        private integer array Red
        private integer array Green
        private integer array Blue
        private integer array RedString
        private integer array GreenString
        private integer array BlueString
        private string array PlayerColor
        private string array PlayerColorString
        private string array PlayerColorWord
        private string array PlayerColorWordString
        
        private constant string EndString = &quot;|r&quot;
    endglobals
    
    function PlayerColoredString takes integer playerId, string s returns string
        return PlayerColor[playerId] + s + EndString
    endfunction
    
    function PlayerColoredTextTag takes integer alpha, player p, texttag tag returns nothing
        call SetTextTagColor( tag, Red[GetPlayerId( p )], Green[GetPlayerId( p )], Blue[GetPlayerId( p )], alpha )
    endfunction
    
    function PlayerColoredTextTagById takes integer alpha, integer playerId, texttag tag returns nothing
        call SetTextTagColor( tag, Red[playerId], Green[playerId], Blue[playerId], alpha )
    endfunction
    
    function GetPlayerColorString takes player p returns string
        return PlayerColor[GetPlayerId( p )]
    endfunction
    
    function GetPlayerColorStringById takes integer playerId returns string
        return PlayerColor[playerId]
    endfunction
    
    function CompressString takes string compstring returns string
        local integer w = StringLength( compstring )
        local integer i = 0
        local string s = &quot;&quot;
        
        loop
        
                if not( SubString( compstring, i, i + 1 ) == &quot; &quot; or SubString( compstring, i, i + 1 ) == &quot;.&quot; or SubString( compstring, i, i + 1 ) == &quot;,&quot; ) then
                    set s = s + SubString( compstring, i, i + 1 )
                endif
                
            set i = i + 1
            exitwhen i &gt; w
            
        endloop
        
        return s
    endfunction
    
    function GetPlayerByColor takes string color returns player
        local string s = StringCase( color, false )
        local integer i = 0
        
        set s = CompressString( s )
        
        loop 
        
            if s == PlayerColorWordString<i> then
                return Player( i )
            endif
            
            set i = i + 1
            exitwhen ( i &gt;= 13 )
        endloop
        
        if s == PlayerColorWordString[13] then
            return Player( 8 )
        elseif s == PlayerColorWordString[14] or s == PlayerColorWordString[15] then
            return Player( 2 )
        endif
        
        return Player( -1 )
    endfunction
    
    private function OnColorChange takes player whichplayer, playercolor color returns nothing
        local integer id = GetPlayerId( whichplayer )
        local integer pc = GetHandleId( color )

        set PlayerColor[id] = PlayerColorString[pc]
        set Red[id] = RedString[pc]
        set Green[id] = GreenString[pc]
        set Blue[id] = BlueString[pc]
        set PlayerColorWord[id] = PlayerColorWordString[pc]
    endfunction
    
    hook SetPlayerColor OnColorChange
    
    private function Init takes nothing returns nothing
        local integer i = 12
        local integer i2
    
        set PlayerColorString[0] = &quot;|c00ff0202&quot;
        set RedString[0] = 0xff
        set GreenString[0] = 0x02
        set BlueString[0] = 0x02
        set PlayerColorWordString[0] = &quot;red&quot;
        set PlayerColorString[1] = &quot;|c000041ff&quot;
        set RedString[1] = 0x00
        set GreenString[1] = 0x41
        set BlueString[1] = 0xff
        set PlayerColorWordString[1] = &quot;blue&quot;
        set PlayerColorString[2] = &quot;|c001be5b8&quot;
        set RedString[2] = 0x1b
        set GreenString[2] = 0xe5
        set BlueString[2] = 0xb8
        set PlayerColorWordString[2] = &quot;teal&quot;
        set PlayerColorString[3] = &quot;|c00530080&quot;
        set RedString[3] = 0x53
        set GreenString[3] = 0x00
        set BlueString[3] = 0x80
        set PlayerColorWordString[3] = &quot;purple&quot;
        set PlayerColorString[4] = &quot;|c00fffc00&quot;
        set RedString[4] = 0xff
        set GreenString[4] = 0xfc
        set BlueString[4] = 0x00
        set PlayerColorWordString[4] = &quot;yellow&quot;
        set PlayerColorString[5] = &quot;|c00fe890d&quot;
        set RedString[5] = 0xfe
        set GreenString[5] = 0x89
        set BlueString[5] = 0x0d
        set PlayerColorWordString[5] = &quot;orange&quot;
        set PlayerColorString[6] = &quot;|c001fbf00&quot;
        set RedString[6] = 0x1f
        set GreenString[6] = 0xbf
        set BlueString[6] = 0x00
        set PlayerColorWordString[6] = &quot;green&quot;
        set PlayerColorString[7] = &quot;|c00e45aaf&quot;
        set RedString[7] = 0xe4
        set GreenString[7] = 0x5a
        set BlueString[7] = 0xaf
        set PlayerColorWordString[7] = &quot;pink&quot;
        set PlayerColorString[8] = &quot;|c00949596&quot;
        set RedString[8] = 0x94
        set GreenString[8] = 0x95
        set BlueString[8] = 0x96
        set PlayerColorWordString[8] = &quot;grey&quot;
        set PlayerColorString[9] = &quot;|c007dbef1&quot;
        set RedString[9] = 0x7d
        set GreenString[9] = 0xbe
        set BlueString[9] = 0xf1
        set PlayerColorWordString[9] = &quot;lightblue&quot;
        set PlayerColorString[10] = &quot;|c000f6145&quot;
        set RedString[10] = 0x0f
        set GreenString[10] = 0x61
        set BlueString[10] = 0x45
        set PlayerColorWordString[10] = &quot;darkgreen&quot;
        set PlayerColorString[11] = &quot;|c004d2903&quot;
        set RedString[11] = 0x4d
        set GreenString[11] = 0x29
        set BlueString[11] = 0x03
        set PlayerColorWordString[11] = &quot;brown&quot;

        set PlayerColorString[12] = &quot;|c001f1f1f&quot;
        set RedString[12] = 0x1f
        set GreenString[12] = 0x1f
        set BlueString[12] = 0x1f
        set PlayerColorWordString[12] = &quot;darkgrey&quot;
        
        set PlayerColorWordString[13] = &quot;gray&quot;
        set PlayerColorWordString[14] = &quot;aqua&quot;
        set PlayerColorWordString[15] = &quot;cyan&quot;
    
        loop
            exitwhen i &lt; 0
            set i2 = GetHandleId( GetPlayerColor( Player( i ) ) )
            set PlayerColor<i> = PlayerColorString[i2]
            set Red<i> = RedString[i2]
            set Green<i> = GreenString[i2]
            set Blue<i> = BlueString[i2]
            set PlayerColorWord<i> = PlayerColorWordString<i>
            set i = i - 1
        endloop
        
    endfunction
    
endlibrary</i></i></i></i></i></i></i>


Btw, is it good that I have both aqua and cyan as teal ?
And gray as grey too :D :eek:

EDIT: Well, ok... [ljass]return Player( -1 )[/ljass] did crash the game XD
So, what should I return then... :S
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, so it looks like this now, and it all seems to work fine :D

JASS:
library PlayerColors initializer Init


///////////////////////////////////////////////////////////////////////////////////////////////////
//                                 *//PlayerColoredString\\*                                     //
//                                 *//Made by , Komaqtion\\*                                      //
//                                                                                               //
//                                                                                               //
//                                        Purpose:                                               //
//                                                                                               //
//             ¤ This snippet is supposed to help people to, with ease, convert                  //
//               a string to use a desired player&#039;s own color!                                   //
//                                                                                               //
//                                         Usage:                                                //
//                                                                                               //
//             ¤ To use this snippet, simply type &quot;call PlayerColoredString(playerid, string)&quot;   //
//               where &quot;string&quot; is the string you want to colorize, and &quot;playerid&quot;               //
//               is the player number of the player, whose color you wan to use.                 //
//               Note: This uses JASS&#039; player number range, meaning that                         //
//               Player 1(Red)&#039;s number is 0, and Player 2(Blue)&#039;s number is 1,                  //
//               and so on...                                                                    //
//               You can also get only the players color-string for own usage...                 //
//               This is accomplished by using the function &quot;GetPlayerColorString&quot;               //
//               which takes the player to get the string, or you can also use                   //
//               the GetPlayerColorStringById function, which takes the player&#039;s                 //
//               Id, or number instead <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                                        //
//               You can also change the color of texttags using this snippet...                 //
//               The function is called &quot;PlayerColoredTextTag&quot; and takes integer alpha           //
//               which is transparency, player p, the playercolor you which to use, and          //
//               texttag tag which is the texttag you which to change the color of !             //
//               You can also use the function &quot;GetPlayerByColor&quot; which takes a string (color)   //
//               and returns the player which has that specific color. You can even type         //
//               &quot;ReD&quot; or &quot;REd&quot; or even &quot;..,,  R, e.., d&quot; and it&#039;ll return Player(0) or          //
//               player red in all three cases. And, you can also get the player&#039;s color         //
//               this way.                                                                       //
//                                                                                               //
//                                                                                               //
//                                    Requirements:                                              //
//                                                                                               //
//             ¤ This snippet&#039;s only requirement is vJASS compilement, which is                  //
//               easiest achieved by downloading JASS Newgen Pack, at                            //
//               <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a>                          //
//               You&#039;ll also have to update JASS Helper to the latest version...                 //
//                                                                                               //
//                                                                                               //
//                                        Credits:                                               //
//                                                                                               //
//             ¤ Azlier, for helpong me out alot with several bugfixes and tweaking !            //
//               Jesus4Lyf, for also helping me out alot ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                    //
//               Quraji, for helping me add the &quot;GetPlayerByColor&quot; function ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                 //
//                                                                                               //
//               And credits, if you use this that is, is not needed to give me                  //
//               though it&#039;s always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                   //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////


    globals
        private integer array Red
        private integer array Green
        private integer array Blue
        private integer array RedString
        private integer array GreenString
        private integer array BlueString
        private string array PlayerColor
        private string array PlayerColorString
        private string array PlayerColorWord
        private string array PlayerColorWordString
        
        private constant string EndString = &quot;|r&quot;
    endglobals
    
    function PlayerColoredString takes integer playerId, string s returns string
        return PlayerColor[playerId] + s + EndString
    endfunction
    
    function PlayerColoredTextTag takes integer alpha, player p, texttag tag returns nothing
        call SetTextTagColor( tag, Red[GetPlayerId( p )], Green[GetPlayerId( p )], Blue[GetPlayerId( p )], alpha )
    endfunction
    
    function PlayerColoredTextTagById takes integer alpha, integer playerId, texttag tag returns nothing
        call SetTextTagColor( tag, Red[playerId], Green[playerId], Blue[playerId], alpha )
    endfunction
    
    function GetPlayerColorString takes player p returns string
        return PlayerColor[GetPlayerId( p )]
    endfunction
    
    function GetPlayerColorStringById takes integer playerId returns string
        return PlayerColor[playerId]
    endfunction
    
    function CompressString takes string compstring returns string
        local integer w = StringLength( compstring )
        local integer i = 0
        local string s = &quot;&quot;
        
        loop
        
                if not( SubString( compstring, i, i + 1 ) == &quot; &quot; or SubString( compstring, i, i + 1 ) == &quot;.&quot; or SubString( compstring, i, i + 1 ) == &quot;,&quot; ) then
                    set s = s + SubString( compstring, i, i + 1 )
                endif
                
            set i = i + 1
            
        exitwhen i &gt; w
        endloop
        
        return s
    endfunction
    
    function GetPlayerByColor takes string color returns player
        local string s = StringCase( color, false )
        local integer i = 0
        
        set s = CompressString( s )
        
        loop 
        
            if s == PlayerColorWordString<i> then
                return Player( i )
            elseif s == PlayerColorWordString[13] then
                return Player( 8 )
            elseif s == PlayerColorWordString[14] or s == PlayerColorWordString[15] then
                return Player( 2 )
            endif
            
            set i = i + 1
            
        exitwhen ( i &gt;= 13 )
        endloop
        
        return null
    endfunction
    
    function GetPlayerColorByColor takes string color returns string
        local string s = StringCase( color, false )
        local integer i = 0
        
        set s = CompressString( s )
        
        loop 
        
            if s == PlayerColorWordString<i> then
                return PlayerColor<i>
            endif
            
            set i = i + 1
            exitwhen ( i &gt;= 13 )
        endloop
        
        if s == PlayerColorWordString[13] then
            return PlayerColor[8]
        elseif s == PlayerColorWordString[14] or s == PlayerColorWordString[15] then
            return PlayerColor[2]
        endif
        
        return &quot;&quot;
    endfunction
    
    private function OnColorChange takes player whichplayer, playercolor color returns nothing
        local integer id = GetPlayerId( whichplayer )
        local integer pc = GetHandleId( color )

        set PlayerColor[id] = PlayerColorString[pc]
        set Red[id] = RedString[pc]
        set Green[id] = GreenString[pc]
        set Blue[id] = BlueString[pc]
        set PlayerColorWord[id] = PlayerColorWordString[pc]
    endfunction
    
    hook SetPlayerColor OnColorChange
    
    private function Init takes nothing returns nothing
        local integer i = 12
        local integer i2
    
        set PlayerColorString[0] = &quot;|c00ff0202&quot;
        set RedString[0] = 0xff
        set GreenString[0] = 0x02
        set BlueString[0] = 0x02
        set PlayerColorWordString[0] = &quot;red&quot;
        set PlayerColorString[1] = &quot;|c000041ff&quot;
        set RedString[1] = 0x00
        set GreenString[1] = 0x41
        set BlueString[1] = 0xff
        set PlayerColorWordString[1] = &quot;blue&quot;
        set PlayerColorString[2] = &quot;|c001be5b8&quot;
        set RedString[2] = 0x1b
        set GreenString[2] = 0xe5
        set BlueString[2] = 0xb8
        set PlayerColorWordString[2] = &quot;teal&quot;
        set PlayerColorString[3] = &quot;|c00530080&quot;
        set RedString[3] = 0x53
        set GreenString[3] = 0x00
        set BlueString[3] = 0x80
        set PlayerColorWordString[3] = &quot;purple&quot;
        set PlayerColorString[4] = &quot;|c00fffc00&quot;
        set RedString[4] = 0xff
        set GreenString[4] = 0xfc
        set BlueString[4] = 0x00
        set PlayerColorWordString[4] = &quot;yellow&quot;
        set PlayerColorString[5] = &quot;|c00fe890d&quot;
        set RedString[5] = 0xfe
        set GreenString[5] = 0x89
        set BlueString[5] = 0x0d
        set PlayerColorWordString[5] = &quot;orange&quot;
        set PlayerColorString[6] = &quot;|c001fbf00&quot;
        set RedString[6] = 0x1f
        set GreenString[6] = 0xbf
        set BlueString[6] = 0x00
        set PlayerColorWordString[6] = &quot;green&quot;
        set PlayerColorString[7] = &quot;|c00e45aaf&quot;
        set RedString[7] = 0xe4
        set GreenString[7] = 0x5a
        set BlueString[7] = 0xaf
        set PlayerColorWordString[7] = &quot;pink&quot;
        set PlayerColorString[8] = &quot;|c00949596&quot;
        set RedString[8] = 0x94
        set GreenString[8] = 0x95
        set BlueString[8] = 0x96
        set PlayerColorWordString[8] = &quot;grey&quot;
        set PlayerColorString[9] = &quot;|c007dbef1&quot;
        set RedString[9] = 0x7d
        set GreenString[9] = 0xbe
        set BlueString[9] = 0xf1
        set PlayerColorWordString[9] = &quot;lightblue&quot;
        set PlayerColorString[10] = &quot;|c000f6145&quot;
        set RedString[10] = 0x0f
        set GreenString[10] = 0x61
        set BlueString[10] = 0x45
        set PlayerColorWordString[10] = &quot;darkgreen&quot;
        set PlayerColorString[11] = &quot;|c004d2903&quot;
        set RedString[11] = 0x4d
        set GreenString[11] = 0x29
        set BlueString[11] = 0x03
        set PlayerColorWordString[11] = &quot;brown&quot;

        set PlayerColorString[12] = &quot;|c001f1f1f&quot;
        set RedString[12] = 0x1f
        set GreenString[12] = 0x1f
        set BlueString[12] = 0x1f
        set PlayerColorWordString[12] = &quot;darkgrey&quot;
        
        set PlayerColorWordString[13] = &quot;gray&quot;
        set PlayerColorWordString[14] = &quot;aqua&quot;
        set PlayerColorWordString[15] = &quot;cyan&quot;
    
        loop
            exitwhen i &lt; 0
            set i2 = GetHandleId( GetPlayerColor( Player( i ) ) )
            set PlayerColor<i> = PlayerColorString[i2]
            set Red<i> = RedString[i2]
            set Green<i> = GreenString[i2]
            set Blue<i> = BlueString[i2]
            set PlayerColorWord<i> = PlayerColorWordString<i>
            set i = i - 1
        endloop
        
    endfunction
    
endlibrary</i></i></i></i></i></i></i></i></i>


As you can see, instead of returning Player( -1 ) in the "GetPlayerByColor" function, I return "null"...
Will that work better ? :S

Just one thing... I have a Text-Tag which gets "player-colored" using the "GetPlayerByColor" function, and (Just to test what happens when I type an invalid color) I use both "aqua" color, and "white" color. (As "white" isn't a valid color here...)
But, then it turns red instead :(

Any ideas to why that's happening ? :S

Here's the test trigger ;)

Trigger:
  • Init
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set TempPoint = (Center of Region 1 &lt;gen&gt;)
      • Floating Text - Create floating text that reads Region 1! at TempPoint with Z offset 0.00, using font size 15.00, color (100.00%, 100.00%, 100.00%), and 0.00% transparency
      • Custom script: call PlayerColoredTextTag( 100, GetPlayerByColor(&quot;white&quot;), bj_lastCreatedTextTag)
      • Custom script: call RemoveLocation(udg_TempPoint)
      • Set TempPoint = (Center of Region 2 &lt;gen&gt;)
      • Floating Text - Create floating text that reads Region 2! at TempPoint with Z offset 0.00, using font size 15.00, color (100.00%, 100.00%, 100.00%), and 0.00% transparency
      • Custom script: call PlayerColoredTextTag( 100, GetPlayerByColor(&quot;aqua&quot;), bj_lastCreatedTextTag)
      • Custom script: call RemoveLocation(udg_TempPoint)
 

quraji

zap
Reaction score
144
>> As you can see, instead of returning Player( -1 ) in the "GetPlayerByColor" function, I return "null"...
Will that work better ? :S

That's what I would do. Just remember to remind the user to check against null returns in the documentation.

Just one thing... I have a Text-Tag which gets "player-colored" using the "GetPlayerByColor" function, and (Just to test what happens when I type an invalid color) I use both "aqua" color, and "white" color. (As "white" isn't a valid color here...)
But, then it turns red instead :(

Any ideas to why that's happening ? :S

Here's the test trigger ;)

Trigger:
  • Init
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set TempPoint = (Center of Region 1 &lt;gen&gt;)
      • Floating Text - Create floating text that reads Region 1! at TempPoint with Z offset 0.00, using font size 15.00, color (100.00%, 100.00%, 100.00%), and 0.00% transparency
      • Custom script: call PlayerColoredTextTag( 100, GetPlayerByColor(&quot;white&quot;), bj_lastCreatedTextTag)
      • Custom script: call RemoveLocation(udg_TempPoint)
      • Set TempPoint = (Center of Region 2 &lt;gen&gt;)
      • Floating Text - Create floating text that reads Region 2! at TempPoint with Z offset 0.00, using font size 15.00, color (100.00%, 100.00%, 100.00%), and 0.00% transparency
      • Custom script: call PlayerColoredTextTag( 100, GetPlayerByColor(&quot;aqua&quot;), bj_lastCreatedTextTag)
      • Custom script: call RemoveLocation(udg_TempPoint)

1. You call 'GetPlayerByColor("white")'. Since there is no white player, it returns null (instead of Player(-1), thank goodness!).

2. Inside the PlayerColoredTextTag function, you use GetPlayerId() to perform the array lookup. GetPlayerId(null) returns 0. So, instead of breaking, the function will perform as if the player passed was Player(0).

JASS:
scope test initializer init

function test takes player p returns nothing
    call BJDebugMsg(I2S(GetPlayerId(p))) // displays 0
endfunction

function init takes nothing returns nothing
    local player p = null // it&#039;s worth noting that if p is uninitialized (no &#039;= null&#039;), the thread will crash when calling test(p).
    call test(p)
endfunction

endscope


So you might want to check if the player == null inside the function. If it is, just exit. Maybe add a [ljass]debug[/ljass] warning.
 
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