Wrappers API¶
Gymnasium wrappers for EldenGym environments.
wrappers ¶
AnimFrameWrapper ¶
Bases: Wrapper
Track boss animation ID and elapsed frames with same animation.
Adds to observation: - boss_anim_id: Current NpcAnimId - elapsed_frames: Number of frames since animation changed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
EldenGym environment |
required | |
anim_id_key
|
Key for animation ID in obs (default: 'NpcAnimId') |
'NpcAnimId'
|
Source code in eldengym/wrappers.py
DictFrameStack ¶
Bases: ObservationWrapper
Stack last N frames for Dict observation spaces.
Stacks the 'frame' key while preserving other observation keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment with Dict observation space |
required | |
num_stack
|
Number of frames to stack (default: 4) |
4
|
|
frame_key
|
Key for frame data in observation dict (default: 'frame') |
'frame'
|
Source code in eldengym/wrappers.py
observation ¶
Stack frames and return modified observation.
Source code in eldengym/wrappers.py
DictGrayscaleFrame ¶
Bases: ObservationWrapper
Convert frames to grayscale in Dict observation spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment with Dict observation space |
required | |
frame_key
|
Key for frame data in observation dict (default: 'frame') |
'frame'
|
Source code in eldengym/wrappers.py
observation ¶
Convert frame to grayscale and return modified observation.
Source code in eldengym/wrappers.py
DictResizeFrame ¶
Bases: ObservationWrapper
Resize frames in Dict observation spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment with Dict observation space |
required | |
width
|
Target width (default: 84) |
84
|
|
height
|
Target height (default: 84) |
84
|
|
frame_key
|
Key for frame data in observation dict (default: 'frame') |
'frame'
|
Source code in eldengym/wrappers.py
observation ¶
Resize frame and return modified observation.
Source code in eldengym/wrappers.py
DodgePolicyRewardWrapper ¶
Bases: Wrapper
Reward shaping for dodge policy training.
Applies configurable penalties for: - Taking damage (hit by boss) - Dodging (to prevent spam) - Being in danger zone (between soft and hard boundary) - Crossing hard boundary (OOB/teleported)
Requires HPRefundWrapper and OOBSafetyWrapper to be applied first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
EldenGym environment with HPRefundWrapper and OOBSafetyWrapper |
required | |
dodge_action_idx
|
int
|
Index of dodge action in action space |
required |
hit_penalty
|
float
|
Penalty for taking damage (default: -1.0) |
-1.0
|
dodge_penalty
|
float
|
Penalty per dodge to prevent spam (default: -0.01) |
-0.01
|
danger_zone_penalty
|
float
|
Penalty for being between soft/hard boundary (default: -0.1) |
-0.1
|
oob_penalty
|
float
|
Penalty for crossing hard boundary/teleport (default: -1.0) |
-1.0
|
Source code in eldengym/wrappers.py
FrameStack ¶
Bases: ObservationWrapper
Stack last N frames (for simple array observations)
Source code in eldengym/wrappers.py
GrayscaleFrame ¶
Bases: ObservationWrapper
Convert to grayscale (for simple array observations)
Source code in eldengym/wrappers.py
HPRefundWrapper ¶
Bases: Wrapper
Refund player and/or boss HP after each step.
Useful for evaluation and data collection where you want to prevent episode termination due to HP loss.
Tracks damage by comparing consecutive observations to avoid race conditions with the refund timing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
EldenGym environment |
required | |
refund_player
|
Whether to refund player HP (default: True) |
True
|
|
refund_boss
|
Whether to refund boss HP (default: False) |
False
|
|
player_hp_attr
|
Attribute name for player HP (default: 'HeroHp') |
'HeroHp'
|
|
player_max_hp_attr
|
Attribute name for player max HP (default: 'HeroMaxHp') |
'HeroMaxHp'
|
|
boss_hp_attr
|
Attribute name for boss HP (default: 'NpcHp') |
'NpcHp'
|
|
boss_max_hp_attr
|
Attribute name for boss max HP (default: 'NpcMaxHp') |
'NpcMaxHp'
|
Source code in eldengym/wrappers.py
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 | |
NormalizeMemoryAttributes ¶
Bases: ObservationWrapper
Normalize memory attribute values to [0, 1] or [-1, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment with Dict observation space |
required | |
attribute_ranges
|
Dict mapping attribute names to (min, max) tuples. If not provided, will use observed min/max during runtime. |
None
|
|
frame_key
|
Key to skip normalization (default: 'frame') |
'frame'
|
Source code in eldengym/wrappers.py
observation ¶
Normalize memory attributes.
Source code in eldengym/wrappers.py
OOBSafetyWrapper ¶
Bases: Wrapper
Out-of-bounds detection and recovery via teleportation.
Uses soft/hard boundary system: - Soft boundary: Tracks last safe position when inside - Hard boundary: Triggers teleport to last safe position when crossed
Adds to info: - oob_detected: True if player crossed hard boundary - teleported: True if teleport was triggered - last_safe_xyz: Last known safe position (inside soft boundary)
Requires player coordinates in obs (player_x, player_y, player_z from EldenGymEnv).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
EldenGym environment with real coords |
required | |
boundary
|
ArenaBoundary instance with is_inside(x, y) method |
required | |
soft_margin
|
Distance inside the hard boundary for safe zone (default: 3.0) - Always positive, represents how far inside the hard boundary |
3.0
|
|
hard_margin
|
Distance to extend/shrink hard boundary (default: 0.0) - Positive values extend the boundary outward (more permissive) - Negative values shrink the boundary inward (more restrictive) |
0.0
|
Source code in eldengym/wrappers.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |
ResizeFrame ¶
Bases: ObservationWrapper
Resize frames to target shape (for simple array observations)
Source code in eldengym/wrappers.py
SDFObsWrapper ¶
Bases: Wrapper
Add SDF (Signed Distance Field) observations for arena boundary awareness.
Adds to observation: - sdf_value: Signed distance to boundary (negative = inside) - sdf_normal_x: X component of normal vector pointing to boundary - sdf_normal_y: Y component of normal vector pointing to boundary
Requires player coordinates in obs (player_x, player_y from EldenGymEnv).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
EldenGym environment with real coords |
required | |
boundary
|
ArenaBoundary instance with query_sdf(x, y) method |
required | |
live_plot
|
Enable live visualization of positions and SDF |
False
|
Source code in eldengym/wrappers.py
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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
Available Wrappers¶
Dict Observation Wrappers¶
These wrappers work with EldenGym's Dict observation space (containing 'frame' and memory attributes).
DictFrameStack¶
Stack the last N frames while preserving other observation keys.
from eldengym.wrappers import DictFrameStack
env = eldengym.make("Margit-v0", ...)
env = DictFrameStack(env, num_stack=4) # Stack last 4 frames
DictResizeFrame¶
Resize frames to a target resolution.
from eldengym.wrappers import DictResizeFrame
env = eldengym.make("Margit-v0", ...)
env = DictResizeFrame(env, width=224, height=224)
DictGrayscaleFrame¶
Convert frames to grayscale.
from eldengym.wrappers import DictGrayscaleFrame
env = eldengym.make("Margit-v0", ...)
env = DictGrayscaleFrame(env)
NormalizeMemoryAttributes¶
Normalize memory attribute values to [0, 1].
from eldengym.wrappers import NormalizeMemoryAttributes
env = eldengym.make("Margit-v0", ...)
env = NormalizeMemoryAttributes(env, attribute_ranges={
"HeroHp": (0, 1900),
"NpcHp": (0, 10000),
})
Utility Wrappers¶
HPRefundWrapper¶
Refund player and/or boss HP after each step. Useful for evaluation and data collection where you want to prevent episode termination due to HP loss.
from eldengym.wrappers import HPRefundWrapper
env = eldengym.make("Margit-v0", ...)
# Refund only player HP (default)
env = HPRefundWrapper(env, refund_player=True, refund_boss=False)
# Refund both player and boss HP
env = HPRefundWrapper(env, refund_player=True, refund_boss=True)
Parameters:
refund_player(bool): Whether to refund player HP. Default:Truerefund_boss(bool): Whether to refund boss HP. Default:Falseplayer_hp_attr(str): Attribute name for player HP. Default:'HeroHp'player_max_hp_attr(str): Attribute name for player max HP. Default:'HeroMaxHp'boss_hp_attr(str): Attribute name for boss HP. Default:'NpcHp'boss_max_hp_attr(str): Attribute name for boss max HP. Default:'NpcMaxHp'
Info dict additions:
When refund_player=True:
- info["player_damage_taken"] - Raw HP damage taken this step
- info["player_damage_taken_normalized"] - Damage as fraction of max HP (0.0 to 1.0)
When refund_boss=True:
- info["boss_damage_dealt"] - Raw HP damage dealt to boss this step
- info["boss_damage_dealt_normalized"] - Damage as fraction of boss max HP (0.0 to 1.0)
Example with reward shaping:
from eldengym import HPRefundWrapper
env = eldengym.make("Margit-v0", ...)
env = HPRefundWrapper(env, refund_player=True)
obs, info = env.reset()
for _ in range(1000):
action = policy(obs)
obs, reward, term, trunc, info = env.step(action)
# Penalize taking damage
damage_penalty = -info.get("player_damage_taken_normalized", 0) * 10.0
shaped_reward = reward + damage_penalty
AnimFrameWrapper¶
Track boss animation ID and elapsed frames since animation changed. Useful for learning animation-based dodge timing.
from eldengym.wrappers import AnimFrameWrapper
env = eldengym.make("Margit-v0", ...)
env = AnimFrameWrapper(env)
Parameters:
anim_id_key(str): Key for animation ID in obs. Default:'NpcAnimId'
Observation additions:
obs["boss_anim_id"]- Current boss animation IDobs["elapsed_frames"]- Number of frames since animation changed
SDFObsWrapper¶
Add Signed Distance Field (SDF) observations for arena boundary awareness. Requires an ArenaBoundary instance and player coordinates in obs (player_x, player_y from EldenGymEnv).
from eldengym import SDFObsWrapper, ArenaBoundary
boundary = ArenaBoundary.load("arena_boundary.json")
env = eldengym.make("Margit-v0", ...)
env = SDFObsWrapper(env, boundary=boundary, live_plot=True)
Parameters:
boundary(ArenaBoundary): Arena boundary instance withquery_sdf(x, y)methodlive_plot(bool): Enable live visualization of positions and SDF. Default:False
Observation additions:
obs["sdf_value"]- Signed distance to boundary (negative = inside arena)obs["sdf_normal_x"]- X component of normal vector pointing to boundaryobs["sdf_normal_y"]- Y component of normal vector pointing to boundary
OOBSafetyWrapper¶
Out-of-bounds detection and recovery via teleportation. Uses a soft/hard boundary system where the soft boundary tracks last safe position and the hard boundary triggers teleport.
from eldengym import OOBSafetyWrapper, ArenaBoundary
boundary = ArenaBoundary.load("arena_boundary.json")
env = eldengym.make("Margit-v0", ...)
env = OOBSafetyWrapper(env, boundary=boundary, soft_margin=3.0, hard_margin=0.0)
Parameters:
boundary(ArenaBoundary): Arena boundary instancesoft_margin(float): Distance inside the hard boundary for safe zone. Default:3.0hard_margin(float): Distance to extend/shrink hard boundary. Default:0.0- Positive values extend the boundary outward (more permissive)
- Negative values shrink the boundary inward (more restrictive)
Boundary thresholds:
- Hard boundary:
sdf_value < hard_margin - Soft boundary:
sdf_value < (hard_margin - soft_margin)
Info dict additions:
info["oob_detected"]- True if player crossed hard boundaryinfo["teleported"]- True if teleport was triggeredinfo["last_safe_xyz"]- Last known safe position (inside soft boundary)
Example configurations:
# Default: hard at polygon edge, soft 3 units inside
env = OOBSafetyWrapper(env, boundary, soft_margin=3.0, hard_margin=0.0)
# Hard extended 2 units out, soft still 3 units inside hard
env = OOBSafetyWrapper(env, boundary, soft_margin=3.0, hard_margin=2.0)
Combining Wrappers for Dodge Policy¶
A common pattern for training dodge policies with full wrapper stack:
from eldengym import (
ArenaBoundary,
HPRefundWrapper,
AnimFrameWrapper,
SDFObsWrapper,
OOBSafetyWrapper,
)
boundary = ArenaBoundary.load("arena_boundary.json")
env = eldengym.make("Margit-v0", host="192.168.48.1:50051")
# Apply wrappers (order matters!)
env = HPRefundWrapper(env, refund_player=True, refund_boss=False)
env = AnimFrameWrapper(env)
env = SDFObsWrapper(env, boundary=boundary, live_plot=True)
env = OOBSafetyWrapper(env, boundary=boundary, soft_margin=3.0, hard_margin=0.0)
# Now obs contains:
# - player_x, player_y, player_z, boss_x, boss_y, boss_z (from base env)
# - dist_to_boss, boss_z_relative (from base env)
# - boss_anim_id, elapsed_frames (from AnimFrameWrapper)
# - sdf_value, sdf_normal_x, sdf_normal_y (from SDFObsWrapper)
# And info contains damage tracking and OOB detection
Legacy Wrappers¶
These wrappers work with simple array observations (not Dict spaces).
FrameStack- Stack last N framesResizeFrame- Resize frames to target shapeGrayscaleFrame- Convert to grayscale
Creating Custom Wrappers¶
You can create custom wrappers using the Gymnasium wrapper API:
import gymnasium as gym
from gymnasium import Wrapper
class CustomWrapper(Wrapper):
"""Custom wrapper example."""
def __init__(self, env):
super().__init__(env)
# Your initialization
def step(self, action):
# Modify action or observation
obs, reward, terminated, truncated, info = self.env.step(action)
# Custom logic here
modified_reward = reward * 2.0
return obs, modified_reward, terminated, truncated, info
def reset(self, **kwargs):
obs, info = self.env.reset(**kwargs)
# Custom logic
return obs, info
# Use the wrapper
env = gym.make("EldenGym-v0")
env = CustomWrapper(env)
Common Wrapper Patterns¶
Frame Stacking¶
from gymnasium.wrappers import FrameStack
env = gym.make("EldenGym-v0")
env = FrameStack(env, num_stack=4) # Stack last 4 frames
Action Repeat¶
from gymnasium.wrappers import ActionRepeatWrapper
env = gym.make("EldenGym-v0", frame_skip=1) # Disable built-in skip
env = ActionRepeatWrapper(env, repeat=4) # Repeat each action 4 times
Reward Scaling¶
from gymnasium.wrappers import TransformReward
env = gym.make("EldenGym-v0")
env = TransformReward(env, lambda r: r / 100.0) # Scale rewards
Frame Resize¶
from gymnasium.wrappers import ResizeObservation
env = gym.make("EldenGym-v0")
env = ResizeObservation(env, shape=(84, 84)) # Resize to 84x84
Gray Scale¶
from gymnasium.wrappers import GrayScaleObservation
env = gym.make("EldenGym-v0")
env = GrayScaleObservation(env) # Convert to grayscale
Combining Wrappers¶
import gymnasium as gym
from gymnasium.wrappers import (
ResizeObservation,
GrayScaleObservation,
FrameStack,
)
# Create base environment
env = gym.make("EldenGym-v0", scenario_name="margit")
# Apply wrappers in order
env = GrayScaleObservation(env) # RGB -> Gray
env = ResizeObservation(env, (84, 84)) # Resize
env = FrameStack(env, num_stack=4) # Stack frames
# Now ready for training
obs, info = env.reset()
print(obs.shape) # (4, 84, 84) - 4 stacked grayscale frames