question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mechanism to elegantly cleanup components

See original GitHub issue

Use case:

PhysicsComponent contains a Box2D body. Every time either a component of this class is removed from an entity or the entity itself is removed from the engine, we need to call world.destroyBody(component.body). It would be nice to have a mechanism to catch this event and act accordingly.

Options:

  • EntityListener that processes entities in the family of entities with physics component that does the cleanup on entity removed from the family.
  • Make ComponentListener a public class and have something like this:
public interface ComponentListener {
    public void componentAdded(Entity e, Component e);
    public void componentRemoved(Entity e, Component e);
}

public class Engine {
    public void addComponentListener(Class<? implements Component> componentClass, ComponentListener listener)
    public void removeComponentListener(Class<? implements Component> componentClass, ComponentListener listener);
}
  • Somehow reuse the Entity signaling system that already notifies engine of these events.
  • Component could implement Disposable and the Engine would call dispose() if it implements the interface. Nasty because it adds logic to components.

What do you guys think would be best?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mgsx-devcommented, Dec 30, 2016

I would go for solution 2 for many reasons :

  • it address both component cleanup and component initialization. With the Box2D example, you may want to create body once component added. With this solution, both body creation/destruction code would be in the same listener.
  • sometimes you need whole entity to cleanup/initialize component which is not possible with the dispose method.

For now my workaround is to use PooledEngine and implements Poolable interface on my components which works but it doesn’t work with other engine implementation.

@saltares I think this topic should be reopened.

0reactions
tdgrootcommented, Apr 4, 2016

Since nothing really happened on this issue, I’ll just drop my solution here: https://gist.github.com/Desmaster/a05d669208a05f98e8074761af5aad98

Could be way more advanced, but it currently fits my needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing cleanup components | Entities | 1.0.0-pre.15
Cleanup components are like regular components, but when you destroy an entity that contains one, Unity removes all non-cleanup components instead.
Read more >
Against Cleaning - Debates in the Digital Humanities
In reality, data cleaning is a consequential step in the research process that we often ... However, the menus also express nonscalable elements—historical ......
Read more >
Clean Code and the Art of Exception Handling - Toptal
In this article, we will discuss how to manage exceptions elegantly, ... specialized programming language constructs or computer hardware mechanisms.
Read more >
3 Ways to Clean Up Components - Michael Thiessen
If you convert parts of your component to use computed props, it magically cleans up your code. Okay, it's not exactly magic. But...
Read more >
What is the most innovative component or mechanism ... - Reddit
Watching the game rules and components evolve was a thrilling and ... their values signifying their move speed AND attack value is so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found