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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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