Jump to content
  • Advertisement
Sign in to follow this  
flyslasher

Component design and game class design

This topic is 2101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I am trying to implement a component design and I am currently unsure if I am implemented it effectively or correctly.

My classes and their children are as follow:
 

1. MainGame

2. VideoManager

3. EntityManager

4. Entity

             |---6. InputComponent  |---8. CharacterInputComponent
5. Component |
             |---7. RenderComponent |---9. CharacterRenderComponent

10. SmartList

1. So the main game class holds function for initializing, running, and terminating the game. It also has a pointer to the various managers( more will be added for like sounds or resources ) which manage more specialized parts of the game.

2. The video manager has collection of the shaders, the projection and view matrices, etc.. all for handling the drawing commands.

3. The entity manager has a list of all entities that are in the world and knows to update them if need be( drawing, input, etc. ). The list is my SmartList.

4. The entity has a list of components that are linked to it. These components give all functionality to the entity other than the basics. It operates kind of like a unique component manager.

5. This is a base class that only really defines a function( update call ) used by all components and the unique string id of all components( for use in my smart list ).

6 & 7. These are specialized components that have specific roles, one for rendering and one for input. More will inevitably be made.

8 & 9. These would be the actual components that I would link to my entities since they implement the functions more specifically.

10. It's my own template class that can store and sort through all the objects. It uses double pointers and a binary search to look up things and a merge sort to keep everything in order. Memory is automatically allocated as the list grows/shrinks in exponents of 2. Through template classes and function pointers I can use this for any value and sort by any value such is integers, strings( as for my use ), or even complicated sets of data with specialized comparison.

Now here is how the flow of a input and render call.

//Draw a single scene and starts in the main game loop
MainGame -> VideoManager -> EntityManager -> Entity -> RenderComponent


//Input detected
GLFWCallback -> EntityManager -> Entity -> InputComponent

Here is my main.cpp and how it starts/run the game + making entities and components.


#include "GameClass.h"
#include "EntityManager.h"
#include "Entity.h"
#include "components/InputComponent.h"
#include "components/RenderComponent.h"

#include "Logger.h"

using namespace std;

struct MyInputComponent : public InputComponent
{
    void KeyPressed( int iKey, int iAction )
    {
        if( iKey == GLFW_KEY_ESCAPE && iAction == GLFW_PRESS )
        {
            GameClass::GetGameInstance()->EndGame();
        }
    }
};

int main( int argc, char* argv[] )
{

    GameClass* pGame = GameClass::GetGameInstance();

    if( !pGame->InitGame( 640, 480, 640, 480, "My Game" ) )
    {
        Logger::Log( "", "Couldn't initialize game." );
        return 1;
    }

    EntityManager* pEntityManager = pGame->GetEntityManager();

    MyInputComponent* c = new MyInputComponent();
    b->SetComponentName( "InputComponent Dude" );

    Entity* a = new Entity();
    a->SetEntityName( "EntityA" );

    Entity* b = new Entity();
    b->SetEntityName( "EntityB" );

    pEntityManager->AddEntity( a );
    pEntityManager->AddEntity( b );

    b->BindComponent( c );

    pEntityManager->RemoveEntity( pEntityManager->LookUpEntity( "EntityA" ) );

    pEngine->RunEngine();

    pEngine->TerminateEngine();

    delete a;
    delete b;
    delete c;

    return 0;
}

I'm also trying to do my best to stop decoupling. For example my VideoManager shouldn't need to know about entities or components hence why the flow chart goes in such steps, but I am worried it's adding extra overhead.

This is my first time working a larger project. I would like some suggestions or notable problems with my design so I can refactor my code. I usually develop for a while, assess all issues, and rebuild it from scratch more streamlined, but my limited experience doesn't make it easy to assess everything completely.

Edited by PandaDragonThing

Share this post


Link to post
Share on other sites
Advertisement

Why do you have an InputComponent and a CharacterInputComponent?  Same with the RenderComponent.

 

You should have an InputComponent and a RenderComponent, that's it.  The InputComponent would be used by a system that has the InputComponent as a requirement (typically would also require a CharacterComponent).

 

RenderComponent would hold the graphical information for an entity and what is to be drawn;  you shouldn't have different kind of render components.

 

You need to get OOP out of your mind.  Outside of a Base Component class or base SystemClass, you probably shouldn't be using any OOP inheritance.  Do some more reading if you would like.  My Journal (in my sig) I have some articles about ECS.

 

Good luck!

Share this post


Link to post
Share on other sites

