Advertisement Jump to content
Sign in to follow this  
Krankles

Trying to Convert Simple Java ECS to C++

This topic is 1785 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

Hi, I'm reading up on ECS on t-machine and other places, but now I'm trying to implement my own simple ECS. I'm stuck on trying to convert https://github.com/adamgit/Entity-System-RDBMS-Inspired-Java from Java to C++. In the EntityManager, he has a variable called componentStores which is defined as 

HashMap<Class, HashMap<Integer, ? extends Component>> componentStores;

However, I don't know the equivalent of this in C++.

 

So far, I've been able to create and generate entity ids, but I don't know how to actually add the component to the entity in a map. In the hashmap above, what types of data is it containing? Also, the second hashmap inside the hashmap, is the "? extends Component" supposed to be a pointer to a given Component? For example, I made a PositionComponent, would the hashmap contain a pointer to the PositionComponent? And, what is the point of the first data type "Class?" Sorry if this doesn't make sense, I don't fully understand what's happening, but the big question is how can you convert the hashmap into C++?

 

My entity header file looks like this:

#ifndef ENTITY_MANAGER_H
#define ENTITY_MANAGER_H

#include <map>
#include <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>

class EntityManager
{
public:
        EntityManager();
        ~EntityManager();

        uint32_t create_entity();
        void kill_entity(uint32_t id);

private:
        std::vector<uint32_t> entity_list;
        uint32_t assignable_entity_id;

        uint32_t generate_entity_id();
};

#endif

and this is my EntityManager source file

#include "EntityManager.h"

EntityManager::EntityManager()
: assignable_entity_id(1)
{

};

EntityManager::~EntityManager()
{

};

uint32_t EntityManager::create_entity()
{
        uint32_t id = generate_entity_id();

        if (id < 1) {
                // throw error
                return 0;
        } else {
                entity_list.push_back(id);
                return id;
        }
}

uint32_t EntityManager::generate_entity_id()
{
        if (assignable_entity_id < UINT32_MAX) {
                std::cout << "Generated ID: " << assignable_entity_id << std::endl;
                return assignable_entity_id++;
        } else {
                for (unsigned int i = 1; i < UINT32_MAX; i++) {
                        std::vector<uint32_t>::iterator it = std::find(entity_list.begin(), entity_list.end(), i);
                        if (*it != i) {
                                std::cout << "Generated ID: " << i << std::endl;
                                return i;
                        }
                }

                // throw error
                return 0;
        }
}

void EntityManager::kill_entity(uint32_t id)
{
        std::vector<uint32_t>::iterator it = entity_list.begin();
        for (; it != entity_list.end(); it++) {
                if (*it == id) {
                        std::cout << "Killing Entity: " << *it << std::endl;
                        entity_list.erase(it);
                        return;
                }
        }
} 

Any help would be greatly appreciated, thanks.

Edited by Krankles

Share this post


Link to post
Share on other sites
Advertisement
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!