OS

Inherits: Object

Operating System functions.

Description

Operating System functions. OS wraps the most common functionality to communicate with the host operating system, such as the clipboard, video driver, date and time, timers, environment variables, execution of binaries, command line, etc.

Tutorials

Methods

void

alert ( String text, String title="Alert!" )

bool

can_use_threads ( ) const

void

close_midi_inputs ( )

int

create_process ( String path, PackedStringArray arguments )

void

delay_msec ( int msec ) const

void

delay_usec ( int usec ) const

void

dump_memory_to_file ( String file )

void

dump_resources_to_file ( String file )

int

execute ( String path, PackedStringArray arguments, Array output=[], bool read_stderr=false )

int

find_keycode_from_string ( String string ) const

String

get_cache_dir ( ) const

PackedStringArray

get_cmdline_args ( )

String

get_config_dir ( ) const

PackedStringArray

get_connected_midi_inputs ( )

String

get_data_dir ( ) const

String

get_environment ( String variable ) const

String

get_executable_path ( ) const

PackedStringArray

get_granted_permissions ( ) const

String

get_keycode_string ( int code ) const

String

get_locale ( ) const

String

get_locale_language ( ) const

String

get_model_name ( ) const

String

get_name ( ) const

int

get_process_id ( ) const

int

get_processor_count ( ) const

int

get_static_memory_peak_usage ( ) const

int

get_static_memory_usage ( ) const

String

get_system_dir ( SystemDir dir, bool shared_storage=true ) const

int

get_thread_caller_id ( ) const

String

get_unique_id ( ) const

String

get_user_data_dir ( ) const

bool

has_environment ( String variable ) const

bool

has_feature ( String tag_name ) const

bool

is_debug_build ( ) const

bool

is_keycode_unicode ( int code ) const

bool

is_stdout_verbose ( ) const

bool

is_userfs_persistent ( ) const

Error

kill ( int pid )

void

open_midi_inputs ( )

void

print_all_resources ( String tofile="" )

void

print_all_textures_by_size ( )

void

print_resources_by_type ( PackedStringArray types )

void

print_resources_in_use ( bool short=false )

bool

request_permission ( String name )

bool

request_permissions ( )

bool

set_environment ( String variable, String value ) const

Error

set_thread_name ( String name )

void

set_use_file_access_save_and_swap ( bool enabled )

Error

shell_open ( String uri )

Enumerations

enum VideoDriver:

  • VIDEO_DRIVER_GLES2 = 0 --- The GLES2 rendering backend. It uses OpenGL ES 2.0 on mobile devices, OpenGL 2.1 on desktop platforms and WebGL 1.0 on the web.

  • VIDEO_DRIVER_VULKAN = 1 --- The Vulkan rendering backend.


enum Weekday:

  • DAY_SUNDAY = 0 --- Sunday.

  • DAY_MONDAY = 1 --- Monday.

  • DAY_TUESDAY = 2 --- Tuesday.

  • DAY_WEDNESDAY = 3 --- Wednesday.

  • DAY_THURSDAY = 4 --- Thursday.

  • DAY_FRIDAY = 5 --- Friday.

  • DAY_SATURDAY = 6 --- Saturday.


enum Month:

  • MONTH_JANUARY = 1 --- January.

  • MONTH_FEBRUARY = 2 --- February.

  • MONTH_MARCH = 3 --- March.

  • MONTH_APRIL = 4 --- April.

  • MONTH_MAY = 5 --- May.

  • MONTH_JUNE = 6 --- June.

  • MONTH_JULY = 7 --- July.

  • MONTH_AUGUST = 8 --- August.

  • MONTH_SEPTEMBER = 9 --- September.

  • MONTH_OCTOBER = 10 --- October.

  • MONTH_NOVEMBER = 11 --- November.

  • MONTH_DECEMBER = 12 --- December.


enum SystemDir:

  • SYSTEM_DIR_DESKTOP = 0 --- Desktop directory path.

  • SYSTEM_DIR_DCIM = 1 --- DCIM (Digital Camera Images) directory path.

  • SYSTEM_DIR_DOCUMENTS = 2 --- Documents directory path.

  • SYSTEM_DIR_DOWNLOADS = 3 --- Downloads directory path.

  • SYSTEM_DIR_MOVIES = 4 --- Movies directory path.

  • SYSTEM_DIR_MUSIC = 5 --- Music directory path.

  • SYSTEM_DIR_PICTURES = 6 --- Pictures directory path.

  • SYSTEM_DIR_RINGTONES = 7 --- Ringtones directory path.

