OS in Godot – Complete Guide

Unlocking the Power of Operating Systems in Godot 4

Embracing the capabilities of an operating system within a game development environment is a thrilling journey that can take your projects to new heights. In the bustling arena of game development, the operating system (OS) plays a fundamental role that often remains unseen, but is crucial for crafting seamless and responsive gaming experiences. We’re here to uncover the inner workings of the OS class in Godot 4, a powerhouse of functionality that bridges your games with the underlying system, and learn how it can turbocharge your creativity.

What is the OS Class in Godot 4?

Imagine having a command center within Godot 4, where you have direct lines of communication with the heart of your computer’s operating system. This is precisely what the OS class offers. In essence, it is a robust collection of methods and properties that allow game developers to query and interact with the host operating system. It wraps around common functionalities you would expect to communicate with, like video drivers, environmental variables, file system directories, and even the execution of external binaries.

What is it for?

The OS class is your gateway to performing a variety of system-level operations that can enhance gameplay, improve performance, or simply provide convenience. For instance, you might want to manage window properties, manipulate environment variables for different platforms, delay execution for a set amount of time, or access system directories in a uniform way across various operating systems. The versatility of the OS class serves as a testament to its indispensability in game development with Godot.

Why Should I Learn It?

Understanding the OS class is about harnessing control and expanding possibilities. You’ll be able to tailor experiences based on user’s system configurations, make real-time performance adjustments, and introduce elements like file management or system commands into your games. By mastering the OS class, you optimize your game for a myriad of platforms and devices. As we peel back the layers of what the OS class can do, you’ll discover new possibilities and potentially spur innovation within your projects—a compelling reason to deepen your Godot proficiency.

CTA Small Image

Querying System Information with the OS Class

One of the fundamental capabilities of the OS class in Godot 4 is querying system information. This allows you to adapt your game to the player’s hardware and software environments, leading to a more personalized and optimized experience.

Getting the OS Name:
To detect the operating system on which the game is running, you can use the following snippet:

var os_name = OS.get_name()
print("Your Operating System is: " + os_name)

Checking Screen Dimensions:
Sometimes, you need to know the size of the screen to adjust your game’s UI accordingly. This can be done with:

var screen_size = OS.get_screen_size()
print("The Screen Size is: " + str(screen_size))

Detecting Memory Usage:
To optimize game performance, checking memory usage helps in adjusting game content dynamically. Here’s how:

var memory_used = OS.get_static_memory_used()
print("Memory Used: " + str(memory_used) + " bytes")

Finding the User Data Directory:
When you want to save or retrieve user-specific data like save files, you’ll need to know the user data directory:

var user_data_dir = OS.get_user_data_dir()
print("User Data Directory: " + user_data_dir)

Manipulating the Game Window

Customizing the game window is a common task and Godot’s OS class offers straightforward methods for this purpose.

Setting Fullscreen Mode:
Enabling or disabling fullscreen mode can be done with a simple function call:

OS.window_fullscreen = true  # Enable fullscreen
# OS.window_fullscreen = false  # Disable fullscreen

Changing the Window Size:
Sometimes you might want to provide options for different window sizes. You can easily adjust the window size like so:

OS.window_size = Vector2(1280, 720)  # Set the window size to 1280x720 pixels

Minimizing and Maximizing:
You have the power to programmatically minimize or maximize the window with:

OS.window_minimized = true  # Minimize the window
# OS.window_maximized = true  # Maximize the window

Centering the Window:
For a pleasant user experience, you might want to center the game window on the screen:

OS.center_window()  # Center the window on the screen

In this part of the tutorial, we’ve explored how to query system information and manipulate the game window using Godot’s OS Class. These examples provide you with the basics to start tailoring your game’s operation to both system specifications and player preferences. Stay tuned for more advanced examples in the continuation of our comprehensive guide on using the OS class in Godot 4.Now that we’ve covered system querying and window management, let’s delve deeper into the capabilities of the OS class with practical coding examples that you can apply in your game development ventures using Godot 4.

Accessing System Directories:
Knowing how to get system directory paths is essential for file operations. Below, we retrieve the path for various system directories:

var desktop_dir = OS.get_system_dir(OS.SYSTEM_DIR_DESKTOP)
print("Desktop Directory: " + desktop_dir)

var documents_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS)
print("Documents Directory: " + documents_dir)

var downloads_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
print("Downloads Directory: " + downloads_dir)

Executing External Processes:
You can even launch external applications or run system commands from your game, hereby blending the lines between game and the operating system:

# Run an external text editor
OS.execute("notepad.exe", [], false)

# Open a web page in the default web browser

Managing the Clipboard:
Interacting with the clipboard allows for the sharing of text with other applications and vice versa, which can be a very handy feature within your game:

# Copy a string to the clipboard
OS.set_clipboard("Text copied from Godot!")

# Get the current string in the clipboard
var clipboard_contents = OS.get_clipboard()
print("Clipboard contains: " + clipboard_contents)

Delaying Execution:
Slowing down your code’s execution can be useful for timers, cooldowns, or to simulate processing time, and can be achieved as follows:

# Delay the execution for 1 second (1000 milliseconds)
print("1 second has passed.")

