dominion-dev / dominion-ecs-java

Insanely fast ECS (Entity Component System) for Java
https://dominion-dev.github.io
MIT License
288 stars 17 forks source link

java.lang.ClassCastException #195

Closed jwdeveloper closed 3 weeks ago

jwdeveloper commented 1 month ago

When I query for the MoveGridComponent, HideComponent is returned. They only hint I can give you is that all components I use implement Component class, so maybe there is some issue with type Hash calculation?

public void executeTest(Dominion dominion) {

        var components = dominion.findEntitiesWith(MoveGridComponent.class);
        offset.add(0.1f, 0, 0);
        for (var comp : components) {
            var grid = comp.comp();
            var yy = random.noise(
                    (grid.getInitLocation().x + offset.x) * 0.1f,
                    (grid.getInitLocation().z + offset.z) * 0.1f);
            yy *= maxHeight;
            grid.transform().position().y = yy;
        }
    }
java.lang.reflect.InvocationTargetException
    at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at io.github.jwdeveloper.engine.systems.SystemsLoader.lambda$bindSystemMethod$1(SystemsLoader.java:73)
    at io.github.jwdeveloper.ce.core.events.DefaultEventListener.publish(DefaultEventListener.java:80)
    at io.github.jwdeveloper.ce.core.GameObjectEngine.handleFrame(GameObjectEngine.java:236)
    at io.github.jwdeveloper.ce.core.GameObjectEngine.loopCycle(GameObjectEngine.java:207)
    at io.github.jwdeveloper.ce.core.GameObjectEngine.start(GameObjectEngine.java:148)
    at io.github.jwdeveloper.engine.ComponentEngineImpl.start(ComponentEngineImpl.java:32)
    at io.github.jwdeveloper.ce.core.GameObjectEngine.lambda$startAsync$4(GameObjectEngine.java:155)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
Caused by: java.lang.ClassCastException: class org.example.components.HideComponent cannot be cast to class org.example.components.MoveGridComponent (org.example.components.HideComponent and org.example.components.MoveGridComponent are in unnamed module of loader 'app')
    at org.example.ExampleSystem.executeTest(ExampleSystem.java:29)
    ... 17 more
jwdeveloper commented 1 month ago

I found out that this bug only happens when I query by One component. Query with 2 components or more always works

jwdeveloper commented 1 month ago

I think I found what was causing the Issue. Every time I was creating new entity, its components was provided in random order, since I was using HashSet to store components. Changing HashSet to ArrayList fixed issue, and now components are provided in same order

It was looked like this:

1: MoveComponent, JumpComponent, RenderComponent
2: JumpComponent, MoveComonent, RenderComponent
3: RenderComponent, JumpComponent, MoveComponent
... and so on

And now:

1: MoveComponent, JumpComponent, RenderComponent
2: MoveComponent, JumpComponent, RenderComponent
3: MoveComponent, JumpComponent, RenderComponent
...

I'm not familiar with Dominion architecture, however I still consider this as a Bug, since I should be able to insert components in any order

ps: This library is amazing, both code, documentation and performance :D