Skip to content

[ER] QOL binding for retrieving local player's PlayerIns#272

Open
ArloFilley wants to merge 6 commits intovswarte:mainfrom
ArloFilley:feat/er-chrins-kill
Open

[ER] QOL binding for retrieving local player's PlayerIns#272
ArloFilley wants to merge 6 commits intovswarte:mainfrom
ArloFilley:feat/er-chrins-kill

Conversation

@ArloFilley
Copy link

Adds two small quality-of-life methods to the eldenring crate, mirroring functionality in the DS3 crate.

+ PlayerIns::Instance

Adds a manual FromStatic impl for PlayerIns that returns the local main player via WorldChrMan.main_player. This creates a simple binding for accessing the main player. Useful for finding players stats, data, and position.

Mirrors previously existing implementation

impl FromStatic for PlayerIns {
fn name() -> Cow<'static, str> {
"PlayerIns".into()
}
/// Returns the singleton instance of `PlayerIns` for the main player
/// character, if it exists.
unsafe fn instance() -> InstanceResult<&'static mut Self> {
unsafe {
WorldChrMan::instance()
.and_then(|man| man.main_player.ok_or(InstanceError::NotFound))
.map(|mut ptr| ptr.as_mut())
}
}
}

+ CS::ChrIns.kill()

Adds a kill() method to ChrIns and all subclasses. Kills characters by setting current hp (ChrIns.module_container.data.hp) to 0.

Mirrors previously existing implementation

#[for_all_subclasses]
pub impl ChrInsExt for Subclass<ChrIns> {
/// Returns the character ID string for this character, of the form `c1234`.
fn id(&self) -> String {
self.superclass().modules.data.id()
}
/// Set this character's HP to zero, killing it.
fn kill(&mut self) {
self.superclass_mut().modules.data.hp = 0;
}
}

/// the caller must ensure that no other references to [`WorldChrMan`] are
/// held at the time of calling. This method mutably borrows [`WorldChrMan`]
/// internally to reach `main_player`.
unsafe fn instance() -> InstanceResult<&'static mut Self> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should implement this as a new method PlayerIns::main_player() as well as for DS3? I don't really feel like it's intuitive to have PlayerIns::instance() give you the main player thinking about it now. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feel intuitive to me that PlayerIns::instance returns the main player. I do agree however that it should be immediatly obvious to everyone what PlayerIns::instance is returning, which is not currently the case. Personally I'd like to keep the current pattern, but I'm not sure there's a way to make the return type more explicit without a wrapper around PlayerIns. Unless I'm missing something?

If there isn't a way to make the return type more obvious then a new main_player() method is the way to go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main_player() (or local_player()?) makes sense to me too. issue isn't the return type, it's that there are multiple player instances for NPCs and networked players but instance() implies it's a singleton

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main player also doesn't have a static lifetime. it's reallocated when reloading the game, so maybe this shouldn't be a FromStatic anyways

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of agree that this is stretching the definition a bit. What do you think @nex3?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw I think PlayerIns::local_player() would be more appropriate than PlayerIns::main_player()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main player also doesn't have a static lifetime. it's reallocated when reloading the game, so maybe this shouldn't be a FromStatic anyways

This is also the case for several much broader types that are currently FromStatic, like MapItemMan. We definitely want a global way to access these singleton instances, but they don't follow the requirement that the 'static lifetime last the remaining duration of the program. This is probably worth a separate discussion.

For this case, I'm fine with changing the name here and in DS3 to local_player().

@ArloFilley ArloFilley changed the title [ER] QOL bindings for ChrIns (kill) and PlayerIns (FromStatic) [ER] QOL binding for retrieving local player's PlayerIns Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants