22 Relationships makes it possible to describe entity graphs natively in ECS.
33
44 Adding/removing relationships is similar to adding/removing regular components,
5- with as difference that instead of a single component id, a relationship adds
5+ with the difference that instead of a single component id, a relationship adds
66 a pair of two things to an entity. In this pair, the first element represents
77 the relationship (e.g. "Eats"), and the second element represents the relationship
88 target (e.g. "Apples").
@@ -36,9 +36,6 @@ world:add(alice, pair(Likes, bob))
3636-- Test if entity has a relationship pair
3737print (world :has (bob , pair (Eats , Apples ))) -- true
3838
39- -- Test if entity has a relationship wildcard
40- print (world :has (bob , pair (Eats , jecs .Wildcard ))) -- true
41-
4239--[[
4340 Querying for relationship targets
4441
@@ -77,26 +74,6 @@ for child in world:query(pair(ChildOf, parent)) do
7774 print (`Entity {child } is a child of parent {parent }` )
7875end
7976
80- --[[
81- Querying with wildcards and getting targets
82-
83- When you query with a wildcard, you can use world:target() to get the
84- actual target entity. This is useful when you want to find all entities
85- with a relationship, regardless of the target.
86- ]]
87-
88- -- Find all entities that eat something (any target)
89- for entity in world :query (pair (Eats , jecs .Wildcard )) do
90- local food = world :target (entity , Eats ) -- Get the actual target
91- print (`Entity {entity } eats {food }` )
92- end
93-
94- -- Find all entities that like someone (any target)
95- for entity in world :query (pair (Likes , jecs .Wildcard )) do
96- local target = world :target (entity , Likes )
97- print (`Entity {entity } likes {target }` )
98- end
99-
10077--[[
10178 Combining relationship queries with regular components
10279
@@ -118,39 +95,6 @@ for entity, pos, health in world:query(Position, Health, pair(ChildOf, parent))
11895 print (`Child {entity } has position {pos } and health {health }` )
11996end
12097
121- --[[
122- Querying for entities with multiple relationship targets
123-
124- An entity can have multiple relationships with the same relationship type
125- but different targets. For example, bob might like both alice and charlie.
126-
127- When querying with a wildcard, you'll get the entity once, but world:target()
128- will return the first matching target. If you need all targets, you'll need
129- to use a different approach (see the targets example for advanced usage).
130- ]]
131-
132- local charlie = world :entity ()
133- world :add (bob , pair (Likes , charlie ))
134-
135- -- This query will return bob once, even though bob likes both alice and charlie
136- for entity in world :query (pair (Likes , jecs .Wildcard )) do
137- local target = world :target (entity , Likes )
138- print (`Entity {entity } likes {target }` ) -- Will show one target per entity
139- end
140-
141- --[[
142- Querying for all relationships with a specific target
143-
144- You can also query for all entities that have any relationship with a
145- specific target using a wildcard for the relationship part.
146- ]]
147-
148- -- Find all entities that have any relationship with alice as the target
149- for entity in world :query (pair (jecs .Wildcard , alice )) do
150- -- Note: This is less common and may have performance implications
151- print (`Entity {entity } has some relationship with alice` )
152- end
153-
15498--[[
15599 Relationship pairs, just like regular component, can be associated with data.
156100]]
@@ -178,30 +122,4 @@ for entity, eats_data in world:query(pair(Eats, Apples)) do
178122 print (`Entity {entity } eats apples: amount = {eats_data .amount }` )
179123end
180124
181- --[[
182- When querying for relationship pairs, it is often useful to be able to find
183- all instances for a given relationship or target. To accomplish this, a game
184- can use wildcard expressions.
185-
186- Wildcards may used for the relationship or target part of a pair:
187-
188- pair(Likes, jecs.Wildcard) -- Matches all Likes relationships
189- pair(jecs.Wildcard, Alice) -- Matches all relationships with Alice as target
190-
191- Using world:target() is the recommended way to get the target in a wildcard
192- query. However, if you're in a very hot path and need maximum performance,
193- you can access the relationship column directly (see advanced examples).
194- ]]
195-
196- for entity in world :query (pair (Eats , jecs .Wildcard )) do
197- local nth = 0
198- local food = world :target (entity , Eats , nth )
199- while food do
200- local eats_data = world :get (entity , pair (Eats , food ))
201- assert (eats_data ) -- This coerces the type to be non-nilable for the type checker
202- print (`Entity {entity } eats {food }: amount = {eats_data .amount }` )
203-
204- nth += 1
205- food = world :target (entity , Eats , nth )
206- end
207- end
125+ -- For wildcard queries and world:target (0-based index), see 042_target.luau and 043_wildcards.luau.
0 commit comments