mizu icon indicating copy to clipboard operation
mizu copied to clipboard

The scene must be initialized in a certain order

Open sedyh opened this issue 2 years ago • 0 comments

Problem

At the moment, scene initialization should always go in the order: components > systems > entities. Otherwise, they will not find each other.


type Game struct{}

func (g *Game) Setup(w engine.World) {
    w.AddComponents()
    w.AddSystems()
    w.AddEntities()
}

Solution

It is necessary to correct this behavior by replacing the functions of adding all parts with a builder that will automatically initialize at the very end of the scene creation.

func (w *world) AddComponents(components ...interface{}) {
	w.futureComponents = append(w.futureComponents, components...)
}
func (w *world) AddSystems(systems ...interface{}) {
	w.futureSystems = append(w.futureSystems, components...)
}
func (w *world) AddEntities(entities ...interface{}) {
	w.futureEntities = append(w.futureEntities, components...)
}
func (w *world) ChangeScene(next Scene) {
	next.Setup(w)
        w.buildScene()
}

sedyh avatar May 13 '22 11:05 sedyh