#Is it possible to get the entity a component is attached to?

1 messages · Page 1 of 1 (latest)

shrewd bison
#

Hi! Thanks for reading! To be brief, I would like to know if it's possible to get a component's entity without running a query. I want to implement a method on a component that when called, deletes it and its entity after some time. Is this possible? Also, is this something I should be doing at all? I'm not too familiar with ECS or the design patterns I should and shouldn't use for it. Any tips would be greatly appreciated!

wild timber
#

I'm not sure what you mean by "not running a query" or why you would not want to, but you can just request the entity whenever you query for a component, e.g.
ship_query: Query<Entity, With<Ship>>

This gives the Entity the Ship component is attached to.

shrewd bison
#

Hey, thanks for the reply! I would like to be able to call it without having to specify something along those lines in the system definition for brevity. Do components have an attribute with information about its entity, or something similar?

whole ermine
#

No they don't, they are literally just the struct/enum you wrote

#

The derive AFAIK doesn't really do anything presently, it's just future-proofing

#

It sounds like fighting the ECS

shrewd bison
#

So It'd be better to pass the entity in through the system itself?

whole ermine
#

What actually triggers this delete?

shrewd bison
#

a function call within a system

whole ermine
#

but what circumstance?

shrewd bison
#

I want to make a crate that destroys itself when hit enough. I want to handle this through an event which would call the destroy method on the crate and destroy it.

whole ermine
#

That sounds like OOP I guess

shrewd bison
#

How would you recommend I do it in ECS?

whole ermine
#

Just make a system that destroys all crates that have 0 hp or something

#

and by destroy there I mean despawn

shrewd bison
#

oh

#

that makes a lot more sense

whole ermine
#

Components in principle are just data, systems are the behaviour

shrewd bison
#

I think that'll work. Thanks for the help!

#

Before I go, is there a proper way I should be organizing these systems? like a bundle for systems

whole ermine
#

You can make plug-ins to group systems, resources etc. together, but they don't truly encapsulate, because you can still access things from other plug-ins

#

like you could have a CameraPlugin and put all your camera spawning and moving logic in it