Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section to book about Mut wrappers and the ergonomics #1275

Open
alice-i-cecile opened this issue Jun 1, 2024 · 0 comments
Open

Add section to book about Mut wrappers and the ergonomics #1275

alice-i-cecile opened this issue Jun 1, 2024 · 0 comments

Comments

@alice-i-cecile
Copy link
Member

So what you're running into is when you request a &mut Position in your system, Bevy hands you Mut<Position> instead.

Mut is a "smart pointer" used to implement change detection. When it's mutably dereferenced, Bevy says "hey, this value was probably changed!". The downside of this is that the ergonomics are sometimes surprising.

Mut implements Deref and DerefMut, so fields and methods are "forwarded" to the internal type. That's why setting the field values works. But to assign to the internal value (and not the smart pointer), you need to dereference it first. Same with doing things like adding or subtracting the values with straightforward trait impls

We should probably talk about matching as well.

I'm not sure what section of the book this belongs in, maybe a "frequently encountered problems" section? Maybe an aside in the section on queries?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant