News for hackers. Stuff that matters.
Re:The Repeated Deaths of OOP (2015) by samsquire on Sunday June 06, 2021 @09:00AM (#27412852)
ECS is often implemented in an OOP language. I know of no ECS languages.

You have objects that you iterate over and apply changes to the struct or object's fields like x and y coordinates in a game tick for the "next state"

Essentially you end up with lots of global functions and very large API space that has no ordering. There's inherent ordering to global function application to produce the correct result. So it produces complex APIs.

If you move the global functions to the entity objects themselves, you end up with OOP. So I'm not really sure what the author is arguing for.

Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @12:28PM (#27414194)
ECS can also be implemented in functional or imperative languages, and it often is.

The fact that ECS can be implemented in OOP languages doesn't make it an OOP pattern, only that OOP languages have Turing-completeness. You could also write Pure-FP code or implement Prolog in an OOP language, or vice-versa.

> If you move the global functions to the entity objects themselves, you end up with OOP.

You're probably talking about moving the global functions (also called Systems) into the the Component objects. Entities in ECS are normally just integers to which components are attached to, the global functions you're talking to correspond to

This is possible for the simplest usages of ECS, however this is very limiting. One of the most important aspects of ECS, especially in modern frameworks, is the possibility of Systems (the "global functions") to operate in more than one kind of Component at a time.

For example: a collision detection System might want to operate on both "Position" and "Visibility" Components to avoid performing collision detection on invisible components.

This ability is crucial to ECS for two reasons: to have good separation of concerns, but also for performance.

With your suggestion in OOP, you would have to do that with multiple inheritance, or maybe by picking one Component to host the System and break encapsulation.

Re:The Repeated Deaths of OOP (2015) by jayd16 on Sunday June 06, 2021 @01:06PM (#27414513)
Why do critics think looping over an array is somehow incomprehensible in OO and must be replaced with inheritance or something? Most ECS implementations are in OO languages. In Unity/C#, those systems are objects those arrays holding component information are even objects!

Its really not a stretch for OOP. Its just a design pattern.

Re:The Repeated Deaths of OOP (2015) by bccdee on Sunday June 06, 2021 @02:24PM (#27415092)
Being written in an object-oriented language doesn't make code object-oriented. I could write Java using classes as (namespaces for stateless functions) XOR (structs with no methods), and while it'd still technically all be objects and methods, in practice it wouldn't embody the spirit of OOP at all.

It is in keeping with the spirit of OOP to wrap up different entities' data in separate objects and have them interact with each other through methods. Exposing all of every entity's data to the world and letting unaligned actors unilaterally mutate many entities' state with no middle man is very un-OO, even if you dress it up in classes and methods.

Re:The Repeated Deaths of OOP (2015) by jayd16 on Sunday June 06, 2021 @05:18PM (#27416426)
>Exposing all of every entity's data to the world and letting unaligned actors unilaterally mutate many entities' state

But that is NOT the pattern. Entities are ids, they have component tags that opt entities into some functionality and the systems encapsulate and track the mutable state that system controls.

If you want to start sharing ownership of mutable state, that basically comes down to an implementation detail. Unity's ECS system does NOT let "unaligned actors unilaterally mutate many entities' state with no middle man." One could perhaps implement it that way if they so choose. It's just a matter of taste because, again, the design pattern is not inherently OO or non-OO.

Re:The Repeated Deaths of OOP (2015) by bccdee on Sunday June 06, 2021 @09:14PM (#27417969)
Don't systems sometimes have to access each other's components to do work? Isn't any given component exposed to all systems, and not merely one?
Re:The Repeated Deaths of OOP (2015) by jayd16 on Sunday June 06, 2021 @10:18PM (#27418309)
Its a bit of an implementation detail I would say. If you use a single thread and explicitly order your systems then its probably possible to have fully accessible global state.

Unity, for example, encapsulates the actual component data and uses a fairly complex API around command buffers to schedule readonly and read/write commands across many threads.

Re:The Repeated Deaths of OOP (2015) by samsquire on Sunday June 06, 2021 @04:05PM (#27415848)
Functions grouped with data is inherently OOP. Even if you don't interact with a graph of objects in a class, you're still using OOP if you define methods on your class and use those methods to interact with fields.
Re:The Repeated Deaths of OOP (2015) by bccdee on Sunday June 06, 2021 @09:12PM (#27417949)
A method on a class is just syntactic sugar for a function with an argument. It takes more than just "the presence of methods" for something to be OOP.
Re:The Repeated Deaths of OOP (2015) by samsquire on Tuesday June 08, 2021 @09:05AM (#27434611)
Isn't the grouping of methods with data a core fundamental principle of OOP? Even if it is syntactic sugar, Variables, Types, Classes, functions, method calls, structs are just syntactic sugar too. It just so happens that the syntactic sugar of grouping functions with data is OOP.
Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @01:34PM (#27414727)
ECS is doing a bit more than looping over arrays, though. In modern production ECS systems there's often the case where a System acts over two or more Components is very common, and looping itself is not good enough for those cases (because you get O(n^2), O(n^3), O(n^4), etc, etc complexity), so it's nowhere near as simple as that.

Like I said, it's possible to implement anything that is computable in any Turing-complete language. The fact that there are some ECS implementations in OOP languages speaks more to the popularity of those languages, rather than their capabilities or suitability to having ECS implemented in it. Of course ECS is going to need classes in languages that don't allow structs. Of course ECS is going to need classes in languages that don't allow freestanding functions. But that's doesn't mean that everything that is implemented in an OOP language can be considered just another OOP pattern.

ECS is a more foundational way of organising code, in the same space as OOP itself.

One of OOP features is encapsulation: data and methods living together. ECS enforces the complete opposite: complete separation. Another OOP feature is inheritance. ECS removes inheritance and replaces with composition.

If one says that ECS a just another design pattern, then it's only fair to say that OOP is also just a design pattern in itself, since I could implement OOP itself in C (see Linux Kernel), Haskell or ECS if I wanted.

Re:The Repeated Deaths of OOP (2015) by jayd16 on Sunday June 06, 2021 @05:41PM (#27416597)
> System acts over two or more Components is very common, and looping itself is not good enough for those cases (because you get O(n^2), O(n^3), O(n^4), etc, etc complexity), so it's nowhere near as simple as that.

Am I missing something? Sort your data structure and quickly loop over them? Why would you ever do things at n^4 complexity?

> then it's only fair to say that OOP is also just a design pattern

Yup. It is. No reason to treat it like a religion throw it away because you can't force

Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @09:34PM (#27418075)
> Am I missing something?

Yeah. You're missing the part where I said ECS is not just looping. It's clearly also not just sorting, if you know how ECS is structured. You need either indexing, like databases, or you need to watch for changes and cache them before usage, like in SQL materialised views. If you want to keep discussing the basics of ECS I strongly suggest searching at least how it is structured, its goals, and how it works.

Re:The Repeated Deaths of OOP (2015) by jayd16 on Monday June 07, 2021 @02:59AM (#27419827)
Perhaps I should because it seems like you simply listed ways to sort.

My understanding is any ECS implementation would always spend the time bookkeeping to prevent O(n^4) or other combinatoric explosion. Systems run every frame while entities are spawned much less often, if at all between load screens.

Nested linear searches as you suggest would also blow out any locality improvements, which made the suggestion even more confusing to me.

Unity's implementation is deeply focused on keeping component data tightly packed so systems can quickly loop over them with SIMD, or even batch them across other threads. They keep entities sorted in buckets by component list, eg entities with components A, B, and AB are grouped separately (but still as arrays of the underlying component data).

They can trivially pass organized sets of arrays to any system that may need them. No linear searching is required as the arrays of separate component data are naturally matched. If a system needs to operate on A and B, the paired arrays are ready made.

...but what do they know?

Re:The Repeated Deaths of OOP (2015) by kaba0 on Sunday June 06, 2021 @01:47PM (#27414826)
> If one says that ECS a just another design pattern, then it's only fair to say that OOP is also just a design pattern in itself, since I could implement OOP itself in C (see Linux Kernel), Haskell or ECS if I wanted.

Well, it is?

What do you mean by looping itself? How is having a struct dissimilar to having an object iterated, especially with C++ essentially having barely any difference between a struct and a class?

Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @02:06PM (#27414972)
> Well, it is?

If you say so we can agree on that, yes. :)

> What do you mean by looping itself?

I mean that that ECS is not merely looping over an array like GP claimed. In production ECS, Systems normally act on more than one kind of Component, so "iterating over the array" of components is not feasible. Iterating over entities kinda kill the performance and data locality too, so you need something smart closer to "materialised SQL views" to get performance.

> How is having a struct dissimilar to having an object iterated, especially with C++ essentially having barely any difference between a struct and a class?

I never said it was. I just said that merely using a "class" doesn't make something an OOP pattern, especially if the implementor has no choice over which programming feature to use.

Re:The Repeated Deaths of OOP (2015) by kaba0 on Sunday June 06, 2021 @02:13PM (#27415016)
Thanks for the details! Though wouldn’t using arrays of structs kill the performance the same way as array of objects do (stack-allocated ones)? So, one would essentially have to have a SOA, or — the OOP abstraction: a class of arrays that enclose this hot loop of code, making the implementation opaque to a calling code.
Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @02:36PM (#27415186)
> Though wouldn’t using arrays of structs kill the performance the same way as array of objects do (stack-allocated ones)?

Not really. Components are long-lived so they're not allocated on the stack. And contiguous arrays of structs/objects is the best case for cache locality, that's the entire performance case of ECS.

Re:The Repeated Deaths of OOP (2015) by kaba0 on Sunday June 06, 2021 @03:28PM (#27415598)
A simple struct/object having in order an int, a string pointer and another int having 3 different things happening to them can in some case be worse than having all the ints next to each other, etc. ISIISI… vs III…SSS…III.. So for cache locality the latter is the best option.
Re:The Repeated Deaths of OOP (2015) by meheleventyone on Sunday June 06, 2021 @04:04PM (#27415840)
Best way to think about ECS is a generic pattern to help make things data oriented. It’s not necessarily going to be optimal though. It’s SoA over the Entity rather than individual struct members which might in a more tuned setup be more performant.

This is also where ECS as a pattern falls down (from the simplistic ideas about them being performant) because keeping components in packed arrays is only one part of making them efficient. The biggest issue is making sure your entity data is best organised for the most common data access patterns. For example you can’t actually store data in big blocks of single component type but need to consider the shapes/archetypes (common sets of components) that make up an Entity. For each archetype we can store the components packed in arrays so that they can be iterated contiguously. That way Systems can visit all of the archetypes that contain the components they care about and iterate straight through them whilst minimising cache misses. This approach obviously falls down the more heterogeneous your Entities are. It also incurs a cost when a component is added or removed from an Entity so makes some common ECS patterns more expensive. There are other approaches with different trade offs.

But this is also pretty academic for most games which don’t have the entity count or gameplay complexity to actually require this level of performance anxiety.

Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @04:43PM (#27416152)
Not really. There's no silver bullet when it comes to AoS or SoA. If you have a pair of integers, A and B, that are supposed to be always read/write together, the ABABAB of ECS beats AAA...BBB in terms of cache locality, hands down.

The advantage of ECS in this case is that it enforces data organisation to be coherent to the way data is used.

All this is mentioned on the seminal articles about Data-oriented Design, including the ones that popularised the terms AoS and SoA, and in lots of literature about modern ECS.

I suggest looking up what Entities and Components mean in ECS. It is doing all those things you're thinking of, but in a more optimal way.

Re:The Repeated Deaths of OOP (2015) by dkarl on Sunday June 06, 2021 @12:39PM (#27414292)
I agree. I think the author leans too much on the supposed incompatibility of OO style and ECS style, which they explain only by linking to the Wikipedia page for object-relational impedance mismatch. I've worked in many systems that satisfy the author's description of ECS in their treatment of the central domain objects, and they were all built in OO languages and were thoroughly OO in the rest of the codebase.

I think the author was on the right track in recognizing OO as "a set of mechanics we can cherry pick," but I really don't follow how they think an embrace of ECS style would force programmers to abandon most or all OO mechanics.

Re:The Repeated Deaths of OOP (2015) by hgs3 on Sunday June 06, 2021 @10:04AM (#27413230)
> I know of no ECS languages.

Any language with mixins and reflection can do ECS. e.g. Use mixins (components) and reflection to check which mixins an object (entity) has, then executing a code path (system) based upon that answer. Many dynamic languages, like Ruby, support this combination of features. I think the issue is many game developers work in C++ which lacks them.

There is a school of thought that design patterns are actually missing language features [1]. ECS seems like a text book case.

[1] http://wiki.c2.com/?AreDesignPatternsMissingLanguageFeatures

Re:The Repeated Deaths of OOP (2015) by anonymoushn on Sunday June 06, 2021 @12:23PM (#27414165)
ECS is commonly implemented without reflection. What are these implementations missing?
Re:The Repeated Deaths of OOP (2015) by hgs3 on Sunday June 06, 2021 @03:31PM (#27415615)
If the language in question has decent reflective capabilities, then I'd question why they don't use it. Perhaps they ran some benchmarks and found the language runtime performance lacking or maybe they are implementing the ECS pattern to the letter (e.g. using an integer to represent an "entity") as opposed to implementing ECS as a concept.
Re:The Repeated Deaths of OOP (2015) by cma on Sunday June 06, 2021 @03:32PM (#27415624)
> I know of no ECS languages.

Shouldn't Unity's DOTS/Burst subset of C# count? It gets optimization from alias analysis that you can't get with an ECS in C++ or unrestricted C#.

https://docs.unity3d.com/Packages/[email protected]/manual...

Re:The Repeated Deaths of OOP (2015) by cout on Sunday June 06, 2021 @09:17AM (#27412954)
What would an ECS language look like? Would it differ substantially from an OO language?

ECS seems to me more like a pattern than a paradigm.

Re:The Repeated Deaths of OOP (2015) by galangalalgol on Sunday June 06, 2021 @09:44AM (#27413099)
And that pattern is easy to implement in C or fortran 77 without being unidiomatic. So I don't vthing you need anything special.

Edit: to clarify, a system can just be a function, an entity just an ID and a component should be plain old data anyway, so a struct is perfect.

Re:The Repeated Deaths of OOP (2015) by ratww on Sunday June 06, 2021 @12:32PM (#27414231)
> a system can just be a function, an entity just an ID and a component should be plain old data anyway, so a struct is perfect.

Correct. This is exactly how most ECS frameworks are implemented today (except when it's implemented in languages that doesn't have those features, of course).

Re:The Repeated Deaths of OOP (2015) by kazoomonger on Sunday June 06, 2021 @11:27PM (#27418734)
I don't think ECS as such would really make a language on its own, but languages that use traits instead of inheritance would be approximately that. Both ECS and traits eschew using the inheritance-style "is-a" thinking. ECS goes for "has-a" thinking, and traits go for "acts-as" thinking. A few popular trait-based languages that would feel comfortable to someone familiar with ECS are Rust or Go.