I'm currently playing around with ECS but was wondering if my understanding of ECS was correct. Based on what I've understood so far:
- Entities: Are structs and only hold an id that identifies it.
- Components: Also are structs, but holds properties that describe said component. Example: position (vec3), hp (float), etc. ***Also stores a reference to the entity it represents?
- Systems: Functions that act upon components. If I need to update my Position Component, a system would do that.
My questions so far:
- Should components also store a reference or id to the entity it represents? I'm asking this after reading an article where the author stored an array of components within the component struct. I was under the understanding that entities and components should be treated independently.
- Should I keep an array for each component and loop through them in my main loop. I'm very confused about this part. In oop I would just put all gameobjects in an array and loop through them.
I also wrote some pseudo code showing what I think the whole thing should look like. It's very Rust...but the code is straight forward. Thanks for reading btw:
MainLoop
UpdateTime()
FixedUpdate
UpdateSystems
UpdatePositions() // System: Loops through all position components and updates them
EndFixedUpdate
RenderUpdate
UpdateSystems
UpdateRenderables() // System: Loops through all Render Components and draws
UpdatePostprocessingSystems()
EndRenderUpdate
Loop MainLoop
// Components
struct Entity {
id: u16, // unsigned integer
}
struct TransformComponent {
entityID: u16, // Reference to entity
position: Vector3,
}
struct RenderComponent {
entityID: u16, // Reference to entity
meshID: GLuint, // Reference to mesh
}
// Should I keep arrays of each kind of component seperatly?
// Systems
fn update_tranforms () {
// Loop array of TransformComponents
// Apply physics to position property
// Input logic: aka if (Input.rightArrow) blah.blah.blah
}
fn update_render_components () {
// Loop array of render components
// Draw using meshID in RenderComponent
}