-
Notifications
You must be signed in to change notification settings - Fork 146
Description
Hey, I was wondering if you could add in an overloaded method for the PooledEntity that takes in a boolean for whether or not the component being removed should be freed up in the pool. This might sound counter-intuitive considering that you want to free up components in a pool, but let me explain.
I have a finite state machine in my code, where each state has a list of components that describe that state. When states are changed, all the components in the old state are removed from the entity and all the components in the new state are added to the entity. However, when I remove the components from the old state, I do NOT want to free them up in the pool, because as long as the state machine is still active, the entity could switch back to that state and would require the components to be available only for the entity (and not freed up for use anywhere else).
Specific example: One component I have in my running state is a SpeedComponent set with a speed value of 8.0f. This component is stored in a list of components inside of my running state. The first time I enter the running state, everything works as expected. The player moves at the proper speed. However, once the player switches OUT of the running state, entity.remove(...) is called on the SpeedComponent, resetting its speed value to 0.0f which is the default value of the SpeedComponent. Now, this component is up for grabs in the PooledEngine ready to be used by any entity. This creates a problem because the component is not really up for grabs. It's technically still in use by the entity, even though it's not added to the entity.
In my case, all of the components get freed up when the state machine is reset, because once the state machine doesn't need to be used anymore, then it goes through all the states and all their components resetting them and putting them back in the pool.
TL;DR
Add in a boolean parameter freeUp in the PooledEntity's remove method that allows entities to remove components without resetting the component and putting it back in the pool.