AI generated
AI assisted

This section will guide you through using the Dominique library's Entity-Component-System (ECS) framework with a sample code snippet. We will demonstrate how to create and manage entities and components within a scene.


Basic ECS Usage

Below is an example of how to use the Dominique Engine's ECS system to create and manage entities and components.

// main.cpp

#include <iostream>
#include "dengine/ecs/ecs.hpp"
#include "dengine/spdlog_helper.h"

struct TransformComponent {
    float position = 0.0f;
    float rotation = 0.0f;
};

struct TagIdComponent {
    int id;
};

int main() {
    auto& logger = getMultiSinkLogger();

    logger.info("TransformComponent ID: {}", de::ecs::Scene::GetId<TransformComponent>());
    logger.info("TagIdComponent ID: {}", de::ecs::Scene::GetId<TagIdComponent>());

    de::ecs::Scene scene;

    de::ecs::EntityID newEnt = scene.NewEntity();
    scene.Assign<TransformComponent>(newEnt);

    auto* t = scene.Get<TransformComponent>(newEnt);
    logger.info("Initial TransformComponent position: {}", t->position);

    t->position = 3.0f;
    logger.info("Updated TransformComponent position: {}", t->position);

    return 0;
}

Step-by-Step Explanation


1. Include the Required Headers

#include "dengine/ecs/ecs.hpp"
#include "dengine/spdlog_helper.h"
  • dengine/ecs/ecs.hpp: Provides ECS system functionalities.
  • dengine/spdlog_helper.h: Enables logging for easier debugging.

2. Define Component Structures

struct TransformComponent {
    float position = 0.0f;
    float rotation = 0.0f;
};

struct TagIdComponent {
    int id;
};
  • TransformComponent: Defines a component with position and rotation properties.
  • TagIdComponent: Defines a component with an id property.

3. Initialize a Logger

auto& logger = getMultiSinkLogger();
  • Create a logger that supports multiple output sinks.

4. Log Component IDs

logger.info("TransformComponent ID: {}", de::ecs::Scene::GetId<TransformComponent>());
logger.info("TagIdComponent ID: {}", de::ecs::Scene::GetId<TagIdComponent>());
  • Logs unique IDs for each component type.

5. Create a Scene and an Entity

de::ecs::Scene scene;
de::ecs::EntityID newEnt = scene.NewEntity();
  • Initializes a new Scene instance and creates an entity.

6. Assign a Component to the Entity

scene.Assign<TransformComponent>(newEnt);
  • Assigns a TransformComponent to the created entity.

7. Retrieve and Modify the Component

auto* t = scene.Get<TransformComponent>(newEnt);
logger.info("Initial TransformComponent position: {}", t->position);

t->position = 3.0f;
logger.info("Updated TransformComponent position: {}", t->position);
  • Retrieves the component from the entity, logs its initial state, modifies its position, and logs the updated state.

Advanced Usage

For more advanced features such as system integration, dynamic entity management, and real-time component updates, refer to the Dominique Engine Documentation.


Troubleshooting

If you encounter issues, try the following:

  1. Check Component Definitions: Ensure all components are correctly defined and properly included.
  2. Log Information: Use logging functions to track component states and entity creation.
  3. Verify Dependencies: Ensure that necessary libraries are linked correctly in your build system.

Further Assistance

For additional help, visit the Dominique GitHub Repository. You can also open an issue for community support or contribute to the project.


Thank you for using Dominique Engine! We hope this guide helps you build powerful ECS-based applications. If you have any feedback or suggestions, feel free to contact us.