Handling Application Lifecycle:
Responding to application lifecycle events such as focus loss or gain is crucial for a polished user experience. Here’s a sample of how you can interact with these states:

# Check if the window has focus
if OS.has_focus():
    print("Window is focused.")
    print("Window is not focused.")

# Reacting to focus loss, you might save the game state
func _notification(what):
    if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT:
        print("Game state saved on focus loss.")

These code snippets showcase the breadth of functionality available through the OS class in Godot 4. By integrating system interaction within your game, you create more engaging and user-friendly applications that cater to a wide array of usage scenarios and elevate your game’s quality.

Remember, the key is to make sure that these tools enhance your gameplay experience without becoming a distraction or an annoyance to your players. Use these features judiciously to create games that feel responsive, thoughtful, and seamlessly integrated with the player’s device. Our journey through Godot’s OS class demonstrates just how much power and flexibility you, as a game developer, have at your fingertips. Embrace these possibilities, and your games will undoubtedly stand out.Environment Variables:
Accessing environment variables allows your game to adapt to the user’s system configuration, which can be crucial for cross-platform compatibility.

var path_variable = OS.get_environment("PATH")
if path_variable != null:
    print("PATH Environment Variable: " + path_variable)
    print("PATH Environment Variable not found.")

Handling System Power States:
It’s imperative to handle power states, especially if your game is running on a laptop or a mobile device.

# Check if the device is being charged
var is_charging = OS.is_in_low_processor_usage_mode()
print("Is the device charging? " + str(is_charging))

# Check the battery level
var battery_level = OS.get_power_percent_left()
print("Battery level: " + str(battery_level) + "%")

Working with Date and Time:
Date and time operations are often necessary for creating log files, saving game states with timestamps, or event scheduling within your game.

var current_time = OS.get_datetime()
print("Current datetime: " + str(current_time))

var current_unix_time = OS.get_unix_time()
print("Current Unix time: " + str(current_unix_time))

Internationalization and Locales:
If your game has a global audience, leveraging internationalization features can provide an enriching player experience.

# Get the current locale
var locale = OS.get_locale()
print("Current locale: " + locale)

# List all known locales - useful for language selection menus
var locales = OS.get_locale_list()
for locale in locales:
    print("Known Locale: " + locale)

Changing the Mouse Cursor:
Customizing the mouse cursor can add a nice touch to your game’s UI, making for a more immersive experience.

# Change the mouse cursor to a custom image
var cursor_texture = preload("res://your_custom_cursor.png")

# Reset the cursor to the system default

Getting and Setting System Time:
Occasionally, you may need to retrieve or adjust the system time, for example when creating an in-game event that syncs with real-world time.

# Get the system time
var system_time = OS.get_time()
print("System time: " + str(system_time))

# Set the system time (Note: Requires sufficient permissions)
var new_time = {"hour":15, "minute":30, "second":0}

Adjusting Thread Priority:
For multi-threaded applications, fine-tuning thread priority can help in managing resource allocation for different tasks.

# Increase the current thread's priority

# Reset the thread priority to normal

Each example provided here shows the breadth of system interaction that Godot 4 facilitates through its OS class, enabling your games and applications to be more dynamic and responsive to the player’s environment and preferences. As you experiment with these code snippets, consider how they can be woven into your game design to create experiences that feel as though they’re an integral part of the player’s device ecosystem. It’s through these thoughtful touches that your game can resonate more deeply with your players, and we encourage you to explore these features in depth to see how they can enhance your own projects.

Continue Your Game Development Journey

Your exploration into the OS class in Godot 4 is just the beginning. As you’ve seen, the ability to integrate and interact with the underlying operating system can bring a new level of polish and professionalism to your games. Now that you’ve got your feet wet, it’s the perfect time to dive deeper and broaden your horizons in game development.

The Godot Game Development Mini-Degree offers an extensive suite of high-quality courses designed to take you from a curious beginner to a capable game developer. Whether you’re looking to fine-tune your skills in 2D and 3D game creation, master the nuances of GDScript, or build out fully-fledged RPG, RTS, or platformer games, we’ve got you covered. This collection is a treasure trove for anyone serious about making an impact in the game development scene.

For those who crave a broader selection, visit our comprehensive list of Godot courses. With Zenva, you’ll gain in-demand skills through hands-on projects and build a robust portfolio showcasing your prowess. Each step in your learning journey will reaffirm your ability to create captivating games. So why wait? Take the next step with us and let’s turn your game development dreams into reality.


As we’ve ventured through the possibilities offered by the OS class in Godot 4, it’s clear that the tools to elevate your game to new standards of interaction and personalization are at your disposal. Remember, the key to effective game development is not just about crafting compelling gameplay but also ensuring that your game operates seamlessly within the ecosystem of your player’s devices. We hope this journey has sparked your imagination and inspired you to engage with these powerful features.

At Zenva’s Godot Game Development Mini-Degree, you’ll find the resources to continue expanding your skillset and pushing the boundaries of what’s possible in game development. Whether you’re looking to build your next game or improve upon an existing project, we’re here to support you with top-tier educational content. Join our community of learners and developers, and let’s create incredible gaming experiences together!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.