zllangct / ecs

A Go-implementation of the ECS (Entity-Component-System), focus on the development of game server.
BSD 3-Clause "New" or "Revised" License
138 stars 9 forks source link

Unable to get data of different Entities using GetInterestedComponents #10

Open cchandel opened 3 years ago

cchandel commented 3 years ago

Hi, I've setup a world and added 3 Entities with movement component.

collection.go:34: collection Add:{"Speed":0.008888888888888889,"Course":145}
collection.go:34: collection Add:{"Speed":0.0033333333333333335,"Course":120}
collection.go:34: collection Add:{"Speed":0.0033333333333333335,"Course":45}

I'm trying to use an Input system similar to your example to get and change speed for an entity.

type MoveChange struct {
    ecs.FreeDisposableComponent[MoveChange]
}
import (
    "github.com/zllangct/ecs"
    "astt-server/components"
)

type MoveChangeSystem struct {
    ecs.System[MoveChangeSystem]
}

func (mc *MoveChangeSystem) Init() {
    ecs.AddRequireComponent2[components.Movement, components.MoveChange](mc)
}

func (mc *MoveChangeSystem) PreUpdate(event ecs.Event) {

    iterMovement := ecs.GetInterestedComponents[components.Movement](mc)
    if iterMovement == nil {
        return
    }
    for mov := iterMovement.Begin(); !iterMovement.End(); iterMovement.Next() {
        println(mov.Speed, mov.Course)
        println(mov.Owner().Entity())
    }
}

I get 3 values printed - but they're all the same.

+8.888889e-003 +1.450000e+002 -7673762469411480564

+8.888889e-003 +1.450000e+002 -7673762469411480564

+8.888889e-003 +1.450000e+002 -7673762469411480564

How do I get the data for each separate entity?

Thanks.

zllangct commented 3 years ago
        // mov should be reset every loop
    for mov := iterMovement.Begin(); !iterMovement.End(); mov = iterMovement.Next() {
        println(mov.Speed, mov.Course)
        println(mov.Owner().Entity())
    }
    //style 1:
    for iter := NewIterator(c); !iter.End(); iter.Next() {
        v := iter.Val()
        fmt.Printf("style 1: %+v\n", v)
    }

    //style 2:
    iter := NewIterator(c)
    for c := iter.Begin(); !iter.End(); c = iter.Next() {
        fmt.Printf("style 2: %+v\n", c)
    }
cchandel commented 3 years ago

This works.

for mov := iterMovement.Begin(); !iterMovement.End(); mov = iterMovement.Next()

However, changing the value

mov.Course = 140

does not change the value of course.

movementSystem.go:170: unit id:5 refresh:1s co:45
collection.go:34: collection Add:{"Speed":0,"Course":140}
moveChangeSystem.go:59: unit: 5 course changed: 140
movementSystem.go:170: unit id:5 refresh:1s co:45