Quillraven / Fleks

Fast, lightweight, multi-platform entity component system in Kotlin
MIT License
174 stars 19 forks source link

Change "comparator" in IteratingSystem from "val" to "var" #151

Closed ludek17052 closed 3 weeks ago

ludek17052 commented 3 weeks ago

Hello, Is there any reason why "comparator" is "val"? I need Comparator with "world" parametr in System class and I guess I cannot define it in head of System class. So I need to create comparator later but comparator is "val" and I cannot set it.

I am using this comparator:

private class MyComparator(val world: World) : EntityComparator { override fun compare(o1: Entity, o2: Entity): Int = with(world) { if (o1.has(MyComponent) && o2.hasNo(MyComponent)) return -1 if (o1.hasNo(MyComponent) && o2.has(MyComponent)) return 1 return 0 } }

Quillraven commented 3 weeks ago

Since you just have a single component, can you not just use

compareEntityBy(MyComponent) and assign it to your comparator in the constructor?

The alternative is the compareEntitiy method which already runs in the context of a world.

Imo those two things should be sufficient. The only reason for var is if someone needs to change the comparator at some point in the system but at least I never had this use case and no one complained so far.

Please try the two Fleks compare methods that I mentioned above. They should be sufficient. If not then let me know 😉

edit:

Here is an example of the compareEntity

comparator = compareEntity { entityA, entityB ->
        entityB[SystemTestComponent].x.compareTo(entityA[SystemTestComponent].x)
    },
ludek17052 commented 3 weeks ago

It seems that compareEntity works fine. Thank you.