Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PooledEngine Entity Remove Component Without Freeing it Up in the Pool #250

Open
fatalcubez opened this issue Oct 29, 2016 · 2 comments
Open

Comments

@fatalcubez
Copy link

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.

@mgsx-dev
Copy link
Contributor

I faced same problem and the best solution I found so far (which work well) is to separate "definition" components from "state" components.

In your example, you would have a SpeedDefComponent containing speed (this component is never removed) and for instance a WalkingStateComponent containing nothing. Your state machine then add/remove WalkingStateComponent to reflect entity state. Your system process entities with both components.

@fatalcubez
Copy link
Author

Yeah, that's one solution, but it'll lead to a lot of duplicate components that aren't necessary. Already I'm up to around 40-50 components, and the last thing I want to do is split them apart into definition and state variants. Because I'm not in a hurry to get this fixed, I'd rather wait until the issue is addressed at the Engine level with my proposed solution.

Thanks for the help though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants