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_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_player_id())
// Other player object

scr_draw_player_name(name, gms_admin_rights(player_id), player_id)

Replies (3)

Last message on 7 Dec 2017

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.