[swift-users] Checking/getting custom objects from a collection

David Hart david at hartbit.com
Fri Apr 8 01:47:33 CDT 2016

Unity, the game engine, uses a component system heavily. It uses C# and retrieves components using generic functions. And no, it doesn't force having a single instance of each component class:


Crashes the compiler, but that's how I would implement it.

Sent from my iPad
> On 08 Apr 2016, at 02:29, Jens Alfke via swift-users <swift-users at swift.org> wrote:
> I’m not familiar with this design pattern, but it looks like Entity would just contain an array of Component. That makes add() and remove() straightforward.
> I’m not sure about your get() method. It sounds as though the implication is that an entity could only contain a single component of a given class, but that sounds awfully limiting. (If Limb is a Component, then a Spider entity needs eight of them…) I also don’t believe that Swift is dynamic enough to be able to interrogate the classes of components that way at runtime. You could implement that at a higher level by adding a `type` property to ComponentType, that returns some component-type enumeration or maybe a string, and checking component types via that property.
> —Jens
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

More information about the swift-users mailing list