Property Descriptions

  • bool low_processor_usage_mode

Default

false

Setter

set_low_processor_usage_mode(value)

Getter

is_in_low_processor_usage_mode()

If true, the engine optimizes for low processor usage by only refreshing the screen if needed. Can improve battery consumption on mobile.


  • int low_processor_usage_mode_sleep_usec

Default

6900

Setter

set_low_processor_usage_mode_sleep_usec(value)

Getter

get_low_processor_usage_mode_sleep_usec()

The amount of sleeping between frames when the low-processor usage mode is enabled (in microseconds). Higher values will result in lower CPU usage.

Method Descriptions

Displays a modal dialog box using the host OS' facilities. Execution is blocked until the dialog is closed.


  • bool can_use_threads ( ) const

Returns true if the current host platform is using multiple threads.


  • void close_midi_inputs ( )

Shuts down system MIDI driver.

Note: This method is implemented on Linux, macOS and Windows.


Creates a new process that runs independently of Godot. It will not terminate if Godot terminates. The file specified in path must exist and be executable. Platform path resolution will be used. The arguments are used in the given order and separated by a space.

If the process creation succeeds, the method will return the new process ID, which you can use to monitor the process (and potentially terminate it with kill). If the process creation fails, the method will return -1.

For example, running another instance of the project:

var pid = OS.create_process(OS.get_executable_path(), [])

See execute if you wish to run an external command and retrieve the results.

Note: This method is implemented on Android, iOS, Linux, macOS and Windows.

Note: On macOS, sandboxed applications are limited to run only embedded helper executables, specified during export.


  • void delay_msec ( int msec ) const

Delays execution of the current thread by msec milliseconds. msec must be greater than or equal to 0. Otherwise, delay_msec will do nothing and will print an error message.

Note: delay_msec is a blocking way to delay code execution. To delay code execution in a non-blocking way, see SceneTree.create_timer. Yielding with SceneTree.create_timer will delay the execution of code placed below the yield without affecting the rest of the project (or editor, for EditorPlugins and EditorScripts).

Note: When delay_msec is called on the main thread, it will freeze the project and will prevent it from redrawing and registering input until the delay has passed. When using delay_msec as part of an EditorPlugin or EditorScript, it will freeze the editor but won't freeze the project if it is currently running (since the project is an independent child process).


  • void delay_usec ( int usec ) const

Delays execution of the current thread by usec microseconds. usec must be greater than or equal to 0. Otherwise, delay_usec will do nothing and will print an error message.

Note: delay_usec is a blocking way to delay code execution. To delay code execution in a non-blocking way, see SceneTree.create_timer. Yielding with SceneTree.create_timer will delay the execution of code placed below the yield without affecting the rest of the project (or editor, for EditorPlugins and EditorScripts).

Note: When delay_usec is called on the main thread, it will freeze the project and will prevent it from redrawing and registering input until the delay has passed. When using delay_usec as part of an EditorPlugin or EditorScript, it will freeze the editor but won't freeze the project if it is currently running (since the project is an independent child process).


  • void dump_memory_to_file ( String file )

Dumps the memory allocation ringlist to a file (only works in debug).

Entry format per line: "Address - Size - Description".


  • void dump_resources_to_file ( String file )

Dumps all used resources to file (only works in debug).

Entry format per line: "Resource Type : Resource Location".

At the end of the file is a statistic of all used Resource Types.


Executes a command. The file specified in path must exist and be executable. Platform path resolution will be used. The arguments are used in the given order and separated by a space. If an output Array is provided, the complete shell output of the process will be appended as a single String element in output. If read_stderr is true, the output to the standard error stream will be included too.

If the command is successfully executed, the method will return the exit code of the command, or -1 if it fails.

Note: The Godot thread will pause its execution until the executed command terminates. Use Thread to create a separate thread that will not pause the Godot thread, or use create_process to create a completely independent process.

For example, to retrieve a list of the working directory's contents:

var output = []
var exit_code = OS.execute("ls", ["-l", "/tmp"], output)

To execute a composite command, a platform-specific shell can be invoked. For example:

var output = []
OS.execute("CMD.exe", ["/C", "cd %TEMP% && dir"], output)

Note: This method is implemented on Android, iOS, Linux, macOS and Windows.

