Log In  
Follow
apa64
Follow

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:

I'm a software engineer, professional coding background is a bit of data comms in C and lot of Java starting from 1.4, some Android and mostly Java EE for enterprise systems with web fronts. For the last 5 or so years I haven't really coded except shell or other script/configuration languages for automating configuration management, CI/CD and so on (nowadays they call it devops).

I've been interested in PICO-8 for some time, but finally got it as a part of the great itch.io Black Lives Matter bundle. To me gamedev is previously unknown territory and it's fun to learn how games differ from other applications.

Also playing around with PICO-8 on my summer vacation revealed how much brain capacity my dayjob takes. On work days there is no chance that I could do anything productive with PICO in the evening. Scary.

I'm used to (and like) working within frameworks. I got a bit of blank paper syndrome with PICO-8 initially :) First I tried to apply traditional OOP but it didn't feel like a good fit for games. Browsing this BBS I found about ECS and it feels like a better fit for game applications. Now I just have to bend my mindset to it!

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

Hmm. The links above don't look like links but they work anyway.

P#81053 2020-08-22 20:28 ( Edited 2020-08-22 20:29)

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?
P#79575 2020-07-18 19:38

Cart #apa64_tictactoe1-0 | 2020-07-08 | Code ▽ | Embed ▽ | No License

Woohoo I made my first game! Quite ugly (both graphics and code) but correctly working 3x3 tic-tac-toe for 2 players, no computer opponent. Also available at GitHub.

P#79067 2020-07-08 20:34 ( Edited 2020-07-08 20:34)

Cart #apa64_scroller2-0 | 2020-07-02 | Code ▽ | Embed ▽ | No License

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...

P#78797 2020-07-02 17:10

Follow Lexaloffle:        
Generated 2021-06-15 20:01:01 | 0.099s | Q:15