#Entity to EntityRef

39 messages · Page 1 of 1 (latest)

misty night
#

Hey yall! I'm still working on understanding how the whole ECS works in practice.
Currently I am trying to write a SCeneHook closure that will add a collider to all my objects that contain a Cube, using the contained cube's Aabb's half_extents.

However, I am struggling to figure out how to get access to the Cube inside the Entity I am looking at since I don't know how to convert an Entity to an EntityRef. Or maybe it's the wrong approach altogether?
Any help is appreciated!

here's the code I currently have:


        hook: SceneHook::new(|entity, cmds| {
            entity
                .get::<Children>()
                .and_then(|c| c.iter().filter(|e| EntityRef::from(e) ??? ));
        }
burnt valley
#

You probably need access to &mut World in your hook

misty night
#

mhh I see

#

and then?

burnt valley
misty night
#

Ah I see

#

But can you only ever resolve entities in exclusive systems then?

burnt valley
#

you can use Query::get, but that only allows access to the components defined in the query

#

you also might be able to use &World if you don't need mutation

misty night
#

So what is the typical workflow for accessing a childrens Components?

#

Or is there none?

burnt valley
#

let cube = query.get(entity); would be roughly what I use most of the time. But that doesn't work for an abitrary callback, which is what I'm assuming you're doing

misty night
#

Ahh I see

#

yeah you're right I use the callback from the SceneHook

burnt valley
#

oh, this is the lib bevy_scene_hook

misty night
#

yes exactly

#

sorry I should have clarified that in the question i suppose

burnt valley
#

yeah, probably not possible to get data from the children

misty night
#

Well I'm trying it using the world approach you described before currently

burnt valley
#

I don't think you'll be able to get access to the world in the hook

misty night
#

It proves to be difficult xD

burnt valley
#

bevy_scene_hook probably only works to add some data to the root entity of a scene and not to actually modify the scene

misty night
#

Well it does loop through every entity contained in the scene

burnt valley
misty night
#

Oh!

#

How-

#

did you learn that so fast?

#

Like how does one look at the bevy docs in general to find things like that out?

burnt valley
#

I've been involved in bevy for awhile. just forget a lot of stuff in the moment.

If I was figuring it out from scratch it'd just be a lot of reading the docs and hoping to find what I need. Probably starting with EntityRef and looking at what I can get from that, and then looking at the docs for the returned types and hopefully get lucky and notice that world has get_entity

misty night
#

Haha okay I see

#

is there a place where noobs can ask their idiotic questions to get a quick answer from people that know bevy? Or do you create a thread here every time?

burnt valley
#

Best to just create a thread usually. If it's something that is obvious you can get answers on general sometimes. There's the topic specific channels too. Like it's easier to get help in #rendering for tending related stuff

misty night
#

I see, well thanks and I will try to implement what I'm looking for with the new earned knowledge, otherwise report back here ^^

#

Oh while I'm at it, would it make sense to keep a reference to the entity.world() at the start of the closure or is it okay to call entity.world() for every possible child?

burnt valley
#

I think they'd both work. I'd probably do the first one, but there's a good chance they'll compile to the same code

misty night
#

You'd go for the first one for better readability or what would be the motive?

burnt valley
#

I'm old and I was taught to help the compiler whenever possible, so I'm just more comfortable doing that. The real advice I'd give is to optimise for readability and only do these types of micro pref optimizations with benching.

misty night
#

Haha okay, I thought in the sense of its easier to see if I refer to "world" instead of "entity.world()" all the time

#

but its no big difference either way i suppose