mreinstein / ecs

data oriented, functional entity component system
MIT License
86 stars 8 forks source link

System priorities #23

Closed pavelvasev closed 9 months ago

pavelvasev commented 3 years ago

Hi Mike!

In my case there is a need to specify priorities for systems. I understand that there are onFixedUpdate, onPreUpdate,onUpdate,onPostUpdate order, thus priorities (4 ones) are already presented. But I think it will be more usable to use digital priorities. For example:

ECS.addSystem( world, system1, { priority: 10} )
ECS.addSystem( world, system2, { priority: 90} )

Actually I need all these just to catch added and removed "events" carefully. Probably there are other solutions.

One of solution I see is to add a system in a correct order. However in my case it is not an option, because I would like to add systems dynamically via GUI.

One of caveat with approach of specifiyng priority in addSystem is an inability to change that priority dinamically. Aside that it breaks your super-elegant optionless design. Probably better way will be something like this:

var h1 = ECS.addSystem( world, system1 )
var h2 = ECS.addSystem( world, system2 )
...
ECS.setSystemPriority( world, h1, 10 );
ECS.setSystemPriority( world,h2, 90 );
...
ECS.setSystemPriority( world,h1, 25 );
...
ECS.removeSystem( world,h1 ); // this is a subject to another talk.. ;-)

What do you think about that?

mreinstein commented 3 years ago

I've definitely thought about adding a priority system but I haven't given it much careful thought yet.. Something like this could work.

If you need to re-order the systems right now, you can adjust the values in world.systems:

const world = ECS.createWorld()

// swap order of systems 0 and 2
const tmp = world.systems[0]
world.systems[0] = world.systems[2]
world.systems[2] = tmp
pavelvasev commented 3 years ago

Mike thank you for explaining the work-around!

mreinstein commented 2 years ago

Still open to supporting this if you'd like to PR. :)

mreinstein commented 9 months ago

I'm closing this for now because I think one can achieve this by ordering how systems are added to the ECS instance.

Today's behavior, which is easy-ish to reason about:

// initial setup
ECS.addSystem(world, system1)
ECS.addSystem(world, system2)

// later, in the sim loop...
ECS.update(world, dt)  // invocation order is system1.onUpdate then system2.onUpdate

That said I'm open to considering it if anyone comes along and feels they must have it.