Page 1 of 1

Question

Posted: December 7th, 2017, 1:45 am
by MrGodin
I think i know the relationship between smart pointers and scope. If a container (vector) holds a smart pointer then it is responsible for freeing it ? .. correct ? as in this example.

Code: Select all

void EntityManager::Refresh()
{
	
	for (auto i(0u); i < maxGroups; i++)
	{
		auto& v(groups[i]);
		v.erase(std::remove_if(std::begin(v),std::end(v),
			[i](Entity* ent)
		{
			return !ent->Active() || !ent->HasGroup(i);
		}
		), std::end(v));
	}
	
	entities.erase(std::remove_if(std::begin(entities), std::end(entities),
		[](const std::unique_ptr<Entity>& inEnt)
	{
		return !inEnt->Active();
	}), std::end(entities));
}

Re: Question

Posted: December 7th, 2017, 2:04 am
by MrGodin
I love my managers lol
groups hold entities with a particular component
ent->Add<Collider>();// add a collider component to the entity
ent->AddGroup(groupCollider);// add the entity to the group that holds colliders
i made the entity class have a static reference to EntityManager ie:: Entity::SetManager(m_EntityMgr.get()); now all entities have a reference err, pointer to the entity manager so ent->AddGroup(groupCollider) adds the entity to the group which the manager holds

Re: Question

Posted: December 8th, 2017, 12:23 am
by albinopapa
Yes, if you have a vector<unique_ptr<Entity>> and you erease an element from the vector, the unique_ptr at that index will have it's destructor called and the memory is freed for you, you do not need to call release or reset() for each removed element. Basically, what you have is fine.