00:00how do you structure a game engine when
00:02deciding on how to handle objects in
00:04your game you have a few options let's
00:06take a look at two of them in this code
00:08we have an entity it's extremely simple
00:10to see what parts The Entity is made out
00:12of it's extremely simple to work with
00:14this particular entity okay now where's
00:17the problem with this why am I not doing
00:19everything like this well the problem is
00:22what if you want more data in this
00:24entity do you really need the data for
00:26all instances of the entity if yes then
00:29great if if not then you probably
00:31immediately thought let's make another
00:33class that inherits this entity great
00:35now we have an entity and a class that
00:37expands the data in the way we need now
00:40if we expand the underlying entity all
00:42classes that inherit the entity expand
00:44as well no one here is saying that
00:46that's the wrong way to do this this is
00:48perfectly fine if that's all you need
00:50but let's dig a bit deeper the first
00:52solution has one big problem and that's
00:54modularity let's say that we want to
00:56have an entity Warrior and an entity
00:59healer Warrior can attack and healer can
01:01heal but now we want to add an entity
01:04Paladin that can heal and attack with
01:06more and more complexity you'd have to
01:08create different subsets of entities for
01:10each possible scenario that's perfectly
01:12fine if you 100% know what entities you
01:15need but what if we don't know that or
01:17what if we know that the entities can
01:19change during development we don't want
01:21to maintain that large hierarchy of
01:23interconnected entity relationships so
01:26what can be done with it I'm sure you've
01:28heard about it already you've been
01:30probably waiting all this time for it
01:32it's our favorite entity component
01:34system in entity component system the
01:36entities don't hold their own data they
01:39have just some sort of index telling us
01:41what the entity is so we can work with
01:43it all the data is in components these
01:45can be attached to an entity without
01:47having to bother with inheritance each
01:49entity can have a component or not so
01:51there's no unnecessary data being held
01:54by The Entity the last and the most
01:56important part of entity component
01:58system are the systems that in interact
02:00with the entities through their
02:01components let's take a look at an
02:03example so we can see what we're talking
02:05about we have an entity we'll call it
02:07player but for our engine it's entity
02:09zero this entity has a transfer
02:11component it tells us the position
02:13rotation and size of the entity the last
02:16piece of the puzzle is a movement system
02:18which takes in a transform component and
02:20moves it a bit I have a big problem with
02:24these explanations and that's ambiguous
02:26let me explain I may be a dum dum but
02:29how are entities and components supposed
02:31to be connected if you want to achieve
02:33zero waste hierarchy maybe I'm being too
02:36newb for this but just saying that it's
02:38connected doesn't translate into code
02:40very well if you're like me then I have
02:41good news for you I have written my own
02:43entity component system that works and
02:46kind of makes sense to me let's take a
02:48look at my own entity component system
02:50in which I'm trying a bunch of stuff for
02:52example this is how a component looks
02:54weird that it's bunch of list right well
02:57from what I understand CPU likes it more
02:59when the data in memory is in lists of
03:01simple Primitives it should lead to
03:03better performance but I haven't done
03:05the testing yet it just looks fun to me
03:07let's take the same example but I'll
03:09actually show you how it would work in
03:11my engine right now as it structured you
03:13might like it you might hate it it might
03:15be completely wrong but hey it works
03:18right now okay we have an entity with an
03:20ID and a component musk it's just a list
03:23of enems that set the components that
03:25the entity has so we can check it faster
03:27than searching through the components
03:29themselves let's add a transform
03:30component which is just a container for
03:32a bunch of lists holding the transform
03:34data for all entities that have it now
03:37how do we say What entity has what
03:39component the simplest dumbest easiest
03:42way with a entity ID to index in the
03:45list I know that there won't be that
03:46many components so for now I just have a
03:49container for maps mapping the entities
03:51to their component indexes in the
03:53component containers if we want to add a
03:56component to The Entity we just need to
03:58add a new value into the component list
04:00add the correct value into the entity's
04:02component mask and add a pair entity ID
04:05to the last index in the list into the
04:07mapping now that we can find out what
04:09entity has what component using our
04:11mapping we can add our movement system
04:13the movement system holds all the
04:15indexes in the transform component in a
04:17separate list of indexes so we can
04:19simply and easily Define what components
04:21should move it has a few functions like
04:24add and remove entity which just put the
04:26component indexes into the list of
04:28working components so so that we can
04:30simply look through it access the
04:31components and modify them if necessary
04:34for our case we'll run a for Loop
04:36looping through the components we want
04:37and modifying them now is this the best
04:40implementation of course not but it's
04:42one that my brain came up with and it
04:44suits my needs for now so I'm incredibly
04:46happy with it it's just for inspiration
04:49you don't have to use it but maybe now
04:51entity component system makes a bit more
04:53sense to you and to me as well if you
04:55don't want to write your own ECS in C++
04:58I've heard great thinks about the
05:00library entt link will be in the
05:03description below I haven't tried it I
05:05just skimmed through it and it's really
05:07complex but it should have great
05:10performance thank you for watching
05:11subscribe if you want