Entity Component Systems - My Thoughts After Making a Game with One

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to a quick little video today I'm going to talk about entity component systems and I've been kind of busy the last couple of weeks working on the action-rpg course and I couldn't quite figure out what I wanted to do today for my video I still wanted to upload a video today but I wasn't really sure what I wanted to make it on and a couple weeks ago or maybe a month ago or so I had made a small test project using love 2d and Lua which is a game engine for 2d game development and it's pretty popular it's open-source engine I think it's definitely free I'm pretty sure it's open-source and I decided to try it just for this specific structure so an entity component system is basically just a way to structure your project and it makes you think about your project in a little bit of a different way it's not really object-oriented programming anymore it's using entities instead of objects and instead of inheriting behaviors from a parent object you have components attached to your entity and those components can give you I guess data and certain behaviors so let me kind of show you I don't want to get too much into the actual code of this just because you guys probably you know you might not be familiar with love 2d or you might not be familiar with Lua and the love to the engine so that doesn't really matter a lot here I'm just going to show you kind of how the entities component system works from a general standpoint so you have your entities that make up your game objects so let's say you have a player entity and enemy entity and a laser entity and if we come into my entity code right here you can see I have a player entity an enemy entity and an enemy spawner entity and a laser and also an explosion entity okay and these entities are made up of components so basically an entity is just a list of components and components only contain data so I have player equals true so I know this is a player I have position a position component I have a collision component I have a velocity component and a health component and a sound and a sprite and a friction so these are some different components on the player and these components are reusable so that the enemy entity also has a position component and it has its own position component obviously but the logic behind creating the component is exactly the same and you can see all of my everything but the spawner the spawner doesn't have a position because it doesn't need one so you can see I've got a lot of different components here so your components build your entities you can kind of create a new entity based on components and then you have your systems and these are the things that actually make the game run because with only the entities and the components you just have data and it doesn't actually perform any logic so the system's actually contain the logic and let me show you right here so for example a really a really obvious system is the draw system this is a great system right here and I'm using a library called tiny ECS which does a lot of the work for me for for the systems but basically what I do is I create a new system and this system will only operate on entities that have a position and a sprite and the reason is because if it doesn't have a position then you won't know where to draw it and if it doesn't have a sprite you won't know what to draw and so it will only so this right here it actually won't do anything with our spotter because the spawner doesn't have a sprite or a position component so so you can kind of filter you can kind of filter your entities with your systems and this becomes really powerful because of this system because your logic is completely separate from your data so inside of my process this is the actual function that runs for the system you can see I just call love draw and I draw the entities sprite at the entities position and that's about it I just draw the entity sprite at the entities position so if I come back into my main oh that's my math let's come back into my main don't lure right here you can see these are the systems that I'm running and if I remove the draw system by commenting it out I can run my game well let me run the game with the draw system first so you guys can see it it's nothing special it's just this little game might look familiar to you guys so you can see game works fine if I remove the draw system though from the entity component system and run the game again unable to get local entities I mustn't mess something up there to make sure what I mess up library tiny I messed something up it's come in two main Lua try running it one more time there we go ok let's try this let's try this one more time comment out this remove that run the game again and that's that's my issue oh it's because I actually actually call the draw inside of here I forgot about this the draw system is a little bit specific to this project because of my view and scaling and stuff so it doesn't automatically run the draw system I have to run it myself and it's it was trying to run it but it didn't exist inside the world so here's the this is how funny this this ends up being the entire game is still running and working it's just not drawing anything so I can if you have a bug let's say you have a bug inside of one of your systems or you have a bug in your game and you're not sure where it is you can just start removing systems to try and find out where the bug is and a another funny one to remove is the movement system the movement system because it handles all the logic for moving entities so if I remove that and save I can't move anything nothing moves but everything else still works it still creates enemies it still does everything so entity component systems can create a very decoupled structure where your your game logic and your components are very separate from each other and later on when your projects get really really big it can become easier and easier to it can it can still be really easy to add new things to your project it doesn't really get harder because your system is very deep your structure is very decoupled which just basically means that your logic doesn't really connect if you if you change something in one place it doesn't necessarily mess up other things in your code and that's a very common problem that comes up in a hierarchical system with with object-oriented programming and this has its flaws too every type of structure has its flaws but I really think that this type of structure could be really useful for specific types of games and you probably want me to list some so I have I still have a I feel like a pretty limited understanding of entity component systems I've made a small entity component system but this is the first one I've ever made so it's I don't feel like I have a huge understanding of how they work yet but I can still give you kind of information based off of what I feel like I learned and what I learned was adding new features to the game was really easy to do and it never got difficult to do that I learned that a system like this you might I kind of felt like a system like this works really well for projects where you kind of want to define the rules of your game and not the objects of your game so if you if you want your game to have specific if you want your world to have specific rules that everything follows then this system works pretty well but if you want specific very unique objects because you basically you basically build your you build your objects from your entities or from your components and so you it makes it harder to create kind of you I guess everything is unique because it's a different structure of components right so it's unique in that way but if you create if you want to add something to an entity you have to create a new component which then could potentially be used by everyone which is funny I mean let's say you have an inventory component and you you attach it to your sword item so that your swords can be put inside of your inventory entity right so your your inventory system loops through inventory loops through entities that have the inventory component and it kind of does the logic whatever it needs to on them well you could potentially attach an inventory component to an enemy inside of your game and then you could put that enemy in your inventory because all the logic would work all you need to be able to put something inside of an inventory would be the inventory component and then the system's will then filter what it needs to do and put it inside of your inventory because it has that component so it's a really powerful system and I'm probably going to be using it with some of the projects that I work on this first one I made in Lua obviously just because Lua had some stuff already set up for creating entity component systems so I could use a lot of the libraries that were already created like tiny tiny ECS and that was that was useful but I think when game maker comes out with inline functions and some other like strux basically you know like lightweight objects when those kind of features that are on the roadmap when they come out creating an entity component system inside of game maker might become a lot easier and and more powerful and could be something that you could do inside of game maker so that if you made a larger project it could continue to I guess you could continue to add features with ease and because game maker seems to struggle with coupling or at least I feel like when I'm using it my projects become very interconnected and it can be difficult to work around that and and deal with the inside of game maker I'm sure there are solutions I'm sure it's possible but it is it is tricky sometimes and I think an entity component system inside of game maker could could work we could come up with something that would that would work pretty well real fast I want to show you guys to here if we come into entities again I want to show you some of the stuff that you can do by just messing with entities so we could potentially create we could potentially change this enemy so that if we removed its collision component right here on the enemy and then we run the game oh we still don't have our movement system want to add a movement system back in I did add it back in I just didn't save that was the issue okay so let's make sure I save the entity with the collision component because I think with if I remove the collision component from the entity yeah it can't it can't actually collide so let's come into our systems here and it looks like my collision system right here doesn't actually require the collision component and we want to make sure that it does require that so we'll save the game and it's still movement system friction system it is called just collision right oh it's because I have I loop through all I loop through all the entities but I have a filter right here for my loop entity dot target yes that makes sense require target and require collision I think I think I just need to require collision right there there we go yeah so there's a little bit of coupling there but if I just remove the collision it's because I didn't create I didn't make the system filter correctly this is how the system should be filtering it shouldn't do anything unless it has a collision component but it was trying to so that's really powerful because you can just remove a component from entity let's say you're getting a bug inside of collisions you know it's pretty easy because it's this is this is where the collision logic is for everything in the game well technically this is the projectile collision system so this is the collision logic for all projectiles but that's really not a lot of code to go through and you don't have to look through different objects you can just look in your systems and find it right here so anyways hopefully you guys found this video kind of interesting even if it just gets you curious about entity component systems and has you looking them up online and learning more about them I'd recommend checking them out they're pretty interesting they do seem to be from what I've learned I know there are ways to fix this but they do seem to be a little bit slower then then other systems or other structures because because there is a little bit more processing required but computers are getting so fast nowadays I I'm inclined to go for ease of use for the programmer over over how fast the system is 99% of the time there's only a very rare occasions where you actually need the extra speed so I think this is a really interesting system that you should look into and learn more about if you have questions about it it didn't quite understand something that I was talking about in the video feel free to leave comments I'll do my best to answer them and like I said I'm not an expert on these on this structure yet but I have used it a little bit and I feel like I've got the basics down now so thank you guys so much for watching this video I'll have a video coming out tomorrow that will be kind of a big announcement so keep an eye out for that and I will talk to you guys later
Info
Channel: HeartBeast
Views: 37,853
Rating: undefined out of 5
Keywords: GameMaker Tutorial, GameMaker Studio, Game Design, Game Development, Indie Game, Lua, Love2D, Entity Component System, Tiny ECS, Thoughts, Project Structure
Id: SE2Ded01lUU
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Fri Sep 22 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.