Note: On macOS, sandboxed applications are limited to run only embedded helper executables, specified during export.


  • int find_keycode_from_string ( String string ) const

Returns the keycode of the given string (e.g. "Escape").


  • String get_cache_dir ( ) const

Returns the global cache data directory according to the operating system's standards. On desktop platforms, this path can be overridden by setting the XDG_CACHE_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_config_dir and get_data_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


Returns the command-line arguments passed to the engine.

Command-line arguments can be written in any form, including both --key value and --key=value forms so they can be properly parsed, as long as custom command-line arguments do not conflict with engine arguments.

You can also incorporate environment variables using the get_environment method.

You can set ProjectSettings.editor/run/main_run_args to define command-line arguments to be passed by the editor when running the project.

Here's a minimal example on how to parse command-line arguments into a dictionary using the --key=value form for arguments:

var arguments = {}
for argument in OS.get_cmdline_args():
    if argument.find("=") > -1:
        var key_value = argument.split("=")
        arguments[key_value[0].lstrip("--")] = key_value[1]

  • String get_config_dir ( ) const

Returns the global user configuration directory according to the operating system's standards. On desktop platforms, this path can be overridden by setting the XDG_CONFIG_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_cache_dir and get_data_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


Returns an array of MIDI device names.

The returned array will be empty if the system MIDI driver has not previously been initialised with open_midi_inputs.

Note: This method is implemented on Linux, macOS and Windows.


  • String get_data_dir ( ) const

Returns the global user data directory according to the operating system's standards. On desktop platforms, this path can be overridden by setting the XDG_DATA_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_cache_dir and get_config_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


Returns the value of an environment variable. Returns an empty string if the environment variable doesn't exist.

Note: Double-check the casing of variable. Environment variable names are case-sensitive on all platforms except Windows.


  • String get_executable_path ( ) const

Returns the path to the current engine executable.


With this function, you can get the list of dangerous permissions that have been granted to the Android application.

Note: This method is implemented on Android.


  • String get_keycode_string ( int code ) const

Returns the given keycode as a string (e.g. Return values: "Escape", "Shift+Escape").

See also InputEventKey.keycode and InputEventKey.get_keycode_with_modifiers.


Returns the host OS locale as a string of the form language_Script_COUNTRY_VARIANT@extra. If you want only the language code and not the fully specified locale from the OS, you can use get_locale_language.

language - 2 or 3-letter language code, in lower case.

Script - optional, 4-letter script code, in title case.

COUNTRY - optional, 2 or 3-letter country code, in upper case.

VARIANT - optional, language variant, region and sort order. Variant can have any number of underscored keywords.

extra - optional, semicolon separated list of additional key words. Currency, calendar, sort order and numbering system information.


  • String get_locale_language ( ) const

Returns the host OS locale's 2 or 3-letter language code as a string which should be consistent on all platforms. This is equivalent to extracting the language part of the get_locale string.

This can be used to narrow down fully specified locale strings to only the "common" language code, when you don't need the additional information about country code or variants. For example, for a French Canadian user with fr_CA locale, this would return fr.


  • String get_model_name ( ) const

Returns the model name of the current device.

Note: This method is implemented on Android and iOS. Returns "GenericDevice" on unsupported platforms.


Returns the name of the host OS. Possible values are: "Android", "iOS", "HTML5", "macOS", "Server", "Windows", "UWP", "X11".


  • int get_process_id ( ) const

Returns the project's process ID.

Note: This method is implemented on Android, iOS, Linux, macOS and Windows.


  • int get_processor_count ( ) const

Returns the number of threads available on the host machine.


  • int get_static_memory_peak_usage ( ) const

Returns the maximum amount of static memory used (only works in debug).


  • int get_static_memory_usage ( ) const

Returns the amount of static memory being used by the program in bytes.


Returns the actual path to commonly used folders across different platforms. Available locations are specified in SystemDir.

Note: This method is implemented on Android, Linux, macOS and Windows.

Note: Shared storage is implemented on Android and allows to differentiate between app specific and shared directories. Shared directories have additional restrictions on Android.


  • int get_thread_caller_id ( ) const

Returns the ID of the current thread. This can be used in logs to ease debugging of multi-threaded applications.

Note: Thread IDs are not deterministic and may be reused across application restarts.


  • String get_unique_id ( ) const

Returns a string that is unique to the device.

