mizu icon indicating copy to clipboard operation
mizu copied to clipboard

Filtering entities with external slice reuse

Open sedyh opened this issue 2 years ago • 0 comments

Problem

We need an alternative to Filter, which will allow you to use your own list (for example, for sorting), but at the same time, it will not do the allocation once again.

Solution

You can make an analogue of append, but for entities.

func (v *view) AppendEntities(entities []Entity) {
    for _, en := range v.w.entities {
        if en.mask.contains(v.mask) {
            entities = append(entities, en)
        }
    }
}

type System struct {
    entities []Entity
    balls engine.View
}

func (s *System) Update(w engine.World) {
    // Clear balls
    s.entities = s.entities[:0]
    ...
    // Add balls
    balls.AppendEntities(s.entities)
}

sedyh avatar May 13 '22 11:05 sedyh