# Find index of env in DefaultCallbacks

**URL:** https://discuss.ray.io/t/find-index-of-env-in-defaultcallbacks/3919
**Category:** RLlib
**Created:** [October 25, 2021, 1:15pm UTC](https://discuss.ray.io/t/find-index-of-env-in-defaultcallbacks/3919 "2021-10-25T13:15:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![albheim](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@albheim](https://discuss.ray.io/u/albheim)
#### Post date: [October 25, 2021, 1:15pm UTC](https://discuss.ray.io/t/find-index-of-env-in-defaultcallbacks/3919/1 "2021-10-25T13:15:22Z")

</div>

I have been using `DefaultCallbacks` as shown [here](https://github.com/ray-project/ray/blob/master/rllib/examples/custom_metrics_and_callbacks.py) and manage to log my data in the `on_episode_end` method.

```python
def on_episode_end(self, *, worker: RolloutWorker, base_env: BaseEnv,
                       policies: Dict[str, Policy], episode: MultiAgentEpisode,
                       env_index: int, **kwargs):
        env = base_env.get_unwrapped()[env_index].unwrapped
        episode.custom_metrics[f"env{env_index}/some_value"] = env.some_value

```

I’m running PPO with `num_workers=4` and wanted to log some values from the environments individually (I have some random driving process that is seeded and thus will make the environments evolve differently on each worker).

What I did not realise until recently is that the `env_index` variable is always zero, I had assumed it would contain an index which I should use to index into `base_env` to get the environment that had triggered the end of episode call.

This might explain a lot of confusing data I have looked at, unfortunately I have always just defaulted to looking at environment 0 in tensorboard, and I didn’t realise until now that this was the data from all environments all jumbled together.

I see that `env_index` seems to be obsoleted, is this what it was once used for and maybe it just broke when I updated the ray version a while ago? Can I still somehow get to know which of all my envs are currently logging a value? Or should I just keep a unique id in each of them?

---

<div class="post-metadata">

### Author: ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)
#### Post date: [October 25, 2021, 1:48pm UTC](https://discuss.ray.io/t/find-index-of-env-in-defaultcallbacks/3919/2 "2021-10-25T13:48:16Z")

</div>

Hi @albheim,

The `env_index` is used for vector envs to distinguish between multiple environments on the same worker. This is determined based on the value of `num_envs_per_worker`. The worker has a `worker_index`member that you could use

* * *

> **[Manny is helping people do cool things with RL](https://www.buymeacoffee.com/mannyv)**
>
> Hey, so glad we got to connect. If you found my suggestions useful feel free to buy me a coffee!

---

<div class="post-metadata">

### Author: ![albheim](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@albheim](https://discuss.ray.io/u/albheim)
#### Post date: [October 26, 2021, 7:11am UTC](https://discuss.ray.io/t/find-index-of-env-in-defaultcallbacks/3919/3 "2021-10-26T07:11:40Z")

</div>

Just to clarify for anyone else showing up here, the corresponding code in the `DefaultCallbacks` would look something like

```python
def on_episode_end(self, *, worker: RolloutWorker, base_env: BaseEnv,
                       policies: Dict[str, Policy], episode: MultiAgentEpisode,
                       env_index: int, **kwargs):
        env = base_env.get_unwrapped()[0].unwrapped
        worker_index = worker.worker_index
        episode.custom_metrics[f"env{worker_index}/some_value"] = env.some_value
```
