• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Entity Component System

fightlessbirds

New member
CX Code Contributor
Joined
Apr 15, 2022
Messages
9
For my previous game (Bird Hop) I wanted to use an entity component system, but there were none available for BlitzMax, so I coded my own. Now I am using Cerberus X, and again there is no ECS module available, so I’ve translated my BlitzMax module. In rewriting the module, I’ve cleaned it up and made it more robust.

rcge-ecs-uml.jpg


There are three key elements to the module: Entities, Components, and Systems. An Entity is a thing. Components are characteristics of things. Systems contain logic that is applied to things with certain characteristics. In a game there could be a component for a 2D position, and another for an image or animation. A movement system could select entities with a 2D position, and move them according to their speed. A rendering system could select entities with 2D positions and images/animations, and draw them to the screen. This approach is much more flexible than a class hierarchy approach. Entities can be built, changed, and destroyed at runtime.

Here we can see a simple usage example:
Cerberus:
Local myEcs:= New Ecs()

myEcs.AddSystem(New MoveSystem())
myEcs.AddSystem(New DrawSystem())

myEcs.AddComponent("Position")
myEcs.AddComponent("Image")

Local myEntity:= myEcs.CreateEntity()
myEntity.Bind("Position")
myEntity.Bind("Image")

myEcs.Update(deltaMillis)

myEntity.Kill()
Note: User defined System and Component classes must be added to #REFLECTION_FILTER. Some of the ECS's inner workings rely on reflection.
  1. Create a new Ecs object.
  2. Add user defined Systems to the Ecs.
  3. Add Component classes to the Ecs.
  4. Create Entities, bind/unbind Components, whenever you like.
  5. Update the Ecs once per iteration of your game loop.
  6. Kill Entities when they are no longer needed.
Example System:
Cerberus:
Class DrawSystem Extends System
    Method GetArchetype:String[]()
        Return ["Position", "Image"]
    End
 
    Method Update:Void(entities:List<Entity>, dt:Int)
        For Local e:= EachIn entities
            Local pos:= Position(e.GetComponent("Position"))
            Local img:= Image(e.GetComponent("Image"))
            DrawImage(img, pos.x, pos.y)
        Next
    End
End

My ECS is designed with a naïve object-oriented (OO) approach, however there is a more advanced, more performant way via data-oriented (DO) design. Some excellent examples of DO ECSs are EnTT and flecs.

This module is available on Codeberg as part of my game framework.
 
Last edited:
Welcome! That looks quite a bit of work you put in there. Thank you for sharing it.
 
Back
Top Bottom