Entity

An entity is a game object that consists of a unique id number and is created and monitored by the Registry. Each entity can have various components. What this means is that there are pools of components and those components are owned by each of the specific entity ids.

Even though the entity is just an ID, our implementation of the entity uses an entt::entity as it's id and also has some built in functions that we can use.

By default each entity has a name and a group to aid in the identification of the entity. We can use the name or the group to filter the entities more thoroughly. See the below example:

-- Grab all the entities that have a sprite component
local view = Registry.get_entities(Sprite)

-- Create a simple table that will hold all the enemy entities
local Enemies = {}

-- Loop through the view and place all of the entities that
-- have a group name of "enemy" into the table
view:for_each(
	function (entity)
		if entity:group() == "enemy" then
			table.insert(Enemies, Entity(entity:id()))
		end
	end
)

-- Loop through the enemies and do some work
for k, v in ipairs(Enemies) do 
	local get transform = v:get_component(Transform)
	-- ...More work here with the enemies
end

Contstructors

ConstructorDescription
Entity(string name, string group)This entity ctor takes in two arguments. A string for the name and a string for the group. The name and group can be empty strings "".
Entity(uint32_t id)This entity ctor takes in the unique id of the entity and returns an entity object. The id number is a uint32.
-- Create a new entity
local entity = Entity("name", "group")

-- Create a duplicate lua entity from the previous entity's id
local entityDupe = Entity(entity:id())