Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsMultiMap with selector #153

Open
monodop opened this issue Jan 17, 2022 · 5 comments
Open

AsMultiMap with selector #153

monodop opened this issue Jan 17, 2022 · 5 comments

Comments

@monodop
Copy link
Contributor

monodop commented Jan 17, 2022

It looks to me like AsMultiMap uses some component as a key to reference entities. Is there some way that I can specify a key selector instead of using an entire component?

For example, lets say I have some component:

struct TransformComponent {
  Vector3 Position;
  Quaternion Rotation;
  Entity Parent;
}

If I wanted to get the parent of some component, that's easy to do by following Parent. But if I wanted to find all children of an entity, how could I construct a query to efficiently perform that lookup? Is this even a viable approach?

@Doraku
Copy link
Owner

Doraku commented Jan 17, 2022

You can provide AsMultiMap with a IEqualityComparer to change what the multimap use for equality. You can also make your component implements IEquatable<T> to change the default equality comparer used by the multimap. With both solution you can only take into account the Entity Parent and the multimap would effectively give you all children for a given entity.

@monodop
Copy link
Contributor Author

monodop commented Jan 17, 2022

Ah I think I saw that but didn't make the connection. In another section, I had read This is a base class to create system to update a given EntityMultiMap. If the key implemente IComparable, they keys will processed in a sorted order, or you can give your own key order. and my brain cross-wired that to assume the comparer in AsMultiMap was for sorted ordering.

@Doraku
Copy link
Owner

Doraku commented Jan 18, 2022

Ah yes this is for the AEntityMultiMapSystem so you can control in which order each keys are processed if it is needed. The overall documentation could use some more detail ^^"

@monodop
Copy link
Contributor Author

monodop commented Jan 23, 2022

Ok, so I've been playing around with this a bit. It's still a little awkward using multimap this way because TKey is the component type, not the real key type. For the example above, if I create a multimap, in order to access a component by some parent entity I'd still have to create a TransformComponent with empty position & rotation just so I can look something up by the parent. For this example, it's not terrible, but with slightly more complicated components it's sort of a pain.

Instead, it'd be nice to be able to do something like this:

var map = world.GetEntities().With<TransformComponent>().AsMultiMap(t => t.Parent);

which would mean my lookups could change

// before:
var results = map[new TransformComponent { Entity = entity, Position = Vector3.Zero, Rotation = Quaternion.Identity }];

// after:
var results = map[entity];

@Doraku
Copy link
Owner

Doraku commented Jan 25, 2022

Hum I guess it should be possible but we would need a new type EntityMap<TComponent, TKey> with a Func<TComponent, TKey> selector like in your example (the key should still be part of a component obviously). It would be cool to also allow the key to be produced from multiple components. I will try to play around with this idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants