How I Structure Entities In My Own C++ Game Engine

Ойын-сауық

How do you structure a game engine? That's one of the first questions we as game developers have. Hopefully you'll find this video informative and maybe a bit helpful too.
ENTT C++ ECS: github.com/skypjack/entt
00:00 Intro
00:06 Entity inheritance
01:25 Entity Component System

Пікірлер: 17

  • @jchaloupka007
    @jchaloupka0073 ай бұрын

    Krásná práce! Composition over inheritance can be applied in all sorts of fields in programming, but explaining it through game dev makes it really easy and intuitive to follow and understand, you did an awesome job!

  • @funigamedev

    @funigamedev

    2 ай бұрын

    Díky moc! I'm so happy you enjoyed it, composition over inheritance is exactly what I was aiming for so I'm glad it made sense. Thank you for your feedback!

  • @harywhiteproductions
    @harywhiteproductions3 ай бұрын

    Great video and solution! I'm also writing a game engine in C++ and have been tackling a similar problem. In my one, each entity has an array of component indexes where each index in the array represents a component ID. If an entity doesn't have a certain component, the component index is just set to -1. The systems iterate through the entity IDs they are interested in and they access the component data through that.

  • @funigamedev

    @funigamedev

    3 ай бұрын

    Thank you so much! Your solution sounds great too! It's great to hear different solutions to these problems and to see that I'm not the only one dealing with this :D

  • @SianaGearz
    @SianaGearz19 сағат бұрын

    2:57 There is one tiny little aspect that makes me die inside a little - you have 3 dynamic arrays that have an invariant, that they are ALWAYS the same size, but you're left to ensure that manually. If they were to ever grow out of sync, you'd have a massive problem on your hands. Realisticaly all allocation and deallocation of entities is going to happen in a very confined section of code the correctness of which is trivially ensured, so it's not a real problem, and one might sprinkle a handful asserts in a handful of places and sleep secure that the software is correct without some crazy mechanism. But you know how it is. Also whether you want array of structures or structure of arrays... it depends! I think a Component like this is simple enough that you wil always touch at least position and rotation at the same time, so having them in the same structure (array of structures) is going to act as prefetch and be hypothetically more efficient; but likely not by any measurable amount. However size isn't like these two, there are many operations that do not involve size. So perhaps size is worth a separate Component? But yeah structure of arrays is guaranteed good enough, then size doesn't need to be a separate component, and you don't need to closely consider the operations and their relative frequencies.

  • @funigamedev

    @funigamedev

    15 сағат бұрын

    You're absolutely correct, is there a way to fix the size of the arrays? Having some way to ensure that all of the arrays have the same size without manually checking? It's also something I'm always afraid of "What if I don't delete one part of the component and then all of the indexes are wrong", so I understand the problem but I'm not sure if there's a nice solution. I've been doing a lot of refactoring lately so I can add this to my checklist of what to fix :) Also you're correct that size isn't always returned, the main way why I'm doing the whole transform is that for the purposes of rendering, for each object every frame we'll have to read all three. I'll think about it since size is different than position and rotation, for movement, physics and all that we don't really need the size. Thank you for sharing your thoughts!

  • @SianaGearz

    @SianaGearz

    10 сағат бұрын

    @@funigamedev Well unfortunately C++ doesn't directly support "Design by Contract", a technique where you tag objects and methods with an explicit sanity checking code that is always executed in a validation run. But you can use it as inspiration. Linking three arrays together via a manager that just adjusts their sizes is for one cumbersome looking and potentially inefficient for other doesn't guard against logic bugs related to managing these Components. Various people have done their own approaches to implement Structure of Arrays, you could check them out, they all have something unwieldy and potentially offensive about them.

  • @funigamedev

    @funigamedev

    3 сағат бұрын

    I'll look into it thank you!

  • @lehisluguer9300
    @lehisluguer9300Ай бұрын

    awesome video.. i love it.. but, even if ECS is super efficient and optimal.. wouldn't it be super confusing in the future?? when you have a loot of stuff your videogames?

  • @funigamedev

    @funigamedev

    Ай бұрын

    Thank you so much! You're correct that this approach will need further work to make it easier to work with, for now this is sufficient enough, but I'm already working on something better and easier to use. I don't want to introduce unnecessary complexity in the whole system since ECS is still new to me and C++ as a whole is really new, but I believe in the future there will definitely be a video on how I made it easier to work with.

  • @BigJMC

    @BigJMC

    Ай бұрын

    That’s when you make a behaviour component that holds a ptr to a scriptable object which can be used as a base class for custom behaviour. That way you can create as many scripts as you like without there being too many components.

  • @joedartonthefenderbass
    @joedartonthefenderbassАй бұрын

    Great video! I think the component mask could be a little faster if you used a bitset instead of an unordered map but I may be wrong

  • @funigamedev

    @funigamedev

    Ай бұрын

    Thank you for the tip! I'll definitely look into that, you might be right that it's faster, I'm still kind of searching for some better way to map and keep track of the entity component relationship, since maybe I want one entity to have more than one mesh and for that the current system isn't enough.

  • @Utmanarn
    @Utmanarn3 ай бұрын

    Interesting, I have stumbled upon the problem before where I have tried to make things abstract and follow proper inheritance. But sometimes it just falls flat when my entities suddenly need to have many different variables and functions from one another. It ends up with my base entity only having like 1 or two variables...

  • @funigamedev

    @funigamedev

    3 ай бұрын

    Thank you! I'm glad I'm not alone, thankfully many much smarter people than me have figured out the way already :D

  • @69k_gold
    @69k_gold3 ай бұрын

    I'm a beginner, and the video went really really fast. Can you storyboard more and make your future videos longer, taking time explaining stuff one at a time?

  • @funigamedev

    @funigamedev

    3 ай бұрын

    I'll try to look into that, thank you for your feedback!

Келесі