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
249
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
964
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
964
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
964
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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