package ecsimport ()// entityID is the type for entity IDs.type entityID uint32// runtime type of entitiesvar entityType = reflect.TypeFor[Entity]()// memory size of an Entityvar entitySize = entityType.Size()// memory size of an entityIndexvar entityIndexSize = reflect.TypeFor[entityIndex]().Size()//var wildcard = Entity{1, 0}// Entity is an identifier for entities.//// Can be stored safely in components, resources or elsewhere.// For stored entities, it may be necessary to check their alive status with [World.Alive].//// ⚠️ Always store entities by value, never by pointer!//// In Ark, entities are returned to a pool when they are removed from the world.// These entities can be recycled, with the same ID ([Entity.ID]), but an incremented generation ([Entity.Gen]).// This allows to determine whether an entity hold by the user is still alive, despite it was potentially recycled.typeEntitystruct { id entityID// Entity ID gen uint32// Entity generation}// newEntity creates a new entity with the given ID.func newEntity( entityID) Entity {returnEntity{, 0}}// ID returns the entity's ID, primarily for debugging purposes.func ( Entity) () uint32 {returnuint32(.id)}// Gen returns the entity's generation, primarily for debugging purposes.func ( Entity) () uint32 {return .gen}// IsZero returns whether this entity is the reserved zero entity.func ( Entity) () bool {return .id == 0}// isWildcard returns whether this entity is the reserved wildcard entity.func ( Entity) () bool {return .id == 1}// MarshalJSON returns a JSON representation of the entity, for serialization purposes.//// The JSON representation of an entity is a two-element array of entity ID and generation.func ( Entity) () ([]byte, error) { := [2]uint32{uint32(.id), .gen} , := json.Marshal() // Ignore the error, as we can be sure this works.return , nil}// UnmarshalJSON into an entity.//// For serialization purposes only. Do not use this to create entities!func ( *Entity) ( []byte) error { := [2]uint32{}if := json.Unmarshal(, &); != nil {return } .id = entityID([0]) .gen = [1]returnnil}// MarshalBinary returns a binary representation of the entity, for serialization and networking purposes.func ( *Entity) () ( []byte, error) { := make([]byte, 8)binary.BigEndian.PutUint32([0:4], uint32(.id))binary.BigEndian.PutUint32([4:8], .gen)return , nil}// AppendBinary appends the binary representation of the entity to the given slice,// for serialization and networking purposes.func ( *Entity) ( []byte) ([]byte, error) { = binary.BigEndian.AppendUint32(, uint32(.id)) = binary.BigEndian.AppendUint32(, .gen)return , nil}// UnmarshalBinary into an entity.//// For serialization and networking purposes only. Do not use this to create entities!func ( *Entity) ( []byte) error {iflen() != 8 {returnfmt.Errorf("invalid data length: expected 8 bytes, got %d", len()) } .id = entityID(binary.BigEndian.Uint32([0:4])) .gen = binary.BigEndian.Uint32([4:8])returnnil}// entityIndex denotes an entity's location by table and row index.type entityIndex struct { table tableID row uint32}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.