Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofWeakEntityReference<T>
that might no longer have an otherwise requiredT
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 anEntityUid?
orEntity<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.