Note: This string may change without notice if the user reinstalls/upgrades their operating system or changes their hardware. This means it should generally not be used to encrypt persistent data as the data saved before an unexpected ID change would become inaccessible. The returned string may also be falsified using external programs, so do not rely on the string returned by get_unique_id for security purposes.

Note: Returns an empty string on HTML5 and UWP, as this method isn't implemented on those platforms yet.


  • String get_user_data_dir ( ) const

Returns the absolute directory path where user data is written (user://).

On Linux, this is ~/.local/share/godot/app_userdata/[project_name], or ~/.local/share/[custom_name] if use_custom_user_dir is set.

On macOS, this is ~/Library/Application Support/Godot/app_userdata/[project_name], or ~/Library/Application Support/[custom_name] if use_custom_user_dir is set.

On Windows, this is %APPDATA%\Godot\app_userdata\[project_name], or %APPDATA%\[custom_name] if use_custom_user_dir is set. %APPDATA% expands to %USERPROFILE%\AppData\Roaming.

If the project name is empty, user:// falls back to res://.

Not to be confused with get_data_dir, which returns the global (non-project-specific) user data directory.


Returns true if the environment variable with the name variable exists.

Note: Double-check the casing of variable. Environment variable names are case-sensitive on all platforms except Windows.


Returns true if the feature for the given feature tag is supported in the currently running instance, depending on the platform, build, etc. Can be used to check whether you're currently running a debug build, on a certain platform or arch, etc. Refer to the Feature Tags documentation for more details.

Note: Tag names are case-sensitive.


  • bool is_debug_build ( ) const

Returns true if the Godot binary used to run the project is a debug export template, or when running in the editor.

Returns false if the Godot binary used to run the project is a release export template.

To check whether the Godot binary used to run the project is an export template (debug or release), use OS.has_feature("standalone") instead.


  • bool is_keycode_unicode ( int code ) const

Returns true if the input keycode corresponds to a Unicode character.


  • bool is_stdout_verbose ( ) const

Returns true if the engine was executed with the --verbose or -v command line argument, or if ProjectSettings.debug/settings/stdout/verbose_stdout is true. See also @GlobalScope.print_verbose.


  • bool is_userfs_persistent ( ) const

If true, the user:// file system is persistent, so that its state is the same after a player quits and starts the game again. Relevant to the HTML5 platform, where this persistence may be unavailable.


Kill (terminate) the process identified by the given process ID (pid), e.g. the one returned by execute in non-blocking mode.

Note: This method can also be used to kill processes that were not spawned by the game.

Note: This method is implemented on Android, iOS, Linux, macOS and Windows.


  • void open_midi_inputs ( )

Initialises the singleton for the system MIDI driver.

Note: This method is implemented on Linux, macOS and Windows.


  • void print_all_resources ( String tofile="" )

Shows all resources in the game. Optionally, the list can be written to a file by specifying a file path in tofile.


  • void print_all_textures_by_size ( )

Shows the list of loaded textures sorted by size in memory.


Shows the number of resources loaded by the game of the given types.


  • void print_resources_in_use ( bool short=false )

Shows all resources currently used by the game.


At the moment this function is only used by AudioDriverOpenSL to request permission for RECORD_AUDIO on Android.


  • bool request_permissions ( )

With this function, you can request dangerous permissions since normal permissions are automatically granted at install time in Android applications.

Note: This method is implemented on Android.


Sets the value of the environment variable variable to value. The environment variable will be set for the Godot process and any process executed with execute after running set_environment. The environment variable will not persist to processes run after the Godot process was terminated.

Note: Double-check the casing of variable. Environment variable names are case-sensitive on all platforms except Windows.


Sets the name of the current thread.


  • void set_use_file_access_save_and_swap ( bool enabled )

Enables backup saves if enabled is true.


Requests the OS to open a resource with the most appropriate program. For example:

  • OS.shell_open("C:\\Users\name\Downloads") on Windows opens the file explorer at the user's Downloads folder.

  • OS.shell_open("https://godotengine.org") opens the default web browser on the official Godot website.

  • OS.shell_open("mailto:example@example.com") opens the default email client with the "To" field set to example@example.com. See Customizing ``mailto:` Links <https://blog.escapecreative.com/customizing-mailto-links/>`__ for a list of fields that can be added.

Use ProjectSettings.globalize_path to convert a res:// or user:// path into a system path for use with this method.

Note: This method is implemented on Android, iOS, HTML5, Linux, macOS and Windows.