Jump to content

  • Log In with Google      Sign In   
  • Create Account


Dominik2000

Member Since 12 Jul 2013
Offline Last Active Mar 17 2014 12:44 AM
-----

Topics I've Started

Input handling in ECS system

06 March 2014 - 03:17 AM

I ask me the question how the input managment is best implemented in an ecs system. Especially how to stop the program, when ESC is pressed.

 

The architecture is like this:

A core game class, which initializes the renderer, different systems (placement system, physics system, ...) and the entity manager, which only stores many maps of components, with an entity id. All systems take their components from the system manager, so every system has access to all components it needs. The input low level class will be initialized by the input system. On the update cycle of the input system  if an ESC click occurs, the input system, sets the game isRunning property to false.

 

Or should I initialize the input low level also in the game class and then message direct from the low level input to the game class? I don't think so because I break the layers in this way.

 

Are there any advantages in the second method? Or is my first approach ok?


Component based game and component communication

10 January 2014 - 05:33 AM

Hello,

 

I want to use a component based system in my game. It should have subsystems for logic, and every subsystem has a list of components which stores the data. 

 

The problem is: How to sync the components?

  • Should I have a pointer to another component (Movement subsystem needs position of entity, so its component has a pointer to the position component?)
  • Should I go with a observer pattern, which notifies all other (registered) subsystems, that the position of the entity has changed? Then I need a reference from the movement subsystem to the position subsystem, to get the actual position, because with the notify I only want that the notified subsystem adds a "to-be-processed" entry to it's queue. This should avoid a confuse processing of the entities.

Are there other (better?) approaches?

 

Thank you for your tips!


Component based architecture and messaging

09 September 2013 - 05:28 AM

Hello,

 

I have a component based architecture with subsystems, which hold the component data and implement the game logic. Now a great problem is, how to implement a messaging system.

 

I have a message dispatcher system, with template where every message type registers it's own eventhandler. On Dispatch every function in the list will be called. I don't like this, because it's hard to delete one listener and I don't get how a message queue could be implemented.

 

So I think of a design like that every subsystem has it's own queue and a high level system (e.g. world manager, ...) write to this queue if anything needs to be updated. One problem is, I think, that every high level system needs to know about the low level systems.

 

Is this a good approach? How do you think about coupling/decoupling, because coupling would be the case in my idea.

 

Dominik


Unique Id production

30 August 2013 - 04:26 AM

Hello,

 

I have found a code snippet in this forum, to generate unique ids for each type.

#ifndef UNQIUETYPEID_HPP
#define UNQIUETYPEID_HPP


unsigned int generateNextId()
{
    unsigned int nextId = 0;
    return ++nextId;
}


template< class T >
class UniqueTypeId
{
public:
    static unsigned int m_Id;
};


template< class T >
unsigned int UniqueTypeId<T>::m_Id = generateNextId();


#endif // UNQIUETYPEID_HPP

The error multiple definitions of generateNextId occurs. But even if I write UniqueNextId::generateNextId an error occurs. Why?

 


Message system problem

28 August 2013 - 03:27 AM

I have searched for a very flexible solution for a message/event system. I come to a thread in this forum and implemented the classes:

 

BaseMessage.hpp

#ifndef BASEMESSAGE_HPP
#define BASEMESSAGE_HPP


typedef enum
{
    EXIT_MESSAGE
} MessageTypes;


class BaseMessage
{
public:
    MessageTypes getType() { return m_MessageType; }
    
protected:
    BaseMessage() {}
    virtual ~BaseMessage() {}
    
    void setType(MessageTypes type)
    {
        m_MessageType = type;
    }
private:
    MessageTypes m_MessageType;
};


#endif // BASEMESSAGE_HPP

ExitMessage.hpp

#ifndef EXITMESSAGE_HPP
#define EXITMESSAGE_HPP


#include "BaseMessage.hpp"


class ExitMessage : public BaseMessage
{
public:
    ExitMessage()
    {
        setType(EXIT_MESSAGE);
    }
    
    ~ExitMessage()
    {
        
    }


};


#endif // EXITMESSAGE_HPP

 

HandlerFunctionBase.hpp

#ifndef HANDLERFUNCTIONBASE_HPP
#define HANDLERFUNCTIONBASE_HPP


#include "BaseMessage.hpp"


class HandlerFunctionBase
{
public:
    virtual ~HandlerFunctionBase() {}
    void exec(const BaseMessage& message) { call(message); }
    
private:
    virtual void call(const BaseMessage&) = 0;
};


#endif // HANDLERFUNCTIONBASE_HPP

MemberFunctionHandler.hpp

#ifndef MEMBERFUNCTIONHANDLER_HPP
#define MEMBERFUNCTIONHANDLER_HPP


#include "HandlerFunctionBase.hpp"
#include "BaseMessage.hpp"


template< class T, class MessageT >
class MemberFunctionHandler : public HandlerFunctionBase
{
public:
    typedef void (T::*MemFunc)(const MessageT&);
    MemberFunctionHandler(T *instance, MemFunc function)
    {
        m_pInstance = instance;
        m_Function = function;
    }
    
    void call(const BaseMessage &message)
    {
        (m_pInstance->*m_Function)(static_cast<const MessageT&>(message));
    }
    
private:
    T *m_pInstance;
    MemFunc m_Function;
};


#endif // MEMBERFUNCTIONHANDLER_HPP

MessageSystem.hpp

#ifndef MESSAGESYSTEM_HPP
#define MESSAGESYSTEM_HPP


#include "BaseMessage.hpp"
#include "HandlerFunctionBase.hpp"
#include "MemberFunctionHandler.hpp"


#include <functional>
#include <map>


class MessageSystem
{
public:
    MessageSystem();
    ~MessageSystem();
    
    void push(const BaseMessage& message)
    {
        Handlers::iterator itr = m_Handlers.find(0);
        
        if(itr != m_Handlers.end())
            itr->second->exec(message);
    }
    
    template< class T, class N, class MessageT >
    void subscribe(T *instance, void(N::*memFunc)(const MessageT&))
    {
        m_Handlers[0] = new MemberFunctionHandler< N, MessageT >(instance, memFunc);
    }
    
private:
    typedef std::map< int, HandlerFunctionBase* > Handlers;
    Handlers m_Handlers;
};


#endif // MESSAGESYSTEM_HPP

I thought it's not so difficult but I don't get the subscribe call. Here is my call:

void Engine::initialize()
{
    messages = new MessageSystem();
    
    messages->subscribe(this, &Engine::shutdown);
    
    RenderSystem *renderSystem = new RenderSystem();
    m_Systems.push_back(renderSystem);
    
std::vector<System*>::iterator itr;
for(itr = m_Systems.begin(); itr != m_Systems.end(); itr++)
{
(*itr)->initialize();
        std::cout << "Initialized system: " << (*itr)->getType() << std::endl;
}
}


void Engine::shutdown(ExitMessage *message)
{
    m_bShutdown = true;
}

This is only for test! This error occurs at compile time:

/home/dominik/CLProjects/Cantara/src/Engine.cpp:29:48: error: no matching function for call to ‘MessageSystem::subscribe(Engine* const, void (Engine::*)(ExitMessage*))’

But why? I don't get it.

 

Another question, is this a good approach, or should I use another?

 

Dominik

 
 
 

PARTNERS