question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CustomComponentLookups not working with structs?

See original GitHub issue

Hi,

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.IComponentPool1[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:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
grofitcommented, Apr 24, 2020

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:

        public void Apply(IEntity entity)
        {
            var gameTimeComponent = new GameTimeComponent  {CurrentTime = this._currentGameTime};
            entity.AddComponents(gameTimeComponent);
            
            ref var gameEntityIdComponent = ref entity.AddComponent<EntityIdComponent>(ComponentLookupTypes.EntityIdComponentId);
            gameEntityIdComponent.EntityId = _currentGameEntity;
        }

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.

0reactions
grofitcommented, Apr 24, 2020

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Lookup Not Showing In Community
On the form I have a lookup field to a custom object that doesn't want to work. Are lookups to custom objects suppose...
Read more >
Simple lookup on key for structs
I will have only 8 types of Food object/structs, but unlimited possibilities to search. Ideally, char name[20], would not be needed in my...
Read more >
How to create a struct for the Unity Job System (with Burst) ...
This should be pretty basic, I have created some structs to feed my native arrays which are then passed to a IJobParallelFor and...
Read more >
Struct ComponentLookup<T> | Entities | 1.0.14
This operation does not cause a structural change (even if it occurs on a worker thread), or affect the value of the component....
Read more >
Improvements in custom component handling
Custom components have many interesting enhancements in Tekla Structures 2021. Data lookup file fVF improvements. It is no longer necessary to have a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found