CustomComponentLookups not working with structs?
See original GitHub issueHi,
I’m new to your framework and followed your guide from this page ( https://ecsrx.gitbook.io/project/performance/component-type-lookups ).
I created a Module for the Customlookups and added it to my application like this:
protected override void LoadModules()
{
base.LoadModules();
this.Container.LoadModule(new CustomComponentLookupsModule());
}
When adding a component, that is a struct and was registered in the dictionary, to an entity via Blueprint I get an exception:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type ‘EcsRx.Components.ComponentPool1[GF.FriendsInSpace.Core.Components.EntityIdComponent]' to type 'EcsRx.Components.IComponentPool
1[EcsRx.Components.IComponent]’.
Source=EcsRx
StackTrace:
at EcsRx.Components.Database.ComponentDatabase.GetPoolFor[T](Int32 componentTypeId)
at EcsRx.Components.Database.ComponentDatabase.Set[T](Int32 componentTypeId, Int32 allocationIndex, T component)
at EcsRx.Entities.Entity.AddComponents(IReadOnlyList`1 components)
at EcsRx.Extensions.IEntityExtensions.AddComponents(IEntity entity, IComponent[] components)
I can work around this when replacing the struct with a class. But it would be great if you could make it work again or if I made a mistake maybe you can offer a solution 😃
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Right having a look over and I think there could do with being a few changes, as currently there is only one method to add a struct component, and you dont pass in the component you pass in the generic and the id for it, then it provides you the default component. There is a workaround where with structs you can actually just call
GetComponent<T>(tyoeId)
and get back an empty/current component for that entity.So the other simpler methods for adding components are all for class based ones.
In hindsight this seems rubbish developer experience, so I think this could do with some changes, and hopefully it shouldn’t be a case of moving mountains, but will need to look into it some more to see what the ramifications are for it, as struct and classes have slightly different flows through the underlying parts of the system due to the way they are handled.
Anyway to get you up and running if you change your code to this:
It should do what you expect, however as I say this probably needs better documentation and potentially an improvement to the way you can create and add components.
I started a conv on the discord server about this topic as it touches on a few problem areas and I dont think I can easily wrangle the existing API to be able to let classes/structs mix nicely in the same method.
There was a lot of chat a while ago about moving entities to be structs, and some research was done into it, but it may be that rather than spending more time on allowing variations of adding classes/structs together look more into the entity changes then revisit this topic, as then the storage/interaction will be in 2 separate classes (well class and struct).
So feel free to drop in if you have any strong feelings on stuff.
Also worth pointing out, originally you never really bulk added components you did them one at a time, but the performance hit on group changes was quite large, so we added the
AddComponents
as a sort of performance improvement there as you could batch together multiple additions. So one thing that was originally going to be possible (potentially) with the newer entity separation was better batching/transaction-like handling, so you could just do lots of operations then just “commit” them (which would basically remove these pre-batched methods).