Addons documentation
  • 😄Getting Started
  • 🧩Components
    • 👟Godot Essentials Motion Component
      • 🤸Top-Down Movement Component
      • 🤾Platform Movement Component
      • 🔲Grid Movement Component
    • 💓Health Component
    • 🎥Shake Camera Component
    • 🌪️Rotator Component
    • 🚀Projectile component
    • 🔁Finite State Machine
  • ⏳Autoload
    • 🎒Helpers
    • 🎧AudioManager
    • 🎬Scene Transicioner
    • ⚙️Environment variables
    • 🏆Achievements
  • 🎮Examples
    • 🧗‍♂️Alys - Precision platformer
Powered by GitBook
On this page
  • Getting started
  • Variable tracker
  • Example of use
  • Loading variables from other files
  • Signals
  • API reference
  1. Autoload

Environment variables

To keep environment variables secure, avoid hard-coding sensitive information into your source code. By utilizing environment variables, you can ensure that such information remains protected

PreviousScene TransicionerNextAchievements

Last updated 1 year ago

Environment variables provide a standardized and crucial method for effectively managing sensitive information within your software projects. These variables serve as a secure repository for storing a wide range of confidential data, including but not limited to API keys, access tokens, database credentials, and configuration settings.

Getting started

You can access all features by using the GodotEnvironment class in any part of your code. This class provides extra functionality while maintaining compatibility with OS.get_environment() and OS.set_environment() for runtime variable management.

By default, this class looks for .env files in the root of your project res:// To modify this path, you can use:

GodotEnvironment.ENVIRONMENT_FILES_PATH = "res://project"

or set the value on the Project settings -> GodotEnv -> Root directory:

Variable tracker

For internal execution convenience, the plugin tracks active variables without storing their values in array, as storing sensitive content in this array can risk data leaks or unauthorized access. This approach allows you to verify which variables have been read and loaded into memory without exposing their values

GodotEnvironment.ENVIRONMENT_VARIABLE_TRACKER
# ["ADDRESS", "PORT", "SERVER_ID]

Example of use

# .env file
ADDRESS=127.0.0.1
PORT=9492

SERVER_ID=1919

# random_script.gd
GodotEnvironment.load_env_file(".env")

GodotEnvironment.get_var("PORT") # Returns an empty string if does not exists
# or
GodotEnvironment.get_var_or_null("PORT") # Returns null instead
```
**You only need to load your environment variables once**, and there's no need to load them in every `_ready()` function in your nodes. If you have duplicate variables, the value used will be the one from the last occurrence. Therefore, it's important to review your files carefully to avoid unintentional overwriting.
```dotenv
ADDRESS=127.0.0.1
ADDRESS=192.168.1.55 # This is the one that will be assigned as it overwrites the previous one

Loading variables from other files

This class supports reading multiple environment files. For production, it's highly advisable to refrain from including the .env file in your source code repository. Instead, consider providing on the repository an .env.example file with blank values for the keys used. This strategy enables you to duplicate the file and input the values in your local environment, thereby averting the inadvertent exposure of sensitive information.

# .env.example
ADDRESS=
PORT=
SERVER_NAME=

# random_script.gd
GodotEnvironment.load_env_file(".env.example")
GodotEnvironment.load_env_file(".env.dev")
GodotEnvironment.load_env_file(".env.staging")
# ...

Signals

signal variable_added(key: String)
signal variable_removed(key: String)
signal variable_replaced(key: String)
signal env_file_loaded(filename: String)

API reference

get_var(key: String) -> String

This is an alternative to OS.get_environment(key)

GodotEnvironment.get_var("SERVER_PORT")

get_var_or_null(key: String)

Retrieve the value of an environment variable by its key or null it if it doesn't

GodotEnvironment.get_var_or_null("SERVER_PORT")

set_var(key: String, value: String = "") -> void

Set a environment variable on the runtime process, this is an alternative to OS.set_environment(key, value)

GodotEnvironment.set_var("API_KEY", "991918291921")

remove_var(key: String)-> void

Remove a variable from the runtime process

GodotEnvironment.remove_var("API_KEY")

create_environment_file(filename: String = ".env", overwrite: bool = false) -> void

Create an environment file with the specified filename. If it already exists, it can be overwritten

GodotEnvironment.create_environment_file(".env")
GodotEnvironment.add_var_to_file("env", "PORT", 3000)
GodotEnvironment.add_var_to_file("env", "ENCRYPTION_ALGORITHM", 'SHA256')

load_env_file(filename: String = ".env") -> void

Read an .env file and set the environment variables to be accessible in the code

GodotEnvironment.load_env_file(".env.example")

flush_environment_variables(filename: String = ".env", except: Array[String] = []) -> void

Remove environment variables from the current process runtime. You can add the keys that you do not want to be deleted in this process.

GodotEnvironment.flush_environment_variables(".env")
GodotEnvironment.flush_environment_variables(".env", ["IP_ADDRESS", "COUNTRY"])

add_var_to_file(filename: String, key: String, value: String = "") -> void

Add a key-value pair to an environment file and set the environment variable

GodotEnvironment.add_var_to_file("env", "PORT", 4500)
GodotEnvironment.add_var_to_file("env", "APP_NAME", 'FightingTournament')
⏳
⚙️
Godot2DEssentials settings section