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("