Hacker News new | ask | show | jobs
by skitter 985 days ago
> Systems are the one aspect that for me are hardest to get my head

In an ECS architecture, the world is a database of entities, which are just identifiers, which have components associated with them. Systems is simply code that query this database and operate on the returned data. For example, if you want burning entities to set burnable entities on fire, you might write a system like this, using Bevy as an example:

    fn fire_spread(mut commands: Commands, burning: Query<&Collider, With<Burning>>, flammable: Query<(Entity, &Collider), With<Flammable>>) {
        for col_1 in &burning {
            for (flammable_entity, col_2) in &flammable {
                if col_1.touches(col_2) {
                    commands.entity(flammable_entity).insert(Burning)
                }
            }
        }
    }
The Commands here exists to defer archetype moves – otherwise what should the query do if you added Burning to an entity while the query was running? And of course in a real game, you might want to use a spatial query so time complexity isn't m×n. You could then run this system every tick:

    app.add_systems(Update, fire_spread)
If you're familiar with C++, I can also recommend you check out https://www.flecs.dev/flecs/
1 comments

Awesome, thx.