• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13732588874

08 Mar 2025 01:39AM CUT coverage: 99.425%. Remained the same
13732588874

push

github

web-flow
Add warnings on storing pointers, extend docs (#162)

6393 of 6430 relevant lines covered (99.42%)

28801.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/ecs/entity.go
1
package ecs
2

3
import "encoding/json"
4

5
type entityID uint32
6

7
var entityType = typeOf[Entity]()
8
var entitySize = sizeOf(entityType)
9
var entityIndexSize = sizeOf(typeOf[entityIndex]())
10

11
//var wildcard = Entity{1, 0}
12

13
// Entity is an identifier for entities.
14
//
15
// Can be stored safely in components, resources or elsewhere.
16
// For stored entities, it may be necessary to check their alive status with [World.Alive].
17
//
18
// ⚠️ Always store entities by value, never by pointer!
19
type Entity struct {
20
        id  entityID // Entity ID
21
        gen uint32   // Entity generation
22
}
23

24
func newEntity(id entityID) Entity {
8✔
25
        return Entity{id, 0}
8✔
26
}
8✔
27

28
// ID returns the entity's ID, primarily for debugging purposes.
29
func (e Entity) ID() uint32 {
101✔
30
        return uint32(e.id)
101✔
31
}
101✔
32

33
// Gen returns the entity's generation, primarily for debugging purposes.
34
func (e Entity) Gen() uint32 {
1✔
35
        return e.gen
1✔
36
}
1✔
37

38
// IsZero returns whether this entity is the reserved zero entity.
39
func (e Entity) IsZero() bool {
958✔
40
        return e.id == 0
958✔
41
}
958✔
42

43
// isWildcard returns whether this entity is the reserved wildcard entity.
44
func (e Entity) isWildcard() bool {
1✔
45
        return e.id == 1
1✔
46
}
1✔
47

48
// MarshalJSON returns a JSON representation of the entity, for serialization purposes.
49
//
50
// The JSON representation of an entity is a two-element array of entity ID and generation.
51
func (e Entity) MarshalJSON() ([]byte, error) {
1✔
52
        arr := [2]uint32{uint32(e.id), e.gen}
1✔
53
        jsonValue, _ := json.Marshal(arr) // Ignore the error, as we can be sure this works.
1✔
54
        return jsonValue, nil
1✔
55
}
1✔
56

57
// UnmarshalJSON into an entity.
58
//
59
// For serialization purposes only. Do not use this to create entities!
60
func (e *Entity) UnmarshalJSON(data []byte) error {
2✔
61
        arr := [2]uint32{}
2✔
62
        if err := json.Unmarshal(data, &arr); err != nil {
3✔
63
                return err
1✔
64
        }
1✔
65
        e.id = entityID(arr[0])
1✔
66
        e.gen = arr[1]
1✔
67

1✔
68
        return nil
1✔
69
}
70

71
// entityIndex denotes an entity's location by table and row index.
72
type entityIndex struct {
73
        table tableID
74
        row   uint32
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc