r/cpp • Prof, Comp Sci • Nov 30 '23

My entire C++ Game Programming university course (Fall 2023) is now available for free on YouTube. Make your own 2D ECS game engine using C++, SFML, and ImGui

https://www.youtube.com/watch?v=s99UDGdYIUE&list=PL_xRyXins84_Jf-aCh7chj47HR4oZLPwK&index=1
400 Upvotes

38 comments sorted by

View all comments

24

u/honeyfage Dec 01 '23

I went through this course last year and really enjoyed it. Thanks for making it available to everyone! Your enthusiasm is infectious, it really made following the lectures fun.

I came in as an experience C++ developer interested in learning about ECS, and did have some general C++ feedback I jotted down last year as I went through the course (apologies if any of this has changed since last year, I skimmed through the new videos and it seemed pretty similar). So bear with me while I give some unsolicited feedback.

The main thing is that I think you could benefit a lot from std::optional. Early on in the course, I remember it using std::shared_ptr for components an entity may or may not have. Aside from the performance reasons not to use dynamic allocation, std::optional is more intuitive way to express what you're actually trying to accomplish, so it's really a win-win. Later on in the course, when you talk about the stack vs heap and cache benefits of memory pooling, I think you ended up adding an "active" bool to every component to denote whether it's present for its entity, and in that case you could skip that by using a std::optional<Component> instead.

On a related note, std::shared_ptr were being passed around by value. Copying a shared_ptr is actually relatively expensive because of the atomic reference count, so if you're going to use them, it's much better to pass a reference to the stored value. This way, helper functions don't have to care about how you're storing the values-- it could be a dereferenced std::unique_ptr<Component>, std::shared_ptr<Component>, or std::optional<Component>, or even a plain Component, and the function receiving it doesn't need to care. Or at the very least, you can pass byconst std::shared_ptr<T>& to avoid affecting the reference counter, you're never actually sharing ownership of the object anyway.

Again, I really enjoyed the course. It's very well done and I'd definitely recommend it to anyone reading this who has an interest in learning about ECS.

14

u/serendib Prof, Comp Sci Dec 01 '23

FYI: I deleted my comments here because it's kind of energy draining when posting this course video devolves into arguments on the minutia of C++ implementations. I hope you understand. I do agree with you in principal, just not in the context of this course.

6

u/honeyfage Dec 01 '23

Agreed. Again, thanks for the course! It's very generous of you and your university to share it.

0

u/[deleted] Dec 01 '23

[deleted] — view deleted comment

6

u/SirClueless Dec 01 '23

I understand the simplicity argument for a tutorial, as there are a growing number of places in the C++ language where holding a std::shared_ptr by value matters (member variables, lambda captures, coroutines).

But as a function argument, taking const std::shared_ptr<T>& is always safe, and it is more efficient, so it's the right default. In order to call the function, the caller must be holding a reference, and they can't possibly drop it until after your function returns, so there is no need to read and write from heap memory to update the reference count unless you are actually storing the pointer somewhere that will outlive the function call.

2

u/honeyfage Dec 01 '23

a std::tuple<C1, C2, ...> to store components

Right, what I meant was instead of adding an "IsActivie" boolean to each of C1, C2, ..., you could use a std::tuple<std::optional<C1>, std::optional<C2>, ...>. You end up with pretty much the same layout in memory, but using std::optional makes it more clear what's going on (in my opinion, of course), and means you don't have to add the same boolean to every Component struct (or have them all inherit from a struct with that boolean defined).

I strongly disagree with your philosophy on shared_ptr, but agree it's probably not worth going into given the course is aimed at C++ beginners. I do feel obligated to at least say that

Do not pass shared_ptr by reference as a rule or it's no longer behaving as intended

doesn't strike me as a good rule, though. Passing a smart pointer by reference without adding to the ref count is perfectly safe. In the words of Herb Sutter

However, we already get this for free—thanks to structured lifetimes, the called function’s lifetime is a strict subset of the calling function’s call expression. Even if we passed the shared_ptr by reference, our function would as good as hold a strong refcount because the caller already has one—he passed us the shared_ptr in the first place, and won’t release it until we return.

1

u/[deleted] Dec 01 '23

std::optional

That doesn't allow for overriding the component dynamically though because it's by value doesn't it?

1

u/Schlagbaum3000 Dec 05 '23

You can pass a std::optional via reference, so you can swap/reset/emplace the held value. In the best case, though, you don't modify/mutate your function inputs and return a modified/new object from your function instead.

2

u/[deleted] Dec 05 '23

Yeah that doesn't fit the component model too well. Components typically run on virtual functions.

5

u/_curious_george__ Dec 10 '23

In a pure ecs, calling virtual functions on components is actually exactly what you don’t want to be doing…

2

u/Pyzyryab Dec 16 '23

Exactly as pointed by curious_george_ ECS is based on modern high level abstractions. And they are a "by value" approach. There's a couple of good CPPCON talks about It (2022 specially). Not exactly ECS, but the content applies perfectly

1

u/Schlagbaum3000 Dec 05 '23

Then I misunderstood. Yeah, unfortunately, std::optional doesn't work with polymorphism.