Why do you have an InputComponent and a CharacterInputComponent?  Same with the RenderComponent.

 

You should have an InputComponent and a RenderComponent, that's it.  The InputComponent would be used by a system that has the InputComponent as a requirement (typically would also require a CharacterComponent).

 

RenderComponent would hold the graphical information for an entity and what is to be drawn;  you shouldn't have different kind of render components.

 

You need to get OOP out of your mind.  Outside of a Base Component class or base SystemClass, you probably shouldn't be using any OOP inheritance.  Do some more reading if you would like.  My Journal (in my sig) I have some articles about ECS.

 

Good luck!

 

Ah thanks. That's a good point and something I kind of skipped because I wasn't sure which route was best. What would be the best way, for example, to add input for components? I think, but am not sure, that I can use a callback function and pass a function pointer into my main engine that would handle all input. That callback function would have parameters for component names and the key/mouse information. Then as the game changes states( main menu to actual game play ), I can change the pointer to the relevant function.

Share this post


Link to post
Share on other sites

 

...

 

Ah thanks. That's a good point and something I kind of skipped because I wasn't sure which route was best. What would be the best way, for example, to add input for components? I think, but am not sure, that I can use a callback function and pass a function pointer into my main engine that would handle all input. That callback function would have parameters for component names and the key/mouse information. Then as the game changes states( main menu to actual game play ), I can change the pointer to the relevant function.

 

 

No, you can accomplish this multiple ways.

 

If you're using components which only data, then as I mentioned before, the InputComponent would be used by a system that has the InputComponent as a requirement (typically would also require a CharacterComponent).  You could call it CharacterController, and in it's update function, you'd simply check what buttons are pressed from the InputComponent.

 

If your components also handle logic (not how I would approach this), then you would have the character component register for messages from the input component within the same entitiy.  Then, the Input Component would send messages every Update when buttons are being pressed.

Share this post


Link to post
Share on other sites


You need to get OOP out of your mind.

 

Is this mainly because to avoid bad design when programming the game? I actually found myself using mostly composition. 

Share this post


Link to post
Share on other sites

 


You need to get OOP out of your mind.

 

Is this mainly because to avoid bad design when programming the game? I actually found myself using mostly composition. 

 

If you're programming a game using ECS, you need to stop trying to build entities using inheritance, but rather composition.  It was in response to the OP's design of having an CharacterInputComponent inherit form an InputComponent (I'm OK with having a base Component class).

Share this post


Link to post
Share on other sites

 

 


You need to get OOP out of your mind.

 

Is this mainly because to avoid bad design when programming the game? I actually found myself using mostly composition. 

 

If you're programming a game using ECS, you need to stop trying to build entities using inheritance, but rather composition.  It was in response to the OP's design of having an CharacterInputComponent inherit form an InputComponent (I'm OK with having a base Component class).

 

Sorry. Those inherited classes weren't meant to be there. It was a short term solution so I could do some testing of functionality.

Share this post


Link to post
Share on other sites

You need to get OOP out of your mind.

Is this mainly because to avoid bad design when programming the game? I actually found myself using mostly composition.
If you're programming a game using ECS, you need to stop trying to build entities using inheritance, but rather composition.  It was in response to the OP's design of having an CharacterInputComponent inherit form an InputComponent (I'm OK with having a base Component class).

Just for the record -- if you're writing OO code, then you should already be using composition over inheritance 99% of the time. Use of inheritance should be extremely rare compared to the use of composition in OOP.
 
If you're going from OO-inheritance-based code to ECS-composition-based code, then you've actually just skipped learning traditional OOP in the middle of that transition cool.png

Share this post


Link to post
Share on other sites

You need to get OOP out of your mind.

 

That isn't necessarily true.  There is nothing wrong with applying OOP concepts to a component hierarchy.  For example, it isn't uncommon to see a ColliderComponent that is derived from a Component and then have various implementations such as BoxCollider, SphereCollider, MeshCollider, and CapsuleCollider which are derived classes of the ColliderComponent.  Shallow hierarchies are fine if they are well contained within the scope of a specific feature. 

 

I do agree that perhaps Input should be a single component.  It can be influenced by some AIController, CharacterController, or NetworkController, all of which might be a subclass of Controller.  As for the renderables, I believe it all depends on the engine.  If your lights, planes, meshes, and other render system specific exposed objects share any common ground with one another, having a base renderable class doesn't present an issue.  

 

As long as the hierarchy is kept shallow, it OOP works quite well.

Edited by crancran

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!