Environment API¶
The main EldenGymEnv class implements the Gymnasium environment interface.
EldenGymEnv¶
EldenGymEnv ¶
Bases: Env
Elden Ring Gymnasium environment with non-blocking frame streaming.
Uses pysiphon's frame streaming for efficient polling-based observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenario_name
|
str
|
Boss scenario name |
required |
keybinds_filepath
|
str
|
Path to keybinds JSON file (v2 format: action → keys) |
required |
siphon_config_filepath
|
str
|
Path to siphon TOML config |
required |
memory_attributes
|
list[str]
|
List of memory attribute names to include in observation. Default: ["HeroHp", "HeroMaxHp", "NpcHp", "NpcMaxHp", "HeroAnimId", "NpcAnimId"] |
None
|
actions
|
list[str]
|
List of action names to include in action space. If None, all actions from keybinds file are used. Example: ["move_forward", "move_back", "move_left", "move_right", "dodge_roll/dash"] |
None
|
host
|
str
|
Siphon server host. Default: 'localhost:50051' |
'localhost:50051'
|
reward_function
|
RewardFunction
|
Custom reward function |
None
|
frame_format
|
str
|
Frame format for streaming ('jpeg' or 'raw'). Default: 'jpeg' |
'jpeg'
|
frame_quality
|
int
|
JPEG quality 1-100. Default: 85 |
85
|
max_steps
|
int
|
Maximum steps per episode. Default: None |
None
|
launch_game
|
bool
|
Whether to launch the game automatically. Default: True Set to False if game is already running |
True
|
save_file_name
|
str
|
Name of backup save file to copy during reset (e.g., "margit_checkpoint.sl2"). If None, no save file copying occurs. |
None
|
save_file_dir
|
str
|
Directory containing save files. Required if
save_file_name is provided. Typically: %APPDATA%/EldenRing/ |
None
|
use_device
|
str
|
Preferred input device - 'key' for keyboard (default) or 'mouse'. When 'mouse' is selected, uses mouse binding if available, otherwise falls back to keyboard. |
'key'
|
Source code in eldengym/env.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
close ¶
Close environment and clean up resources.
Source code in eldengym/env.py
render ¶
reset ¶
Reset environment - start new episode.
Source code in eldengym/env.py
step ¶
Execute one step with key toggling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Multi-binary array [0/1] for each semantic action in self.action_keys e.g., [1, 0, 0, 1, ...] to activate move_forward and dodge_roll/dash |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(observation, reward, terminated, truncated, info) |
Source code in eldengym/env.py
Methods¶
Core Gymnasium Methods¶
reset(seed=None, options=None)¶
Reset the environment to initial state.
This method: 1. Releases all keys from previous episode 2. Quits to title screen 3. Copies save file (if configured) 4. Re-enters game and starts scenario
Args:
- seed (int, optional): Random seed
- options (dict, optional): Additional options
Returns:
- observation (dict): Initial observation with keys 'frame' and memory attributes
- info (dict): Additional information
Example:
obs, info = env.reset()
print(f"Frame shape: {obs['frame'].shape}")
print(f"Starting HP: {obs['HeroHp']}")
step(action)¶
Execute one step in the environment with key toggling.
Args:
- action (np.ndarray): Multi-binary array where each element is 0 or 1 representing key states
Returns:
- observation (dict): New observation with 'frame' and memory attributes
- reward (float): Reward for the action
- terminated (bool): Whether episode ended (determined by reward function)
- truncated (bool): Whether episode was truncated (max steps reached)
- info (dict): Additional information including normalized values
Example:
action = env.action_space.sample() # Random multi-binary action
obs, reward, terminated, truncated, info = env.step(action)
print(f"Reward: {reward}, HP: {obs['HeroHp']}")
if terminated:
print(f"Episode ended!")
close()¶
Clean up environment resources.
Rendering¶
render()¶
Return current game frame from the latest observation.
Returns:
- np.ndarray: RGB frame (H, W, 3) in uint8 format
Example:
Note: The frame is captured asynchronously using pysiphon's frame streaming.
Properties¶
Action Space¶
The environment uses MultiBinary action space where each element represents a key state:
env.action_space # MultiBinary(n)
# Each element is 0 (key released) or 1 (key pressed)
# The keys are loaded from keybinds.json
# Example with default keybinds:
env.action_keys # ['W', 'A', 'S', 'D', 'SPACE', 'E', 'Q', 'R']
action = [1, 0, 0, 0, 1, 0, 0, 0] # Press W and SPACE
Keys are toggled intelligently - only changed when the action differs from current state.
Observation Space¶
The environment uses Dict observation space with frame, memory attributes, and computed real coordinates:
env.observation_space # Dict({
# 'frame': Box(0, 255, (H, W, 3), uint8),
#
# # Memory attributes (configurable)
# 'HeroHp': Box(-inf, inf, (), float32),
# 'HeroMaxHp': Box(-inf, inf, (), float32),
# 'NpcHp': Box(-inf, inf, (), float32),
# 'NpcMaxHp': Box(-inf, inf, (), float32),
# 'HeroAnimId': Box(-inf, inf, (), float32),
# 'NpcAnimId': Box(-inf, inf, (), float32),
#
# # Real coordinates (computed automatically)
# 'player_x': Box(-inf, inf, (), float32), # Player global X
# 'player_y': Box(-inf, inf, (), float32), # Player global Y
# 'player_z': Box(-inf, inf, (), float32), # Player global Z
# 'boss_x': Box(-inf, inf, (), float32), # Boss global X
# 'boss_y': Box(-inf, inf, (), float32), # Boss global Y
# 'boss_z': Box(-inf, inf, (), float32), # Boss global Z
# 'dist_to_boss': Box(0, inf, (), float32), # 2D distance to boss
# 'boss_z_relative': Box(-inf, inf, (), float32), # Boss Z relative to player
# })
The memory attributes are configurable via the memory_attributes parameter.
Real Coordinates¶
The environment automatically computes global coordinates for both player and boss using the local-to-global transform:
- Player coordinates (
player_x,player_y,player_z): Directly fromHeroGlobalPos - Boss coordinates (
boss_x,boss_y,boss_z): Computed asNpcLocalPos + (HeroGlobalPos - HeroLocalPos) - Distance (
dist_to_boss): 2D Euclidean distance (ignores Z) - Relative Z (
boss_z_relative):boss_z - player_z(positive = boss above player)
This transform is necessary because the NPC coordinate system resets at map boundaries, while the Hero global position remains stable.
Info Dictionary¶
The info dict returned by step() and reset() contains normalized values:
| Key | Type | Description |
|---|---|---|
normalized_hero_hp |
float | Player HP normalized (0-1) |
normalized_npc_hp |
float | Boss HP normalized (0-1) |
| Memory attributes | float | Raw values from observation |
step_count |
int | Steps in current episode |
Example:
obs, info = env.reset()
print(f"Player HP %: {info['normalized_hero_hp'] * 100:.1f}%")
print(f"Boss HP %: {info['normalized_npc_hp'] * 100:.1f}%")
Configuration¶
Using eldengym.make()¶
import eldengym
env = eldengym.make(
"Margit-v0", # Registered environment
# Optional overrides:
launch_game=False, # Don't launch if already running
memory_attributes=["HeroHp", "NpcHp", "HeroAnimId"], # Custom attributes
frame_format="jpeg", # Or "raw"
frame_quality=85, # JPEG quality (1-100)
max_steps=1000, # Max steps per episode
# Save file management (for reset)
save_file_name="margit_checkpoint.sl2",
save_file_dir=r"C:\Users\...\AppData\Roaming\EldenRing\76561198...",
# Custom reward function
reward_function=eldengym.ScoreDeltaReward(),
)
Direct Instantiation¶
from eldengym.env import EldenGymEnv
from eldengym.rewards import ScoreDeltaReward
env = EldenGymEnv(
scenario_name="Margit-v0",
keybinds_filepath="path/to/keybinds.json",
siphon_config_filepath="path/to/er_siphon_config.toml",
memory_attributes=["HeroHp", "HeroMaxHp", "NpcHp", "NpcMaxHp", "HeroAnimId", "NpcAnimId"],
host="localhost:50051",
reward_function=ScoreDeltaReward(),
frame_format="jpeg",
frame_quality=85,
max_steps=None,
launch_game=True,
save_file_name=None,
save_file_dir=None,
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
scenario_name |
str | Required | Boss scenario name |
keybinds_filepath |
str | Required | Path to keybinds JSON |
siphon_config_filepath |
str | Required | Path to Siphon TOML config |
memory_attributes |
list[str] | Default set | Memory values to poll |
host |
str | "localhost:50051" |
Siphon server address |
reward_function |
RewardFunction | ScoreDeltaReward() |
Reward calculator |
frame_format |
str | "jpeg" |
Frame format ("jpeg" or "raw") |
frame_quality |
int | 85 |
JPEG quality (1-100) |
max_steps |
int | None |
Max steps before truncation |
launch_game |
bool | True |
Auto-launch game on init |
save_file_name |
str | None |
Backup save to copy on reset |
save_file_dir |
str | None |
Directory with save files |