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 WeakEntityReference #5577

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ElectroJr
Copy link
Member

This adds a new WeakEntityReference struct that is intended to be used by component data-fields to refer to entities that may or may not still exist, or in the case of WeakEntityReference<T> that might no longer have an otherwise required T component. Feel free to suggest better names for the struct, maybe it should just be abbreviated?.

Why

The current convention is that a a non-null EntityUid? (or NetEntity) should correspond to a valid non-deleted entity. Saving or loading entities that have components with invalid / deleted entity uids currently logs an error or completely fails, because the assumption is that you forgot to include an entity in the save or something has gone wrong.

Ensuring that entities delete or clear references to themselves when deleted or components are removed is a PITA (device linking, ghosts following, etc). So it'd be nice to have some kind of struct that points to an entity that may or may not still exist, that doesn't need to be manually cleaned up, and which entity serialization knows it can just ignore if the entity is deleted or not included in the save file. Though it also shouldn't get overused, its obviously slower than directly using an EntityUid.

How

The struct is is just a simple wrapper around NetEntity, and there are some new IEntityManager methods & EntitySystem proxy methods to convert back to either an EntityUid? or Entity<T>? and is meant to be used with 'is' casting (i.e., if (Resolve(weakRef) is not {} ent)). Previously it used an EntityUid instead of NetEntity, but I switched mostly just to avoid having to update the auto component state generator.

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

Successfully merging this pull request may close these issues.

1 participant