3 ms·
I feel like blogs about ECS are needlessly verbose when the idea is really simple. Problem: Most games are built in with a game loop ticking frames through man
by jayd16 8y ago
I feel like blogs about ECS are needlessly verbose when the idea is really simple.
Problem:
Most games are built in with a game loop ticking frames through many gameobjects/actors. Many actors share some functionality like physics collision, but there is also a lot of unique functionality on the same objects.
Naive solution:
Use inheritance to build out your core functionality and override functionality as needed.
-This leads to a mess of interacting parts a massive type tree with many permutations of functionality.
-The deep type hierarchies blow out cache locality as the natural way is to tick through all functionality for a single object as you loop through all objects
ECS:
Break functionality apart into discrete systems. Use composition to build the permutations of functionality.
-Its easier to to manage what you intended and also easier to experiment with functionality through composition than inheritance.
-The natural way to loop through functionality is one system at a time. You loop over your entities many times but because you can run the same system code in a tight loop its really speeds things up.
-Systems provide singleton like functionality to store global state in a private way. In a heavy actor model we have to rely on static fields, which can be ok, but now you have a large cumbersome hierarchy of functionality with global access to state.
tl;dr
-Composition over inheritance is good in all walks of coding.
-Many discrete tight loops of systems is usually better than one loop of complex logic.
- skypjack 8y agoI don't think the idea is difficult actually. What I've found difficult when I decided to implement my own tool (https://github.com/skypjack/entt https://github.com/skypjack/entt) was that technical details on how to design something that was both easy to use and with good performance were scattered all around the web. I'm just trying to summarize what I've discovered so far for the "future me". :-)
- ah- 8y agoSadly the article doesn't mention how ECS typically solve the "holes" problem, where your arrays might be quite sparse which leads to inefficiency. Do you happen to know how that's usually done?
- skypjack 8y agoYeah, I'm the author of EnTT C++ ECS (https://github.com/skypjack/entt https://github.com/skypjack/entt) and I can guarantee you there are no holes there. :-) The next post (part 2 of the series) will be more or less all about this point. I hope to publish it as soon as possible. The idea is exactly to guide the reader through different models, from the easiest to implement to the ones that are probably hardest to develop but have no holes, perfect matches, higher performance and so on. Stay tuned.
- fenomas 8y agoIn my ECS I do this by storing the actual data in a dense array, alongside a hash that maps entity IDs to array indexes. If you want to see the (javascript) innards: https://github.com/andyhall/ent-comp https://github.com/andyhall/ent-comp
- ajuc 8y agoWhat's funny is that that's roughly how I wrote my first game when I only knew structural programming (and not that well). Game objects were Pascal records. There was 1 global table with all the records. There were many functions called from the game loop processing all the records in some way. Like one function updated orientation, velocity and position. Other did collision detection between objects, another did collision detection with the map. There was a function for possible reactions to collisions (if you collided with a powerup you get 100 health etc). It was all data driven, objects had integer indices referencing tables with predefined attributes. Like your ship record had "engineNo", which referenced table of engines with power, turning radius, fuel consumption, and other parameters for each kind of engine. Same with guns, armor, power generators. Map was 2d array of records which had tileType, tileImage, xParam, yParam. If tileType was teleport - it teleported any object it collided with it to (xParam, yParam). If tileType was switch it triggered tile on (xParam, yParam) every time something collided with it. There were also tileTypes that damaged anything that collide with it to a various degree, and tileTypes to jump to the next level. That was like 3000 lines of code in 2 files (because Turbo Pascal refused to compile it when it was in one file :) ). Everything was global. There were no classes. And it was pretty easy to understand and modify, and allowed a lot of design possibilities. Well, it would be easy if I used proper variable names and constants. As it was you had to remember what tileType==10 means, and I had 3-screen long function that was nested if-then-else with all possible tiletype-vs-objectType interactions. Like - bullets slow down when entering water tiles and disappear when hitting walls. Napalm particles change into smoke particles which expire after X frames. Napalm particles glue to walls and slowly move down. Grenades explode when touching walls. There were lots of such rules, but putting them in one place was the right thing now that I think about it. I'd argue even with abysmal naming practices it was more understandable than most of modern codebases, where this logic would be separated into 20 small classes. Then I learnt OO programming and tried to make a proper object oriented game architecture, and it was never as simple as that naive approach :)
- pavlov 8y ago> ”Many discrete tight loops of systems is usually better than one loop of complex logic.” A related low-level design choice for high-performance data structures is “Prefer structures of arrays over arrays of structures.”
- pault 8y agoMan this is interesting. This is exactly how I write my react/redux apps. I wonder if this is an architecture that works especially well for UI in general, since games are very similar in some respects (lots of disparate components, mostly reactive, lots of compromises when structuring code to meet requirements for visual design). It's an architecture that I have to spend a lot of time explaining to my coworkers, but once they learn to think that way they can suddenly jump in and do anything since they know exactly where to look for every feature.