Road to ECS
Intro
This is about teaching myself Entity-Component-System (ECS) pattern. My current goal is to implement a few basic features one by one to build a code snippet library, starting from just drawing things on screen. Code will be in my GitHub/road-to-ecs and also as cartridges in this blog thread.
Some personal background:
Entity-Component-System
ECS is a pattern for dividing data and functionality in independent components. It fullfills the Single Responsibility Principle nicely: a functionality (= a system) can do one thing and not care about others.
The pieces of ECS are:
- Entities = "things in the world"
- Components = data
- Systems = functionality
Systems do their thing for Entities which have Components required by the System. For example we could have a creature on screen that moves according to its current speed. The creature is an entity, speed is a component and movement is the system.
I was about to write more but really, this post explains everything better than I could: gamedev.stackexchange: What is the role of “systems” in a component-based entity architecture?
One thing I've picked up so far is that I should implement a system first. Required components will reveal themselves quite naturally when coding the system.
References
- BBS/KatrinaKitten: Tiny ECS Framework v1.1
- BBS/selfsame: Entity Component System
- BBS/alexr: ECS POC 1 v. 0.5
- BBS: Entity Component Systems
- gamedev.stackexchange: What is the role of “systems” in a component-based entity architecture?
- Mark Jordan: Entities, components and systems
- gamedev.net/Klutzershy: Understanding Component-Entity-Systems
Hmm. The links above don't look like links but they work anyway.
Hi, I'm practicing how to do object oriented programming in PICO-8 but struggling a bit to map things in my head between OO design and actual game code. My practice project is doing MBoffin's top-down game tutorial with an OO approach but my long term goal is to do either a top-down adventure/rpg (=Zelda) or turn-based strategy (=UFO X-COM).
The reason I want to do OOP is my Java dev background and also personally I don't like procedural code, it feels so messy and cluttered. I'd like to have the behaviour and presentation of an entity in its class instead of spread around the application but that's the difficult part for me at the moment.
Bunch of questions:
- Is there any point of doing a class for a singleton entity, eg. player actor? As my player1 object will be the only instance I could as well have that and not bother with the prototype. Now I have something like this:
-- prototype player = {} function player:new(o) local o = o or {} setmetatable(o, self) self.__index = self return o end function player:draw() spr(self.sprite, self.x*8, self.y*8) end function player:move() if (btnp(⬅️)) newx-=1 if (btnp(➡️)) newx+=1 if (btnp(⬆️)) newy-=1 if (btnp(⬇️)) newy+=1 ... end function _init() -- actual object player1 = player:new( { x = 10, y = 10, sprite = 2 } ) ... end |
-
A wider question based on above: What entities should I have as classes (or "prototypes" in Lua OOP)? Player actor, NPC characters/enemies, world map, world objects (text signs, anything interactive), ...?
-
What patterns/frameworks/templates you use for game projects? State machines, OOP, functional programming, ...?
- Any pointers to any non-minified game source code to study? Something with longer that 1-char variable names and comments? Anything good in GitHub?
Hello world!
How can I make my code more like a library or a self-contained independent module/component? Here's a text scroller by me (my first published cart yay!). Currently you need to call three functions to use it:
function _init() scroller = init_scroller("lorem ipsum dolor sit amet, consectetur... ", 30, 5/30) end function _update() update_scroller() end function _draw() cls(0) draw_scroller(4, 60, 7) end |
I tried to write it more like an component but couldn't make it work. My idea was that init_scroller() would return "an object" which would contain everything. Then you'd call object.update() and object.draw() to use it and could create as many instances as needed. However my return object had just pointer to global functions and later instances overwrote earlier ones.
Can you point me to a simple example how to do components in PICO-8?
You can probably tell I'm coming from Java background ;) Are there any specific style guides or general conventions for PICO-8 Lua? I'm coding with VS Code, can't do it with the 128x128 window...