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

mlange-42 / ark / 13605754803

01 Mar 2025 02:30PM CUT coverage: 96.988% (-0.2%) from 97.185%
13605754803

Pull #86

github

web-flow
Merge c652e83fc into 4936252be
Pull Request #86: Resource shortcuts `GetResource` and `AddResource`

0 of 8 new or added lines in 1 file covered. (0.0%)

3832 of 3951 relevant lines covered (96.99%)

47445.59 hits per line

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

84.62
/ecs/functions.go
1
package ecs
2

3
import "reflect"
4

5
// ComponentID returns the [ID] for a component type via generics.
6
// Registers the type if it is not already registered.
7
//
8
// The number of unique component types per [World] is limited to 256 ([MaskTotalBits]).
9
//
10
// Panics if called on a locked world and the type is not registered yet.
11
//
12
// Note that type aliases are not considered separate component types.
13
// Type re-definitions, however, are separate types.
14
//
15
// ⚠️ Warning: Using IDs that are outside of the range of registered IDs anywhere in [World] or other places will result in undefined behavior!
16
func ComponentID[T any](w *World) ID {
1,899✔
17
        tp := reflect.TypeOf((*T)(nil)).Elem()
1,899✔
18
        return w.componentID(tp)
1,899✔
19
}
1,899✔
20

21
// ComponentIDs returns a list of all registered component IDs.
22
func ComponentIDs(w *World) []ID {
1✔
23
        intIds := w.storage.registry.IDs
1✔
24
        ids := make([]ID, len(intIds))
1✔
25
        for i, iid := range intIds {
3✔
26
                ids[i] = id8(iid)
2✔
27
        }
2✔
28
        return ids
1✔
29
}
30

31
// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
32
func ComponentInfo(w *World, id ID) (CompInfo, bool) {
2✔
33
        tp, ok := w.storage.registry.ComponentType(id.id)
2✔
34
        if !ok {
3✔
35
                return CompInfo{}, false
1✔
36
        }
1✔
37

38
        return CompInfo{
1✔
39
                ID:         id,
1✔
40
                Type:       tp,
1✔
41
                IsRelation: w.storage.registry.IsRelation[id.id],
1✔
42
        }, true
1✔
43
}
44

45
// TypeID returns the [ID] for a component type.
46
// Registers the type if it is not already registered.
47
//
48
// The number of unique component types per [World] is limited to [MaskTotalBits].
49
func TypeID(w *World, tp reflect.Type) ID {
2✔
50
        return w.componentID(tp)
2✔
51
}
2✔
52

53
// Comp is a helper to pass component types to functions and methods.
54
// Use function [C] to create one.
55
type Comp struct {
56
        tp reflect.Type
57
}
58

59
// C creates a [Comp] instance for the given type.
60
func C[T any]() Comp {
68✔
61
        return Comp{typeOf[T]()}
68✔
62
}
68✔
63

64
// Type returns the reflect.Type of the component.
65
func (c Comp) Type() reflect.Type {
1✔
66
        return c.tp
1✔
67
}
1✔
68

69
// ResourceID returns the [ResID] for a resource type via generics.
70
// Registers the type if it is not already registered.
71
//
72
// The number of resources per [World] is limited to [MaskTotalBits].
73
func ResourceID[T any](w *World) ResID {
6✔
74
        tp := reflect.TypeOf((*T)(nil)).Elem()
6✔
75
        return w.resourceID(tp)
6✔
76
}
6✔
77

78
// ResourceIDs returns a list of all registered resource IDs.
79
func ResourceIDs(w *World) []ResID {
1✔
80
        intIds := w.resources.registry.IDs
1✔
81
        ids := make([]ResID, len(intIds))
1✔
82
        for i, iid := range intIds {
3✔
83
                ids[i] = ResID{id: iid}
2✔
84
        }
2✔
85
        return ids
1✔
86
}
87

88
// ResourceType returns the reflect.Type for a resource [ResID], and whether the ID is assigned.
89
func ResourceType(w *World, id ResID) (reflect.Type, bool) {
2✔
90
        return w.resources.registry.ComponentType(id.id)
2✔
91
}
2✔
92

93
// GetResource returns a pointer to the given resource type in the world.
94
// Returns nil if there is no such resource.
95
//
96
// Uses reflection. For more efficient access, use [Resource].
97
// This more than 20 times faster than the GetResource function.
98
//
99
// See also [AddResource].
NEW
100
func GetResource[T any](w *World) *T {
×
NEW
101
        return w.resources.Get(ResourceID[T](w)).(*T)
×
NEW
102
}
×
103

104
// AddResource adds a resource to the world.
105
// Returns the ID for the added resource.
106
//
107
// Panics if there is already such a resource.
108
//
109
// Uses reflection. For more efficient access, use [Resource].
110
//
111
// The number of resources per [World] is limited to [MaskTotalBits].
NEW
112
func AddResource[T any](w *World, res *T) ResID {
×
NEW
113
        id := ResourceID[T](w)
×
NEW
114
        w.resources.Add(id, res)
×
NEW
115
        return id
×
NEW
116
}
×
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