You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When your game gets *really* big, adding each entity to every system would be time consuming and buggy using the methods mentioned above. However, you can easily add entities to systems based solely on the interfaces that entity implements by
195
+
utilizing the `SystemAddByInterfacer`. This takes a bit of work up front, but makes things much easier if your number of systems and entities increases. We're going to start with an example `System` MySystem, with `Component` ComponentA
196
+
197
+
```go
198
+
typeComponentAstruct {
199
+
num int
200
+
}
201
+
202
+
type mySystemEntity struct {
203
+
ecs.BasicEntity
204
+
*ComponentA
205
+
}
206
+
207
+
typeMySystemstruct {
208
+
entities []mySystemEntity
209
+
}
210
+
211
+
type (m *MySystem) Add(basic ecs.BasicEntity, a *ComponentA) { /* Add stuff goes here */ }
212
+
type (m *MySystem) Remove(basic ecs.BasicEntity) { /* Remove stuff here */ }
213
+
type (m *MySystem) Update(dt float32) { /* Update stuff here */ }
214
+
```
215
+
216
+
The components need to have corresponding Getters and Interfaces in order to be utilized. Let's add them
217
+
218
+
```go
219
+
func(a *ComponentA) GetComponentA() *ComponentA {
220
+
reurn a
221
+
}
222
+
223
+
typeAFaceinterface {
224
+
GetComponentA() *ComponentA
225
+
}
226
+
```
227
+
228
+
### Note
229
+
The convention is that we add Face to the end of the component's name for the interface.
230
+
231
+
Now that we have interfaces for all the components, we need to add an interface to tell if we use the system or not. (BasicEntity already has this setup for you, as does any component or system that uses entities in `engo/common`)
232
+
233
+
```go
234
+
typeMyableinterface {
235
+
ecs.BasicFace
236
+
AFace
237
+
}
238
+
```
239
+
240
+
### Note
241
+
The convention is to add able to the end of the system's name for the interface
242
+
243
+
Finally, we have to add the AddByInterface function to the system. Don't worry about the casting, it can't panic as the world makes sure it implements the required interface befor passing entities to it.
This takes **a pointer to** the interface that the system needs implemented to use AddByInterface.
261
+
262
+
Finally, to add an entity, rather than looping through all the systems, you can just
263
+
264
+
```go
265
+
w.AddEntity(&entity)
266
+
```
267
+
268
+
## Exclude flags
269
+
You can also add an interface to the system for components that can act as flags to NOT add an entity to that system. First you'll have to make the component. It'll have to have a Getter and Interface as well.
Now our system can automatically, and it'll include all the entities that implement the Myable interface, except any entity that implements the NotMyable interface.
0 commit comments