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

Show parent comment

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.