Replies: 2 comments 1 reply
-
I believe what's happening is that with_children doesn't set the parent on spawn. It firsts constructs all the children, then adds a command to push all those children at once into the children list. Basically this seems to be an optimization. Perhaps in an age with hooks and observers, we should avoid the optimization... |
Beta Was this translation helpful? Give feedback.
1 reply
-
From some nice folks in the Discord, and anyone else running into this, you can set the parent/child relationship manually with cmd.entity(parent_entity).add_child(entity);
cmd.entity(entity).set_parent(parent_entity); And the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Are there any gotchas with hooks in regard to parent/child relationships?
I have a parent entity with a
Health
component. This parent entity uses an equipment system that attaches the item as a component to a child entity, enabling multiple items of the same type to be equipped. In this case, I have multiple equippedArmor
child entities.Here, the
Armor
entities should contribute to thetotal_health
of the parent component. I was previously achieving this via customEvents
that managed the health add/subtract, as well as managing equipped child entities (asRemovedComponent
doesn't return component data AFAICT, which I needed in my case). However, this is a fragile approach; if I manually construct an entity with equipped children and bypass events, it would create an incorrect state. The extra hitpoints would not be added, and if removed, total health could even go negative, which should be impossible.For this reason, it seems like hooks are a great solution to this problem.
When an
Armor
child is added, I'm trying to increase theHealth
of the parent component, and the same in the reverse upon removal.(If you're big into spot-the-difference pictures, you might note these are exactly the same minus the plus
on_add
.)Strangely, it seems that the
on_remove
works just as expected, whereas theon_add
does not function at all (It stops executing at attempting to retrieve the parent component, so I suppose it doesn't exist?). Am I getting something wrong here? Is there some system ordering or something else?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions