Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How can I separate data from presentation? #19088

Unanswered
robbie01 asked this question in Q&A
Discussion options

In web development, it's typical to have a "data model" drive some component which handles presentation, as in the classic MVC pattern, the Elm approach, or from a Rust perspective, something to the effect of egui. However, I'm having trouble finding a way to accomplish this in an ECS system.

Let's say I have a bookshelf with books on it. These books have a title, a genre, and a short name. The genre determines the color of the book on the shelf, and the short name determines what's written on the spine. Logically, this can be modeled out with ECS; a root entity has a bespoke Bookshelf component which handles positioning, then each book child of the aforementioned entity has a Book (determines position on shelf), Name (title), Mesh2d, MeshMaterial2d (genre), and a child entity with a Text2d (short name).

However, using this as the model of data tightly couples the model to presentation. What if I want to make an editor for the books? I could query Books for their Name and MeshMaterial2d, which is a bit of a leaky abstraction, but not as bad as having to query these books for a Text2d child! I have to assume the presentational structure of the bookshelf in order to access its data model. Surely there has to be a way I can just back the bookshelf with a Vec<BookMetadata> and use that as the source of truth, right?

You must be logged in to vote

Replies: 1 comment

Comment options

What you proposed would be a typical ECS way to organize this data. It only is a leaky abstraction if you consider it to be, from a non-ECS standpoint. Having an editor do queries seems perfectly reasonable when using an ECS as the data model.

I have to assume the presentational structure of the bookshelf in order to access its data model

This is not just presentation. ECS is a data layout. Each component is being stored in buffers with other components of the same archetype, and more .

With Vec<BookMetadata> you would just be using another, more manual, data layout (which is also not a bad thing if you really need it, it can provide better serialization/de-serialization to/from some disk storage for example). But with just a Bookshelf component storing Vec<BookMetadata> you would lose on all the ECS niceties about the individual components of your books (systems, change detection, ...).
If you really need this data-layout you could also consider having the data duplicated between this Vec and the components of the books. but that's additional "bookkeeping" (oh no) to maintain them in sync.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.