Giving players different username colors

GameMaker Server automatically sets a few variables on the other player object. Two of those variables are 'name' and 'player_id'. You can use these variables in other functions to check for things like admin rights, or to draw the player name.

Drawing a username


To draw a player name above a player, use the following code in the draw event of the other player object:

draw_text(x, y, name)



  • Note #1: If you want to align the name, you can use draw_set_halign and draw_set_valign. (Please see the official GameMaker manual for more info)

  • Note #2: You also need to call draw_self() if you want the sprite of the object to show up too.



Different colors for mods


To give mods a different username color, we need to set the drawing color based on the admin rights of the player:

if gms_other_admin_rights(player_id) > 0
{
    draw_set_color(c_blue)
} else {
    draw_set_color(c_black)
}
draw_text(x, y, name)


By using '> 0' you are checking for kicking or banning permissions. If you want to check this separately, your code should look like this:

if gms_other_admin_rights(player_id) & ar_owner
{
    draw_set_color(c_red)
} else if gms_other_admin_rights(player_id) & ar_ban
{
    draw_set_color(c_blue)
} else if gms_other_admin_rights(player_id) & ar_kick
{
    draw_set_color(c_gray)
} else {
    draw_set_color(c_black)
}
draw_text(x, y, name)


Drawing the username for the player object


As you might have noticed, all code up until now has only been for the other player object. There is an easy way to share this code with the player object, so that both objects draw usernames in the same way. Move the code in the draw event to a new script:

///scr_draw_player_name(name, admin_rights)

var name = argument0,
    admin_rights = argument1;
if admin_rights & ar_owner
{
    draw_set_color(c_red)
} else if admin_rights & ar_ban
{
    draw_set_color(c_blue)
} else if admin_rights & ar_kick
{
    draw_set_color(c_gray)
} else {
    draw_set_color(c_black)
}
draw_text(x, y, name)


In the draw events, you can now add the following code:
// Player object

scr_draw_player_name(gms_self_name(), gms_self_admin_rights())
// Other player object

scr_draw_player_name(name, gms_other_admin_rights(player_id))


More roles (pre-defined)


To add more roles which you know up front and cannot change (for example a YouTuber role), you can add additional checks like this:

if name == "Size43"
{
    draw_set_color(c_yellow)
} else {
    // ... the rest of the ifs (see above)

}
draw_text(x, y, name)


More roles, #2


For other roles that can change based on what the player does, like a green name for a player that has collected 1000G, you'll need to sync these stats. Continuing on the example of the 1000G, add the following to the step event of the player object:

gms_self_set("gold", variable_that_contains_the_amount_of_gold)


Now we can modify our script to check for the variable, like this:
///scr_draw_player_name(name, admin_rights, player_id)

var name = argument0,
    admin_rights = argument1,
    player_id = argument2;
if admin_rights > 0 // Just one check to save some space for the example

{
    draw_set_color(c_blue)
} else if gms_other_get(player_id, "gold") >= 1000
{
    draw_set_color(c_green)
} else {
    draw_set_color(c_black)
}
draw_text(x, y, name)


In the draw events, replace the old script invocations with the following:
// Player object

scr_draw_player_name(gms_self_name(), gms_self_admin_rights(), gms_self_playerid())
// Other player object

scr_draw_player_name(name, gms_other_admin_rights(player_id), player_id)

Replies (11)

Last message on 16 Jul 2021

iamadevil on 13 Jul 2021, 19:27:30
I'm not 100% if this is an error or not, but gms_self_player_id() doesn't exist, and is instead gms_self_playerid(). just letting you know!
Size43 (Administrator) on 16 Jul 2021, 22:37:51
Thank you for pointing this out! I'll fix that.
BabyGolden12 on 5 Nov 2019, 16:42:53
i keep getting a error saying Cannot set consent
BabyGolden12 on 8 Nov 2019, 23:06:49
Nvm but the name tags is all the way on top of the screen
Size43 (Administrator) on 9 Nov 2019, 13:17:16
You might want to play with the y value and add or subtract some offset to get the name tag in the right place.
Jocke on 18 Apr 2019, 10:36:16
I have a problem.. Game maker says that function or script " gms_admin_rights " doesn't exist. What do i do then for other Player object?
Size43 (Administrator) on 24 Apr 2019, 11:58:32
Oops, that should be gms_other_admin_rights. I'll fix it.
Jocke on 25 Apr 2019, 10:00:17
yeah i figured it out lol.
THanks anyways.
TypicalAndy on 6 Dec 2017, 21:13:27
How to see your Own Name?
ProFes on 7 Dec 2017, 13:48:53
name = gms_self_name();

draw_text(x, y, name);
TypicalAndy on 7 Dec 2017, 18:52:33
Thank you.