Question

I have an Entity class defined by a lib, and Registry which is class that manages Entities registered to it with a map.

What i'm trying to do:

//Registry Signature
void Registry::add(Entity* entity);


//my function (IGameEntity subclasses Entity)
void GameLogic::addEntity(shared_ptr<IGameEntity> entity, GameEntityId id) {
 GameEntityId entityId = entity->getId();
 gameEntities.push_back(entity);
 Framework::Registry::instance()->add(entity); //<-- this doesn't work
}

I'm trying to figure how to handle this, since i'm keeping shared_ptr's and then i'm gonna have to pass a pointer.

Another thing is that if the registry gets destroyed by some reason it will call delete on all entities remaining in the map.

If i pass entity has weak_ptr<Entity>(entity) it works but i'm not fully grasping whats going on in terms of what will happen if Registry calls delete on a weak_ptr.

And if i pass entity has entity.get() what will happen when the reference count reaches zero or the registry tries to delete it.

Was it helpful?

Solution

You have two incompatible ownership schemes.

You need to either change one of them, or clone the object that should be transferred from one scheme to the other.

There is a trick for releasing a shared_ptr when there is only one reference to the object. But you don't want to do that. Even if you know enough about the insides of the library to do it.

So, as a practical solution, change the registry thing.

Make it take a shared_ptr.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top