#Nested components behaviour

9 messages · Page 1 of 1 (latest)

undone hinge
#

Hi, if i create 2 components like this:

#[derive(Component)]
pub struct Direction;
#[derive(Component)]
pub struct Orientation(pub Direction);

What happens if I assign Orientation to an entity? will they also get the Direction component?
My experiments seem to suggest that no (because I added a system that acts exclusively on Direction, and my entities with Orientation don't seem to be affected), but I'd like to be sure.

Is this mentioned in the docs somewhere?

junior yew
#

I would avoid nesting components. In your system you would have Query<Direction> or Query<Orientation>. Why would you want to combine both?
But you can make Orientation to be a Bundle instead of a Component. Then it will work.

undone hinge
#

Because both symbolize the same thing: the direction of a segment (it's for a snake game).
But I have one system that applies on all Direction and rotates the sprite accordingly.
Orientation is another system that lets me know the direction of a segment, but I don't want the sprite to be rotated. I want to be apply the same kind of logic inside Orientation, that's why I'm making it contain a Direction.

It looks like it works so far (entities with Orientation component don't seem to have Direction component)

junior yew
#

I am still confused why you need the Orientation Component. You can use the Direction Component in multiple Systems & Queries.

undone hinge
#

I need orientation because:

  • my segment has a Size trait. All sizes get converted by a system to their window-scaled size
  • i implement horizontal segments as Size (width = L, height = 1)
  • i have a global system that acts on Direction and applies a rotation to the object with Direction (Left gets rotates 90 degrees to the left)
  • if I give Direction to a horizontal segment, it will actually be rotated to the left 90 degrees, so it will be displayed as vertical
  • I give it Orientation instead because i still need to know which direction the segment is going to (left to right vs right to left)
  • I could have tried to give all segments a vertical size and rely on the Direction to rotate them, the problem is that the size is scaled to the window; so for windows that are not squared this was giving me the wrong size. (to be fair I could adjust for this in my global system that converts Size to the window-size; and make it a system that takes both Size and Direction. In retrospect that would have been easier lol)

I've read those resources. The only thing that is clear for me now is that if a Bundle has multiple components of the same type, the 'later' components will override the previous components of the same type.
It's not clear to me if, in my case, an entity that is given the Orientation component also has the nested Direction component.

junior yew
#

No it will not. Components have to be added to an Entity directly or as a part of a Bundle.

#

In your case i would use Orientation as an additional/optional Component.

query: Query<Direction, With<Orientation>>
query: Query<Direction, Without<Orientation>>
slate copper
#

If you want one movement and one visual direction, why not have ```rust
pub enum Direction {...}
#[derive(Component)]
pub struct MovingDirection(pub Direction);
#[derive(Component)]
pub struct VisualDirection(pub Direction);


Or at least smth like that