How to Train AI in Godot 4.4 - Reinforcement Learning Beginner Tutorial
This guide helps you set up Reinforcement Learning (RL) in Godot 4 using the godot_rl_agents plugin. Follow these steps to install dependencies, configure the project, and run an RL example.
Prerequisites
- Godot 4: Download and install from godotengine.org/download.
- .NET SDK: Install from dotnet.microsoft.com/download.
- Verify: Open a terminal and run
dotnet --version.
- Verify: Open a terminal and run
- Python 3.12: Download from python.org/downloads.
- Verify: Run
python --versionin a terminal.
- Verify: Run
Setup Instructions
1. Create Project Structure
Create a project folder with the following structure:
/project_root/
├── app/ # Godot project folder
├── stable_baselines3_example.py # RL example script2. Download Required Files
Godot RL Plugin: Download from github.com/edbeeching/godot_rl_agents_plugin.
- Place the plugin files in
app/addons/godot_rl_agents/.
- Place the plugin files in
Example Script: Download
stable_baselines3_example.pyfrom github.com/edbeeching/godot_rl_agents.- Save it in
/project_root/.
- Save it in
3. Install Python Dependencies
Install
uvfor Python package management:bashpip install uvInitialize a Python environment in
/project_root/:bashuv init uv add godot-rl
4. Install .NET Dependencies
Create a dummy C# script inside the Godot editor:
- In the bottom left of the Godot editor, right-click → Create New > Script → choose C# (e.g., name it
dummy.cs).
- In the bottom left of the Godot editor, right-click → Create New > Script → choose C# (e.g., name it
Verify the C# setup:
- After creating the script, a 🔨 (hammer) icon should appear in the top right corner of the Godot editor, indicating C# support is enabled.
Install the ONNX runtime for .NET:
bashcd app/ dotnet add package Microsoft.ML.OnnxRuntime --version 1.21.0
5. Enable the Plugin in Godot
- Open the
app/folder as a project in Godot 4. - Go to Project > Project Settings > Plugins.
- Enable the Godot RL Agents plugin.
Implementing RL in Godot
Create a new AIController node. Extend the script and implement these required functions (ensure it extends AIController at the top of your script):
func get_obs() -> Dictionary:
# Return observations (e.g., player position, environment state)
return {"obs": []}
func get_reward() -> float:
# Define the reward (e.g., +1 for reaching a goal)
return 0.0
func get_action_space() -> Dictionary:
# Define action space (continuous or discrete)
return {
"example_actions_continuous": {
"size": 2,
"action_type": "continuous"
},
"example_actions_discrete": {
"size": 2,
"action_type": "discrete"
}
}
func set_action(action) -> void:
# Apply the action (e.g., move character based on action values)
passRunning the RL Example
Use the provided Python script to train or test the RL model. (Change the save or export path if needed by adding /yourpath/model.zip). Run the following command in /project_root/:
uv run stable_baselines3_example.pyOptional Arguments
--save_model_path=model.zip: Save the model--resume_model_path=model.zip: Load a pre-trained model--onnx_export_path=model.onnx: Export the model to ONNX format--timesteps=100_000: Auto stop after 100,000 steps
Exporting the Model
/export/
├── game.exe # Game binary
├── model.onnx # AI model- Ensure the
.onnxfile is in the same directory as the final.exeexecutable.
Tips for Beginners
- Start Simple: Use a basic Godot scene (e.g., a 2D platformer) to test RL.
- Debug Observations: Ensure
get_obs()returns meaningful data.
For more details, visit the Godot RL Agents repository.