From f77aea4f379de7f9a24a76646a2967795569691c Mon Sep 17 00:00:00 2001 From: Gabriel Cavedal Date: Wed, 25 Nov 2026 20:13:40 -0300 Subject: [PATCH] Decode hex-encoded float32 observations When the plugin's Sync node sends an observation as a hex string of its float32 bytes (keys without "2d"), decode it with np.frombuffer instead of receiving a JSON list of numbers. Plain lists and "2d" keys are unchanged, so older plugins keep working. Co-Authored-By: Claude Opus 5.5 --- godot_rl/core/godot_env.py | 12 +++++++++ tests/test_obs_encoding.py | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tests/test_obs_encoding.py diff --git a/godot_rl/core/godot_env.py b/godot_rl/core/godot_env.py index 72ed8948..60525a76 100644 --- a/godot_rl/core/godot_env.py +++ b/godot_rl/core/godot_env.py @@ -237,6 +237,11 @@ def _process_obs(self, response_obs: dict): if "2d" in k: for sub in response_obs: sub[k] = self._decode_2d_obs_from_string(sub[k], self.observation_space[k].shape) + elif isinstance(response_obs[0][k], str): + # Sent by the plugin's Sync node when binary observations are enabled: + # the float32 bytes of the observation, hex-encoded. + for sub in response_obs: + sub[k] = self._decode_float_obs_from_string(sub[k], self.observation_space[k].shape) return response_obs @@ -437,6 +442,13 @@ def _decode_2d_obs_from_string( ): return np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8).reshape(shape) + @staticmethod + def _decode_float_obs_from_string( + hex_string, + shape, + ): + return np.frombuffer(bytes.fromhex(hex_string), dtype=" GodotEnv: + # Only _process_obs is exercised, so no Godot process or socket is needed. + env = GodotEnv.__new__(GodotEnv) + env.observation_spaces = [obs_space] + env.observation_space = obs_space + return env + + +def test_float_obs_hex_string_is_decoded_exactly(): + values = np.random.default_rng(0).uniform(-1.0, 1.0, size=(3, 69)).astype(np.float32) + env = _env_with_obs_space(spaces.Dict({"obs": spaces.Box(low=-1.0, high=1.0, shape=(69,))})) + + response_obs = [{"obs": row.astype("