How to get the user name?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

How do I get the name of the user that is running my game?

You mean the user name of the session in your operative system?

p7f | 2019-01-29 13:26

Yes, I need to get the user name to assign it as the player name into my game.

JulioYagami | 2019-01-29 14:44

:bust_in_silhouette: Reply From: Calinou

You can use the USERNAME environment variable, which seems to be defined by default on Windows, macOS and Linux:

var username
if OS.has_environment("USERNAME"):
    username = OS.get_environment("USERNAME")
else:
    username = "Player"

The snippet above falls back to Player if the environment variable doesn’t exist for some reason.

Keep in mind this may not be a good solution for multiplayer games, as many people use their real name as their computer user name and would rather stay anonymous.

‘USERNAME’ is not standard (just windows), ‘USER’ is (Linux, MacOS, Android, iOS, …)

skaiware | 2021-03-13 22:03