Composition?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Composition?

Post by GroundUpEngine »

So I've changed my entity system in my engine, but it is still inheritance based and I feel it could get bloated in the future. I want to aggregate my inheritance stuff into components, since most of the code does the job ;)

My current method:
  • I want to make a new object type e.g. Plane
    I have to make a plane class which would inherit the entity class
    Then add all the necessary stuff
    Then my scene manager can make a new plane
Structure:
Entity
- Plane

But with composition:
  • I want to make a new object type e.g. Plane
    My scene manager can make a new plane, by adding a new entity
    Then add all the necessary components e.g. render, physics
Structure:
Entity
Component
- Render component
- Physics component
- Script component


Am I going about this the right way?
User avatar
szdarkhack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Fri May 08, 2009 2:31 am

Re: Composition?

Post by szdarkhack »

Pretty much, yes. The general idea is that if you want to make, say, a plane, you just make a new entity, add the components necessary and add it to the scene. Of course there is nothing stopping you from automating the creation, although since the main point of using a component system is flexibility, i would advice against hardcoding it. Perhaps, for example, you can read what components to attach from a file describing your objects, or something similar.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Composition?

Post by Falco Girgis »

Yeah, that's exactly right.

There's nothing stopping you from having a factory "assemble" a specific type of entity by automatically attaching components for you. That's essentially how saving/loading works. It lends itself extremely well to serialization/deserialization.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Composition?

Post by GroundUpEngine »

szdarkhack wrote:Pretty much, yes. The general idea is that if you want to make, say, a plane, you just make a new entity, add the components necessary and add it to the scene. Of course there is nothing stopping you from automating the creation, although since the main point of using a component system is flexibility, i would advice against hardcoding it. Perhaps, for example, you can read what components to attach from a file describing your objects, or something similar.
Ok, good idea but I quite like the automated approach hehe. But I agree, things would be even more flexible with scripting the types.
GyroVorbis wrote:Yeah, that's exactly right.

There's nothing stopping you from having a factory "assemble" a specific type of entity by automatically attaching components for you. That's essentially how saving/loading works. It lends itself extremely well to serialization/deserialization.
Sweet, I choose that way because my entity types are pretty straight forward and I kept things simple.
Speaking of serialisation, I was considering a PAK file format for all the necessary resources needed. But I wasn't sure how it is done; reading in files and then concenate them into one file, or actually loading them into the engine (e.g. Texture("tex.jpg")) and then serialising the state of the object into a binary file?
Post Reply