The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+3 votes

I want to be able to programmatically create new instances of my game listening on different ports. Is there any possibility of reading arguments passed when you run the game with something like:

godot --path ~/path/to/game/ --args "example"

And then get "example" when the code is running.

Any other easier or clever way of doing this would be much appreciated!

in Engine by (15 points)

1 Answer

+6 votes

You can use OS.get_cmdline_args() for this. However, you'll have to parse command-line arguments yourself. Due to how Godot currently parses them, there are a few limitations to be aware of:

  • Command-line arguments should be written in the form --key=value. Godot will try to open value as if it was a scene file if you use --key value.
  • Custom command-line arguments shouldn't conflict with engine arguments.

If this is too limiting, you can also use environment variables using OS.get_environment() instead.

PS: You can set Editor → Main Run Args in the Project Settings to define command-line arguments to be passed by the editor when running the project.

To parse command-line arguments into a dictionary, you could use something like this:

    var arguments = {}
    for argument in OS.get_cmdline_args():
        # Parse valid command-line arguments into a dictionary
        if argument.find("=") > -1:
            var key_value = argument.split("=")
            arguments[key_value[0].lstrip("--")] = key_value[1]
by (12,878 points)
edited by

Is it possible to parse command line arguments with exported game binaries as well?

Thanks a lot

Can you give an example of how to work with
Editor → Main Run Args
?

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.