News for hackers. Stuff that matters.
Re:Goodbye, shitty Car extends Vehicle object-orientation tu... by DonHopkins on Monday October 21, 2019 @07:42AM (#21311109)
Both Unity and COM use aggregation instead of implementation inheritance to compose bunches of small sub-objects together.

Unity has the notion of components (inheriting from MonoBehaviour => Behaviour => Component => Object), which can be multiply attached to GameObjects (inheriting from Object), that you can switch between with gameObject.GetComponent<ComponentClass>().

COM has the notion of multiple interfaces to the same "controlling unknown" object (analogous to GameObject), that you can switch between with QueryInterface.

https://docs.microsoft.com/en-us/windows/win32/com/aggregati...

https://flylib.com/books/en/3.357.1.89/1/

http://www.369o.com/data/books/atl/0321159624/ch06lev1sec5.h...

https://en.wikipedia.org/wiki/Object_composition#Aggregation

They're basically the same idea, but slightly different in the details and implementation.

COM is essentially C++ pure virtual interfaces implemented by one or more concrete classes using multiple inheritance (like the way Java lets you multiply inherit interfaces, but only singly inherit classes). Different COM interfaces can refer to the same shared C++ implementation object, or separate delegate objects, and "tear off" objects created on demand.

Unity's component model is implemented in C#, but isn't an essential language feature of C#, just a way of using it to implement aggregation.

Re:Goodbye, shitty Car extends Vehicle object-orientation tu... by kragen on Monday October 21, 2019 @09:32AM (#21312023)
From your point of view, are GameObjects in Unity an implementation of entities from the entity–component–system pattern, or is there a difference?
Re:Goodbye, shitty Car extends Vehicle object-orientation tu... by DonHopkins on Tuesday October 22, 2019 @08:05PM (#21329766)
There are some abstract similarities, but neither COM nor Unity GameObjects/Components are an "implementation" of that pattern, since they were both invented a long time before 2007 when Adam Martin defined ECS in "Entity Systems are the future of MMOG development":

http://t-machine.org/index.php/2007/09/03/entity-systems-are...

ECS builds on top of some of the ideas of COM and GameObject/Component, but loses much of the baggage, takes it much further in a different direction, and has a lot of other advantages, like storing the data in separate contiguous cache-friendly buffers.

https://en.wikipedia.org/wiki/Entity_component_system

>In naïve ECS implementations, every system iterates through the complete list of all entities, and selects only those entities that are needed. The total iteration cost for all systems becomes too costly if the number of systems grows or the number of entities is large. In other ECS architectures, every component type is stored in a separate list, so whatever systems operate on a given type of component are only iterating over objects they care about by default. In this common ECS architecture, the described disadvantage actually becomes a major performance advantage, by more efficiently leveraging the CPU instruction and data caches.

http://t-machine.org/index.php/2014/03/08/data-structures-fo...

>Net effect: Contiguous memory is King

>If you store your data contiguously in RAM, it’ll be fast onto the Bus, the CPU will pre-fetch it, and it’ll remain in cache long enough for the CPU(s) to use it with no extra delays.

>NB: this is independent of the programming-language you’re using. In C/C++ you can directly control the data flow, and manually optimize CPU-caching – but whatever language you use, it’ll be compiled down to something similar. Careful selection and use of data-structures will improve CPU/cache performance in almost all languages

Also:

Entity–component–system (ECS) back and forth (skypjack.github.io):

https://news.ycombinator.com/item?id=19166910

Entity Component System Framework that is CPU cache friendly:

https://stackoverflow.com/questions/23473783/entity-componen...

Designing a cache-friendly entity component system:

https://cerulean-skies.com/index.php?article=1

Unity is rolling out a new officially supported entity component system that's a totally different stack than GameObject/Component. (But they interoperate, of course.)

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

One (but not the only) fundamental difference between COM and GameObject/Component, is that you query for COM interfaces with an interface GUID, so you can only have one interface on an object for each GUID. But Unity lets you slap as many of the same component class on an object as you like. The singular Component GetComponent<ComponentClass>() method will just return the first one, but the plural Component[] gameObject.GetComponents<ComponentClass>() returns an array of all the components of the given class.

For example, it is useful to attach several different Colliders to the same GameObject (each with its own position/size/shape/etc). They all just work together as you would expect. A RaycastHit will identify which Collider was hit, as well as the sibling Transform of the GameObject it was attached to.

Another fundamental difference is that with GameObject/Component, each GameObject and component is always a completely separate independent object, but COM allows the implementation class to implement any number of different interfaces with the same instance (using C++ multiple inheritance, so each interface implementation has its own vtable pointing back to the same implementation class's different methods), or delegate to other sub-objects using "aggregation", or even make sub-objects lazily on demand with "tear-off interfaces".