Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions godot_rl/core/godot_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
from collections import OrderedDict
from sys import platform
from typing import Optional
from typing import List, Optional

import numpy as np
from gymnasium import spaces
Expand Down Expand Up @@ -291,8 +291,8 @@ def _close(self):
print("exit was not clean, using atexit to close env")
self.close()

def _launch_env(
self,
@staticmethod
def _build_launch_cmd(
env_path,
port,
show_window,
Expand All @@ -301,8 +301,11 @@ def _launch_env(
action_repeat,
speedup,
**kwargs,
):
# --fixed-fps {framerate}
) -> List[str]:
"""Build the argv list used to launch the Godot game.

Kept separate from the actual launch so it can be unit tested without a game binary.
"""
path = convert_macos_path(env_path) if platform == "darwin" else env_path

launch_cmd = [path]
Expand All @@ -313,7 +316,8 @@ def _launch_env(
launch_cmd.append("--disable-render-loop")
launch_cmd.append("--headless")
if framerate is not None:
launch_cmd.append(f"--fixed-fps {framerate}")
# --fixed-fps is a Godot engine argument, it expects its value as a separate token
launch_cmd.extend(["--fixed-fps", str(framerate)])
if action_repeat is not None:
launch_cmd.append(f"--action_repeat={action_repeat}")
if speedup is not None:
Expand All @@ -322,6 +326,30 @@ def _launch_env(
for key, value in kwargs.items():
launch_cmd.append(f"--{key}={value}")

return launch_cmd

def _launch_env(
self,
env_path,
port,
show_window,
framerate,
seed,
action_repeat,
speedup,
**kwargs,
):
launch_cmd = self._build_launch_cmd(
env_path,
port,
show_window,
framerate,
seed,
action_repeat,
speedup,
**kwargs,
)

self.proc = subprocess.Popen(
launch_cmd,
start_new_session=True,
Expand Down
54 changes: 54 additions & 0 deletions tests/test_launch_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from godot_rl.core.godot_env import GodotEnv


def build(**overrides):
kwargs = dict(
env_path="game.x86_64",
port=11008,
show_window=True,
framerate=None,
seed=0,
action_repeat=None,
speedup=None,
)
kwargs.update(overrides)
return GodotEnv._build_launch_cmd(**kwargs)


def test_no_argument_carries_an_embedded_space():
# Popen receives argv as a list, so a token like "--fixed-fps 60" reaches Godot as a
# single unknown parameter and the engine aborts at startup.
cmd = build(framerate=60, action_repeat=4, speedup=8, show_window=False)
assert all(" " not in arg for arg in cmd[1:])


def test_fixed_fps_is_split_into_two_tokens():
cmd = build(framerate=60)
assert "--fixed-fps" in cmd
assert cmd[cmd.index("--fixed-fps") + 1] == "60"


def test_framerate_none_omits_the_flag():
assert not any("fixed-fps" in arg for arg in build())


def test_plugin_arguments_keep_the_equals_form():
cmd = build(port=11010, seed=3, action_repeat=4, speedup=8)
assert "--port=11010" in cmd
assert "--env_seed=3" in cmd
assert "--action_repeat=4" in cmd
assert "--speedup=8" in cmd


def test_hidden_window_adds_the_headless_flags():
cmd = build(show_window=False)
assert "--disable-render-loop" in cmd
assert "--headless" in cmd


def test_extra_kwargs_are_forwarded():
assert "--custom_arg=7" in build(custom_arg=7)


def test_env_path_stays_first():
assert build()[0] == "game.x86_64"