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
Uh oh!
There was an error while loading. Please reload this page.
-
Are there any gotchas with hooks in regard to parent/child relationships?
I have a parent entity with a
Healthcomponent. 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 equippedArmorchild entities.Here, the
Armorentities should contribute to thetotal_healthof the parent component. I was previously achieving this via customEventsthat managed the health add/subtract, as well as managing equipped child entities (asRemovedComponentdoesn'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
Armorchild is added, I'm trying to increase theHealthof 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_removeworks just as expected, whereas theon_adddoes 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