Improbable Icon

Bring back Entity.Contains<TComponent>()

unity
feedback
sdk
v12-0-1

#1

In my world building code, I heavily utilize the Entity.Contains<TComponent>() method. In upgrading to 12.0.1 I discovered the method was gone and while the functionality can be emulated (kind of) by changing calls to use Entity.GetComponentIds().Contains(TargetComponent.ComponentId) it’s not nearly as elegant and sadly I can’t even re-introduce (easily) the original method as an extension to Entity owing the removal of the Improbable.Worker.Internal.ComponentDatabase class. The outcome is this is that I need to manually change about 75 lines of code spread throughout my world-building code to use the new explicit call. I’m fine with doing that, although it’s kind of a pain.

The problem is that determining whether or not an Entity contains a specific Component seems like something that we’d want to do regularly when working with snapshots as a way to determine what “type” of entity we are interacting with; and thus it seems like having an elegant solution in place to make that determination is important.

It’s possible that there is something new that’s replaced the old .Contains functionaliy that I’m not aware of - if that’s the case please do let me know so I’m using the most elegant possible solution. I’ve included an example of how I’ve used this functionality in 11.x

11.1.0 Version

    private Entity CheckSpawnerEntity(Entity e)
    {
        if (e.Contains<Spawner>())
        {
            return e;
        }
        return null;
    }

12.0.1 Version

    private Entity CheckSpawnerEntity(Entity e)
    {
        if (e.GetComponentIds().Contains(Spawner.ComponentId))
        {
            return e;
        }
        return null;
    }

#3

You’re right that the Contains method was removed in 12. It looks like this breaking change was omitted from the release notes, so apologies for that!

It was removed to bring the C# API in line with the other (C++, Java) APIs, which don’t have that method, and because it has a simple equivalent: either the alternative you’ve already found, or Entity.Get<TComponent>().HasValue, which you may prefer as it’s a bit shorter.

In many cases, you want the value of the component after having checked that it’s present on the entity anyway, so we prefer just offering the Get method. For those cases where you really do only care about existence, the extra .HasValue is hopefully not too much of a burden.

Once again, apologies for the inconvenience, and if you think there’s still a strong argument for providing a separate Contains method, let us know and we can reconsider adding the method (this time to all our language APIs uniformly).

Thanks!