0 votes

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

in Engine by (682 points)

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

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

1 Answer

0 votes
Best answer

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.

by (12,649 points)
selected by

'USERNAME' is not standard (just windows), 'USER' is (Linux, MacOS, Android, iOS, ...)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.