diff --git a/gymnasium/wrappers/rendering.py b/gymnasium/wrappers/rendering.py index 526c47bff..80206e78b 100644 --- a/gymnasium/wrappers/rendering.py +++ b/gymnasium/wrappers/rendering.py @@ -472,6 +472,10 @@ def __init__(self, env: gym.Env[ObsType, ActType]): gym.utils.RecordConstructorArgs.__init__(self) gym.Wrapper.__init__(self, env) + self.screen_size = None + self.window = None # Has to be initialized before asserts, as self.window is used in auto close + self.clock = None + assert ( self.env.render_mode in self.ACCEPTED_RENDER_MODES ), f"Expected env.render_mode to be one of {self.ACCEPTED_RENDER_MODES} but got '{env.render_mode}'" @@ -479,10 +483,6 @@ def __init__(self, env: gym.Env[ObsType, ActType]): "render_fps" in self.env.metadata ), "The base environment must specify 'render_fps' to be used with the HumanRendering wrapper" - self.screen_size = None - self.window = None - self.clock = None - if "human" not in self.metadata["render_modes"]: self.metadata = deepcopy(self.env.metadata) self.metadata["render_modes"].append("human") diff --git a/gymnasium/wrappers/vector/rendering.py b/gymnasium/wrappers/vector/rendering.py index 20e92a667..95c94bd1b 100644 --- a/gymnasium/wrappers/vector/rendering.py +++ b/gymnasium/wrappers/vector/rendering.py @@ -31,6 +31,11 @@ def __init__(self, env: VectorEnv, screen_size: tuple[int, int] | None = None): """ VectorWrapper.__init__(self, env) + self.screen_size = screen_size + self.scaled_subenv_size, self.num_rows, self.num_cols = None, None, None + self.window = None # Has to be initialized before asserts, as self.window is used in auto close + self.clock = None + assert ( self.env.render_mode in self.ACCEPTED_RENDER_MODES ), f"Expected env.render_mode to be one of {self.ACCEPTED_RENDER_MODES} but got '{env.render_mode}'" @@ -38,11 +43,6 @@ def __init__(self, env: VectorEnv, screen_size: tuple[int, int] | None = None): "render_fps" in self.env.metadata ), "The base environment must specify 'render_fps' to be used with the HumanRendering wrapper" - self.screen_size = screen_size - self.scaled_subenv_size, self.num_rows, self.num_cols = None, None, None - self.window = None - self.clock = None - if "human" not in self.metadata["render_modes"]: self.metadata = deepcopy(self.env.metadata) self.metadata["render_modes"].append("human")