Skip to content

Systems as entities #16618

Open
Open
@andriyDev

Description

@andriyDev

Background

Today, bevy_ecs can roughly be split into two parts:

  1. Data Storage and Access: This includes World, entities, components, resources, archetypes and querying.
  2. Systems and Schedules: This is all the infrastructure for creating systems and executing them.

The data storage part is very flexible and powerful. It has facilities for storing arbitrary data on objects, composing data on objects, reacting to changes in data, and so on.

On the other hand, schedules are a hand crafted storage solution for systems which includes all manner of metadata: system name, before/after relations, system sets it belongs to, etc.

As a result, we have to build a lot of APIs. Schedules today can't have systems be removed (#279, wow that's an old issue). We'd have to come up with an ID scheme for systems, provide a function to remove systems, and also make sure the schedule graph reacts appropriately to the change.

Proposal: Systems-as-entities

(queue anything-as-entities meme)

Instead of building more stuff on top of the system storage, we can instead rewrite it to use the data storage and access part! Now we don't need an ID scheme for systems: it's just an Entity. Now we don't need a function to remove systems: that's just despawning an entity. As for having the schedule graphs react to changes, we have several tools to handle this. Change detection, hooks, and observers could be used.

As part of this, we will likely want schedules and system sets to also be entities. Systems can then have a system set or schedule component that matches the particular type.

Issues that help this effort

Immutable Components (#16372)

Today, we likely need to use change detection to keep things in sync. With immutable components, we can just use hooks to sync any schedule data structures.

🌈 Relations (#3742)

Today, we either need to store before/after relationships as one component for all a system's constraints, or we need to spawn an entity to represent that constraint. Either way, these are both cumbersome to manage. With relations, we can use them to represent all the before/after constraints as relations. Finding all edges is very easy then.

Similarly, schedules and system sets could use relations to define which systems are in the schedule or system set.

Risks

  • This would be replacing a ton of our schedule code. New code can be buggy!
  • Users may accidentally mutate the system entities without realizing it by either deleting their entities or mutating their components.
    • This could be fixed with a separate "schedule world", which will be a world that just holds the systems, schedules, and system sets.
  • Mutability may be hard to juggle. I'm not certain of this problem - this may be an implementation detail. The schedule will likely still hold a schedule graph representation of some kind which we can probably just take out of the corresponding component and put it back once we're done.

Prior Art

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-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 accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions