Skip to content

Assets as Entities #11266

Open
0 of 1 issue completed
Open
0 of 1 issue completed
@andriyDev

Description

@andriyDev

What problem does this solve or what need does it fill?

The current asset system is stored in a single resource Assets. This means we need a bespoke ID system for assets, events to report changes to assets, different patterns for adding/removing assets, etc.

What solution would you like?

We can unify assets more tightly with the ECS by turning assets into entities.

  • Each asset would be an entity holding an Asset<MyAsset> component.
  • AssetIds would just be Entitys (or a wrapper around Entity).
  • Change detection for assets would just be regular component change detection.
  • Looking up an asset would just be a regular entity lookup.
  • Asset lifetimes can be held in an AssetLifetime component. Handles would refer to the same reference count and a system would check for assets with a ref count of 0 to despawn that entity.

Possible issues

  • Adding assets is no longer immediate. It requires calling apply_deferred (although at least we can get the entity ID immediately so this is not too bad of an issue).
  • There's nothing stopping users from making "weird" asset entities. For example, one entity with two different asset types. When should the asset be cleaned up? Technically this is fine since as long as you hold handles to the same AssetLifetime ref count, it doesn't matter the type. Treating the same entity as a mesh and an image would just be 2 references.
  • Users can add asset entities without the corresponding systems to manage them. Today, since pretty much all the handling must go through the Assets resource, it's fairly hard to have unmanaged assets. Does this matter? For example, you can do weird things without the right systems with events (never clearing, for example). This would just be more of that.

What alternative(s) have you considered?

Keep the system the same. The resource approach is very workable and is pretty nice to use. There are great guard rails (the types are very strong) and it's fairly hard to "break" it.

Additional context

This isn't originally my idea, I'm not sure whose it is though. I just remember hearing about this on Discord and wanted to document this.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AssetsLoad files from disk to use for things like images, models, and soundsC-FeatureA new feature, making something new possibleD-ComplexQuite challenging from either a design or technical perspective. Ask for help!S-Needs-DesignThis issue requires design work to think about how it would best be accomplishedX-ContentiousThere are nontrivial implications that should be thought through

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions