Skip to content

Commit 92bb92a

Browse files
authored
Fix relation docs, simplify API reference links (#419)
1 parent 3864a17 commit 92bb92a

File tree

5 files changed

+73
-65
lines changed

5 files changed

+73
-65
lines changed

docs/content/concepts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ When dealing with stored entities, it may be required to check whether they are
4545
{{< code-func concepts_test.go TestEntityAlive >}}
4646

4747
In Ark, entities are returned to a pool when they are removed from the world.
48-
These entities can be recycled, with the same ID ({{< api ecs Entity.ID Entity.ID >}}), but an incremented generation ({{< api ecs Entity.Gen Entity.Gen >}}).
48+
These entities can be recycled, with the same ID ({{< api ecs Entity.ID >}}), but an incremented generation ({{< api ecs Entity.Gen >}}).
4949
This allows to determine whether an entity held by the user is still alive, despite it was potentially recycled.
5050

5151
## Components

docs/content/relations/index.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ for relations with different target entities.
3434

3535
## Relation components
3636

37-
To use entity relations, create components that have *embedded* an {{< api ecs RelationMarker >}} as their first member:
37+
To use entity relations, create components that have *embedded* a {{< api ecs RelationMarker >}} as their first member:
3838

3939
```go
4040
type ChildOf struct {
@@ -48,11 +48,12 @@ The component can contain further variables, but the marker must be the first on
4848
## Creating relations
4949

5050
Most methods of `MapX` (e.g. {{< api ecs Map2 >}}) provide var-args for specifying relationship targets.
51-
These are of type {{< api Relation >}}, which is an interface with multiple implementations:
51+
These are of type {{< api ecs Relation >}}, which specifies a component type and a target entity.
52+
There are multiple constructors for {{< api ecs Relation >}}:
5253

53-
{{< api Rel >}} is safe, but has some run-time overhead for component ID lookup.
54-
55-
{{< api RelIdx >}} is fast but more error-prone.
54+
- {{< api ecs Rel >}} uses a generic type parameter to identify the component. It is safe, but has some run-time overhead for component ID lookup on first usage.
55+
- {{< api ecs RelIdx >}} uses an index to identify the component. It is fast but more error-prone.
56+
- {{< api ecs RelID >}} uses a component ID. It is for use with the [unsafe API](../unsafe/).
5657

5758
See the examples below for their usage.
5859

@@ -62,10 +63,10 @@ When creating entities, we can use a `MapX` (e.g. {{< api ecs Map2.NewEntity >}}
6263

6364
{{< code-func relations_test.go TestNewEntity >}}
6465

65-
For the faster variant {{< api RelIdx >}}, note that the first argument
66+
For the faster variant {{< api ecs RelIdx >}}, note that the first argument
6667
is the zero-based index of the relation component in the {{< api ecs Map2 >}}'s generic parameters.
6768

68-
If there are multiple relation components, multiple {{< api Rel >}}/{{< api RelIdx >}} arguments can (and must) be used.
69+
If there are multiple relation components, multiple {{< api ecs Rel >}}/{{< api ecs RelIdx >}} arguments can (and must) be used.
6970

7071
### When adding components
7172

@@ -115,9 +116,9 @@ For short-lived targets, it is better to pass them when building a query with {{
115116

116117
These targets are not cached, but the same filter can be used for different targets.
117118

118-
Filters also support both {{< api Rel >}} and {{< api RelIdx >}}.
119-
In the filter examples above, we used the slow but safe {{< api Rel >}} when building the filter.
120-
When getting the query, we use the faster {{< api RelIdx >}},
119+
Filters also support both {{< api ecs Rel >}} and {{< api ecs RelIdx >}}.
120+
In the filter examples above, we used the slow but safe {{< api ecs Rel >}} when building the filter.
121+
When getting the query, we use the faster {{< api ecs RelIdx >}},
121122
because in real-world use cases this is called more frequently than the one-time filter construction.
122123

123124
Relation targets not specified by the filter are treated as wildcard.
@@ -180,4 +181,4 @@ to represent animals of different species in multiple farms.
180181
{{< code relations_example_test.go >}}
181182

182183
Note that this examples uses the safe and clear, but slower generic variant to specify relationship targets.
183-
As an optimization, {{< api RelIdx >}} could be used instead of {{< api Rel >}}, particularly for queries.
184+
As an optimization, {{< api ecs RelIdx >}} could be used instead of {{< api ecs Rel >}}, particularly for queries.

docs/content/stats/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@ Archetype -- Tables: 1, Comps: 2, Entities: 100, Cap: 1024, Mem: 32.
3636

3737
## World stats
3838

39-
{{< api "ecs/stats" World stats.World >}} provides world information like a list of all component types
39+
{{< api "ecs/stats" World >}} provides world information like a list of all component types
4040
and the total memory reserved for entities and components.
41-
Further, it contains {{< api "ecs/stats" Entities stats.Entities >}} and
42-
a {{< api "ecs/stats" Archetype stats.Archetype >}} for each archetype.
41+
Further, it contains {{< api "ecs/stats" Entities >}} and
42+
a {{< api "ecs/stats" Archetype >}} for each archetype.
4343

4444
## Entity stats
4545

46-
{{< api "ecs/stats" Entities stats.Entities >}} contains information about the entity pool,
46+
{{< api "ecs/stats" Entities >}} contains information about the entity pool,
4747
like capacity, alive entities and available entities for recycling.
4848

4949
## Archetype stats
5050

51-
{{< api "ecs/stats" Archetype stats.Archetype >}} provides information about an archetype, like its components,
51+
{{< api "ecs/stats" Archetype >}} provides information about an archetype, like its components,
5252
memory in total and per entity, and more state information.
5353

54-
Further, it contains a {{< api "ecs/stats" Table stats.Table >}} for each table.
54+
Further, it contains a {{< api "ecs/stats" Table >}} for each table.
5555

5656
## Table stats
5757

58-
{{< api "ecs/stats" Table stats.Table >}} contains size, capacity and memory information for a table.
58+
{{< api "ecs/stats" Table >}} contains size, capacity and memory information for a table.
5959
Tables are used to represent sub-archetypes with the same components, but a different combination
6060
of [relationship](../relations) targets.

docs/layouts/shortcodes/api.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{{- $pkg := .Get 0 -}}
2-
{{- $text := (replace $pkg "/" ".") -}}
2+
{{- $text := (replace $pkg "ecs/" "") -}}
3+
{{- $text := (replace $text "ecs" "") -}}
34
{{- $link := $pkg -}}
45
{{- if .Get 1 -}}
56
{{- $mem := .Get 1 -}}
6-
{{- $text = printf "%s.%s" $text $mem -}}
7+
{{- if $text -}}
8+
{{- $text = printf "%s.%s" $text $mem -}}
9+
{{- else -}}
10+
{{- $text = $mem -}}
11+
{{- end -}}
712
{{- $link = printf "%s#%s" $link $mem -}}
813
{{- if .Get 2 -}}
914
{{- $text = .Get 2 -}}

ecs/relation.go

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,57 @@ type relationID struct {
2525
// Relation is the common type for specifying relationship targets.
2626
// It can be created with [Rel], [RelIdx] and [RelID].
2727
//
28-
// - [Rel] is safe, but has some run-time overhead for component [ID] lookup.
29-
// - [RelIdx] is fast but more error-prone.
30-
// - [RelID] is used in the [Unsafe] API.
28+
// - [Rel] uses a generic type parameter to identify the component.
29+
// It is safe, but has some run-time overhead for component [ID] lookup on first usage.
30+
// - [RelIdx] uses an index to identify the component. It is fast but more error-prone.
31+
// - [RelID] uses a component ID. It is for use with the [Unsafe] API.
3132
type Relation struct {
3233
componentType reflect.Type // Component type of the relation
3334
target Entity // Target entity of the relation
3435
component ID // Component ID of the relation
3536
index uint8 // Component index of the relation in a mapper or query
3637
}
3738

39+
// Rel creates a new [Relation] for a component type.
40+
//
41+
// It can be used as a safer but slower alternative to [RelIdx].
42+
// Requires a component ID lookup when used the first time.
43+
// On reuse, it is as fast as [RelIdx].
44+
func Rel[C any](target Entity) Relation {
45+
return Relation{
46+
target: target,
47+
componentType: reflect.TypeFor[C](),
48+
index: 255,
49+
}
50+
}
51+
52+
// RelIdx creates a new [Relation] for a component index.
53+
// The index refers to the position of the component in the generics
54+
// of e.g. a [Map2] or [Filter2].
55+
// For filters, components specified by [Filter2.With] are also covered by the index.
56+
//
57+
// It can be used as faster but less safe alternative to [Rel].
58+
//
59+
// Note that the index should not be confused with a component [ID] as obtained by [ComponentID]!
60+
// For component IDs, use [RelID].
61+
func RelIdx(index int, target Entity) Relation {
62+
return Relation{
63+
index: uint8(index),
64+
target: target,
65+
}
66+
}
67+
68+
// RelID creates a new [Relation] for a component ID.
69+
//
70+
// It is used in Ark's unsafe, ID-based API.
71+
func RelID(id ID, target Entity) Relation {
72+
return Relation{
73+
target: target,
74+
component: id,
75+
index: 255,
76+
}
77+
}
78+
3879
// relationIDForUnsafe converts the Relation to a relationID.
3980
//
4081
// Relation must use ID or component type.
@@ -55,7 +96,7 @@ func (r *Relation) relationIDForUnsafe(world *World) relationID {
5596
}
5697
}
5798

58-
// id returns the component ID of this RelationID.
99+
// id returns the component ID of this Relation.
59100
func (r *Relation) id(ids []ID, world *World) ID {
60101
if r.index < 255 {
61102
return ids[r.index]
@@ -67,50 +108,11 @@ func (r *Relation) id(ids []ID, world *World) ID {
67108
return r.component
68109
}
69110

70-
// targetEntity returns the target [Entity] of this RelationID.
111+
// targetEntity returns the target [Entity] of this Relation.
71112
func (r *Relation) targetEntity() Entity {
72113
return r.target
73114
}
74115

75-
// RelID creates a new [Relation] for a component ID.
76-
//
77-
// It is used in Ark's unsafe, ID-based API.
78-
func RelID(id ID, target Entity) Relation {
79-
return Relation{
80-
target: target,
81-
component: id,
82-
index: 255,
83-
}
84-
}
85-
86-
// Rel creates a new [Relation] for a component type.
87-
//
88-
// It can be used as a safer but slower alternative to [RelIdx].
89-
// Required a component ID lookup when used the first time.
90-
// Un reuse, it is as fast as [RelIdx].
91-
func Rel[C any](target Entity) Relation {
92-
return Relation{
93-
target: target,
94-
componentType: reflect.TypeFor[C](),
95-
index: 255,
96-
}
97-
}
98-
99-
// RelIdx creates a new [Relation] for a component index.
100-
//
101-
// It can be used as faster but less safe alternative to [Rel].
102-
//
103-
// Note that the index refers to the position of the component in the generics
104-
// of e.g. a [Map2] or [Filter2].
105-
// This should not be confused with component [ID] as obtained by [ComponentID]!
106-
// For component IDs, use [RelationID].
107-
func RelIdx(index int, target Entity) Relation {
108-
return Relation{
109-
index: uint8(index),
110-
target: target,
111-
}
112-
}
113-
114116
// Helper for converting relationSlice
115117
type relationSlice []Relation
116118

0 commit comments

Comments
 (0)