Skip to content

Commit

Permalink
Add prefix to object before spawning to avoid object registered twice…
Browse files Browse the repository at this point in the history
… in engine (#659)

* Add prefix to object before spawning to avoid object registered twice in engine

* format
  • Loading branch information
pengzhenghao authored Mar 1, 2024
1 parent 27f5e71 commit 6578760
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions metadrive/manager/scenario_light_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class ScenarioLightManager(BaseManager):
CLEAR_LIGHTS = False

OBJECT_PREFIX = "traffic_light_"

def __init__(self):
super(ScenarioLightManager, self).__init__()
self._scenario_id_to_obj_id = {}
Expand Down Expand Up @@ -41,12 +43,15 @@ def after_reset(self):
)
lane_info = self.engine.current_map.road_network.graph[str(scenario_lane_id)]
position = self._get_light_position(light_info)
name = scenario_lane_id if self.engine.global_config["force_reuse_object_name"] else None
name = self.OBJECT_PREFIX + scenario_lane_id if self.engine.global_config["force_reuse_object_name"
] else None
traffic_light = self.spawn_object(ScenarioTrafficLight, lane=lane_info.lane, position=position, name=name)
self._scenario_id_to_obj_id[scenario_lane_id] = traffic_light.id
self._obj_id_to_scenario_id[traffic_light.id] = scenario_lane_id
if self.engine.global_config["force_reuse_object_name"]:
assert scenario_lane_id == traffic_light.id, "Original id should be assigned to traffic lights"
assert self.OBJECT_PREFIX + scenario_lane_id == traffic_light.id, (
"Original id should be assigned to traffic lights"
)
self._lane_index_to_obj[lane_info.lane.index] = traffic_light
status = light_info[SD.TRAFFIC_LIGHT_STATUS][self.episode_step]
traffic_light.set_status(status)
Expand Down

0 comments on commit 6578760

Please sign in to comment.