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
132 stars 10 forks source link

Unable to delete Entity #18

Open cchandel opened 1 year ago

cchandel commented 1 year ago

Hi, I'm creatng an entity in an asyncworld as below.

e = Gaw.NewEntity()
e.Add(p, m, t, r, pr, h, i, d, r)
Entities[id] = e

then deleting the entity by

Entities[id].Destroy()

After Destroying, the entity remains available when doing :-

iterT := ecs.GetComponentAll[components.T](m)
for iter := iterT; !iter.End(); iter.Next() {
  ecs.Log.Info(t.id)
}

Please help.

zllangct commented 1 year ago

Now it's running correctly. This bug was caused by incorrect deletion order in the API.

func (e *EntityInfo) Destroy(world IWorld) {
    for i := 0; i < len(e.compound); i++ {
        world.deleteComponentByIntType(e.entity, e.compound[i])
    }
    // must be last
    world.deleteEntity(e.entity)
}

Adjust API to no longer export EntityInfo, All operations should be obtained through SyncWrapper.

world.Sync(func(gaw SyncWrapper) error {
    u, ok := GetUtility[__world_Test_U_Input](gaw)
    if !ok {
        return errors.New("utility not found")
    }
    gaw.DestroyEntity(entities[1])
    return nil
})