Skip to content

Entities Components Systems

Matthew Evans edited this page Nov 9, 2021 · 2 revisions

WTEngine stores game objects in the entity_manager class.

Entities:

Entities are any and all in-game objects, such as the player, background, enemies, even the text showing the score. When creating a new entity the engine will assign a unique ID number and unique name. If the engine is unable to create an entity the error code WTE_ENTITY_ERROR is returned. You can then change the name of the entity if desired. When setting the name the engine will verify it does not already exist.

Creating an entity:

entity_id e_id = world.new_entity();

Setting an entity name:

world.set_name(e_id, "some_unique_name");

Get entity name by ID:

std::string player_name = world.get_name(plr_id);

Get entity ID by name:

entity_id e_id = world.get_id("some_entity");

Components:

Components are the data objects that make up each entity. Examples would be the location on the screen, sprite data, hitpoints, etc. After creating an entity you can assign components to it using its Entity ID. The engine has seperate functions for reading and setting component data. You can also create your own components by extending the base component object.

Adding a component:

mgr::world::add_component<cmp::team>(e_id, 0);

Setting a component's data:

mgr::world::set_component<cmp::overlay>(e_id)->hide();

Reading a component's data:

if(mgr::world::get_component<health>(plr_id)->hp <= 0)

Systems:

Systems are functions that are processed each iteration when the game is running. These are designed to obtain all components of a certain type and process them. For example, the movement system will look for all entities with a velocity component, verify they have a location component, and update their posistion based on the velocity value. You can also create your own systems by extending the base system object. You then load all systems into the system_manager in order they will be processed.

Example:

void wte_demo::load_systems(void) {
    mgr::systems::add(std::make_unique<sys::movement>());
    mgr::systems::add(std::make_unique<sys::colision>());
    mgr::systems::add(std::make_unique<sys::logic>());
    mgr::systems::add(std::make_unique<sys::animate>());
}

The entity_manager has members for reading or setting all components of a type, see its documentation for more information.

Clone this wiki locally