Snippet PlayerColor()

Ryuu

I am back with Chocolate (:
Reaction score
64
This simple snippet colors your text into player colors.
Supports neutral players.

JASS:
function PlayerColor takes player whichPlayer, string whichString returns string
local string array c
local integer i = GetConvertedPlayerId(whichPlayer)
local string s = whichString
local integer Index = 0
set c[1] = "|CFF0303"
set c[2] = "|C0042FF"
set c[3] = "|CFF1CB619"
set c[4] = "|CFF540081"
set c[5] = "|CFFFFFF01"
set c[6] = "|CFFFE8A0E"
set c[7] = "|CFF20C000"
set c[8] = "|CFFE55BB0"
set c[9] = "|CFF959697"
set c[10] = "|CFF7EBFF1"
set c[11] = "|CFF106246"
set c[12] = "|CFF4E2A04"
set c[13] = "|CFF000000"
return c<i>+s+&quot;|r&quot;
endfunction</i>


This can be achieved by calling the function:

JASS:
call PlayerColor(player whichPlayer, string whichString)


For example, this

JASS:
call PlayerColor(&quot;hahahahaha!&quot;, Player(0))


will return:

hahahahaha!

Additional credits go to Darthfett for help on 'return values'.

"Do not condemn me; this is what I had made and what I plan to share. You have three choices: take it, leave it or take it and leave."
 

Vestras

Retired
Reaction score
248
Ever heard about loops and elseifs?
And your little example will show nothing else than compile errors.
 

Ryuu

I am back with Chocolate (:
Reaction score
64
I'm still a beginner :p
I'm still a beginner in Jass
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Why not store each string in an array, and just return the GetPlayerId(whichPlayer) indexed value?

At the very least, set a local integer to the Player's Id, and compare the Id to the numbers, instead of calling the Player(#) function until it reaches the correct player.

For example, this

JASS:
call PlayerColor(&quot;hahahahaha!&quot;, Player(0))


will display:

hahahahaha!

Well not EXACTLY. It returns that string, but that doesn't necessarily display it. It is useful for a DisplayTextToPlayer/Force or BJDebugMsg function call, though.
 

Romek

Super Moderator
Reaction score
963
This is very inefficient..
First off, you could use:
JASS:
local integer p = GetPlayerId(whichPlayer)

Then compare the integers. Less function calls.

What's with all those elses? Just use elseif.

Or a better alternative, as Darth said would be to use arrays.
JASS:
globals
  string array Colours
endglobals

function Init...
   set Colours[0] = &quot;|CFFFF0303&quot;
   // etc
endfunction

function GetColour ...
   return Colours[GetPlayerId(whichPlayer)] + whichString + &quot;|r&quot;
endfunction


That's about as good as this type of snippet can get, and it's been made like that before...
 

Ryuu

I am back with Chocolate (:
Reaction score
64
> Do you have to indent everything so far?

Well no.​

> Why not store each string in an array, and just return the GetPlayerId(whichPlayer) indexed value?

Good suggestion.​

> It returns that string, but that doesn't necessarily display it.

Right.​

> compare the integers.

That's what I did.​

> use elseif.

Why not use loops?​

> and it's been made like that before...

And it has been lost somewhere deep in this forum, only accessible by search :D

Really, my main purpose on making this snippet is for my own personal use but I find it saves a whole load of time so I shared with you guys. Never expecting myself to receive much correction, though.

Thanks for all the corrections.
I learnt something myself :D
 

Romek

Super Moderator
Reaction score
963
JASS:
local string s = whichString

Why? Just use whichString directly.

JASS:
local integer i = GetConvertedPlayerId(whichPlayer)

Should be:
JASS:
local integer i = GetPlayerId(whichPlayer)


And the strings start from 0. Saves a function call, and looks cleaner. (In Jass, players start from 0, not 1)

JASS:
loop
        exitwhen Index &gt; 16
        set Index = (Index + 1)
        if (i == Index) then
                return c[Index]+s+&quot;|r&quot;
        else
                return &quot;&quot;
        endif
endloop

Could be:
JASS:
return c<i>+s+&quot;|r&quot;</i>


No need for loops or "Index" at all.
And it would be impossible for "i" be too high or low if you added the grey "Neutral" colour.

On a related note, did you even test this new one? It wouldn't work for any player other than Player(0) because it returns "" immediately.
 

Romek

Super Moderator
Reaction score
963
Clearly you didn't test this before submitting it.
 

Ryuu

I am back with Chocolate (:
Reaction score
64
I couldn't test it due to computer lag.

EDIT:
Happy new year! :D
 

Furby

Current occupation: News poster
Reaction score
144
JASS:
function PlayerColor takes player whichPlayer, string whichString returns string
local string array c
local integer i = GetConvertedPlayerId(whichPlayer)
local string s = whichString
local integer Index = 0
set c[1] = &quot;|CFF0303&quot;
set c[2] = &quot;|C0042FF&quot;
set c[3] = &quot;|CFF1CB619&quot;
set c[4] = &quot;|CFF540081&quot;
set c[5] = &quot;|CFFFFFF01&quot;
set c[6] = &quot;|CFFFE8A0E&quot;
set c[7] = &quot;|CFF20C000&quot;
set c[8] = &quot;|CFFE55BB0&quot;
set c[9] = &quot;|CFF959697&quot;
set c[10] = &quot;|CFF7EBFF1&quot;
set c[11] = &quot;|CFF106246&quot;
set c[12] = &quot;|CFF4E2A04&quot;
set c[13] = &quot;|CFF000000&quot;
return c<i>+s+&quot;|r&quot;
endfunction</i>

Did you know arrays start at 0? It doesn't even need BJ function then. :rolleyes:
string s, integer Index... useless
This can be achieved by calling the function:

JASS:
call PlayerColor(player whichPlayer, string whichString)


For example, this

JASS:
call PlayerColor(&quot;hahahahaha!&quot;, Player(0))


will return:

hahahahaha!
Uh?? Are you sure? Because I did not know "hahahahaha!" is type player and Player(0) returns string... ;)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top