Тёмный

Easy C++ Reflection 

Low Level Game Dev
Подписаться 36 тыс.
Просмотров 13 тыс.
50% 1

In this video, I will show you how you can easily achieve reflection in C++.
#cpp #programming
Join my Discord:
/ discord
Wishlist Midnight Arrow:
store.steampow...
Join this channel if you want to support me 😻:
/ @lowlevelgamedev9330
Source code:
github.com/mee...
stackoverflow....
stackoverflow....
Music:Evan King - Virtually Impossible
Evan King - Booty Shake Robot
Evan King - Nightmares and Violent Shapes
/ contextsensitive
contextsensiti...

Опубликовано:

 

15 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 48   
@shmuelisrl
@shmuelisrl Год назад
"when ever you have no idea how to implement something difficult my advice is to just think about how you could do it" lol 😂
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
not my finest choice of words I have to admit 😂😂
@ey6413
@ey6413 10 месяцев назад
​@@lowlevelgamedev9330XDD
@nick15684
@nick15684 11 месяцев назад
I would *_highly_* recommend you use *unions* for this. They are *_very_* memory-efficient and scale really well. Actively telling people not to use that is wild honestly, especially when it's the simple solution. That's what I used in my game engine, works like a charm. One of those rare cases where unions actually have a perfect use-case. It's like they were literally made for this.
@lowlevelgamedev9330
@lowlevelgamedev9330 11 месяцев назад
sorry if I was not clear in the video, I would encourage people to use anything they see fit and I think that unions are a good option. For that case I used a pointer because well I need a refference. The thing that I dislike is the cpp union (I think it is called variadic or soemthing)
@ThalusA
@ThalusA 3 месяца назад
@@lowlevelgamedev9330 why do you hate so much std::variant en.cppreference.com/w/cpp/utility/variant ? It serves as an alternative to a platform specific functionality but cross platform and much more well designed en.wikipedia.org/wiki/Variant_type_(COM)
@icaka5140
@icaka5140 Год назад
As a high-level language enjoyer learning low-level ones for univeristy I find this pretty interesting and entertaining, keep up the good work!
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
thank you bro 💪💪
@toksic424
@toksic424 Год назад
The editing keeps getting better and better with each upload. Very cool video!
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
thank you glad to hear feedback 💪
@sinom
@sinom 14 дней назад
Yes in this case std::variant would be a lot better than using unions or void pointers (I think this way you're doing it is probably UB)
@sinom
@sinom 14 дней назад
The # operator in macros does NOT creat a "c string" but instead a "string literal" which for most cases is irrelevant but can cause some bugs if you expect it to always behave like a c style char* string (the main two things to keep in mind is that string literals are stored in static storage and are always const, so anything that would modify the data behind that pointer is UB)
@Kitsomo
@Kitsomo 23 дня назад
Hey, this looks cool and is working for ints and floats. Could u demonstrate usage for char* and bool ? (I m writing a GUI for my game and i m trying to change the value of the addressed variable dynamically)
@coolcodingcat
@coolcodingcat 2 месяца назад
An unordered map with the variable name as the key would make it so you can look up variables by name in constant time
@uanbu6539
@uanbu6539 6 месяцев назад
don't you need to add a semi-colon after variables.push_back({}) in the DECLARE macro?
@lowlevelgamedev9330
@lowlevelgamedev9330 6 месяцев назад
good question, so I didn't add it so that the user has to add it when using declare macro, this way declare macro looks more like an orinary cpp function where you havw to add ; at the end of the statement
@diadetediotedio6918
@diadetediotedio6918 Год назад
Why "don't use variants"?
@marcs9451
@marcs9451 5 месяцев назад
std::variant is implemented using recursive inheritance of unions, the type layout is an absolute mess. a lot of it is exclusive to your STL implementation and not particularly ergonomic to use.
@diadetediotedio6918
@diadetediotedio6918 5 месяцев назад
​@@marcs9451 But you don't need to deal with the type layout, it is kind of already made, no? And, talking about ergonomics, do you think there is a better alternative than them in this sense?
@marcs9451
@marcs9451 5 месяцев назад
@@diadetediotedio6918 When it comes to (efficient) serialising you absolutely need to know the precise data layout, as you will be storing it in some binary format, you don't need to serialise std::variant as-is, but in the end you will end up writing your own variant in the form of serialisable data with a type tag + an "adapter" to std::variant. So basically you'll have duplicated work for little benefit. std::variant is just... very bad, when presented with the opportunity of having proper tagged unions (like Pascal, Rust, Odin, Zig, D, etc...) the C++ committee thought it was a bad idea and instead went with std::variant, which is just an ugly template hack. There's no better alternative in the realm of C++ here, this is a fundamental design flaw of the language that I can assure you, will *never* be fixed. What I do if I'm forced to use C++ is to just generate the boilerplate for a proper tagged union type with its own internal enum (I use a python script for that), so you can use `switch(value.kind())` and exhaustively check all data types (similar to Rust/OCaml enums). In the case of serialisation you can also use a protobuf style interface to handle reading a bunch of bytes from a file and decoding into a struct (reporting errors as needed).
@palapapa0201
@palapapa0201 2 месяца назад
@@marcs9451 Why does the implementation and layout matter unless you are doing non-standard stuff?
@yigithanion
@yigithanion Год назад
Awesome! But when i encountered this problem i couldn't figure out a way to serialize member variables in the header file, so i have to also write the serializing code for it too if i want to have a variable i want to serialize.
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
yes that is significantly more difficult to do, you probably have to use the things that I talked about at the end of the video.
@ChaseCarlson
@ChaseCarlson Год назад
🔥🔥🔥
@codcouch1
@codcouch1 11 месяцев назад
i was confused about how Assign() is called, but i guess it gets called when you set something equal like int foo = 0;
@codcouch1
@codcouch1 11 месяцев назад
nevermind, i see it gets called by std::vector
@RuedigaTheSecond
@RuedigaTheSecond Год назад
Very interesting
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
thanks 💪
@palapapa0201
@palapapa0201 2 месяца назад
This is not really reflection since you can't serialize class types. Also, please use enum classes. There is no reason not to use them.
@JFrancoe
@JFrancoe Год назад
nice video
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
thanks 💪
@theevilcottonball
@theevilcottonball 27 дней назад
Strict aliasing: Am i a joke to you?
@lowlevelgamedev9330
@lowlevelgamedev9330 27 дней назад
probably 😂😭
@marcsfeh
@marcsfeh 23 дня назад
-fno-strict-aliasing
@theevilcottonball
@theevilcottonball 23 дня назад
You can use memcpy.
@netfri25
@netfri25 11 месяцев назад
I think that using a template instead of void* would be much easier
@lowlevelgamedev9330
@lowlevelgamedev9330 11 месяцев назад
hmm I don't think you can use templates to help you much in this situation but you can try to if you want
@netfri25
@netfri25 11 месяцев назад
@@lowlevelgamedev9330 I tried, I managed to implement basic reflection using some weird templates and some of C++20 features, but it's just way too hard. I gave up :)
@carlosdalomba
@carlosdalomba 7 месяцев назад
@@netfri25That's how advanced Template stuff always goes for me in C++ 😂
@blacklistnr1
@blacklistnr1 Год назад
"C++ Reflection" 😂 PSA: I've been on that road before and there's only dead ends some after a few days and some months later. You can trick POD structs similar to this video, but any reflection beyond that is just pain. I've had better luck with code generation. Either way, if you seriously need reflection C++ is the wrong language choice.
@lowlevelgamedev9330
@lowlevelgamedev9330 Год назад
I havent used reflection with cpp so i don't quite know. You are probably right tho I still think that the things in this video are interesting to see
@lucass8119
@lucass8119 11 месяцев назад
While C++ is definitely a more difficult language to get this up and running, you can definitely achieve it with code generation. Your best bet would be writing your own preprocessor using something like clangd, and inserting code before compilation. It would be difficult, for sure, but if you were able to develop a general-purpose tool for this it would be amazing. There's already some impressive tooling for C++, but due to the nature of C++ it's more difficult. I think, if Bison and Flex were able to do it 20 years ago (albeit in C and a lot less language-aware), certainly you can achieve it using clangd. Also, QT has already achieved this with the Meta Object Compiler.
@samalocpp
@samalocpp 11 месяцев назад
I wouldn't recommend to the people to use macros, proper static reflection seems to be coming at C++26...
@lowlevelgamedev9330
@lowlevelgamedev9330 11 месяцев назад
oh yeah I am also waiting for that 😂 but they are taking too long + not everyone might be able to port to cpp26 + some libraries might not support those features (for example I wasn't able to make glm variables constexpr) and O honestly don't think macros are that bad if you know how to use them
@samalocpp
@samalocpp 11 месяцев назад
@@lowlevelgamedev9330 The problem with macros is the pollution propagation of the pre-processor: it won't understand the different contexts and just replace whatever matches with the macro identifier. Hence, it's a dangerous practice, specially in libraries, because may the people using your library don't notice a macro identifier and confidently use that w/o noticing, opening up the doors for bugs, undefined-behaviour, etc... But i understand that when some feature lacks you gotta offer alternatives, i just don't think macros are the ideal alternative, some people went down to the Clang AST level and tried to deflect some info of its ASTNodes, either from the dumped AST or from the AST Emitter itself, by modifying the source code, i think thats a better approach to this problematic, because these also offer capabilities that can't be reached even with the use of macros.
Далее
How C++ took a turn for the worse
5:03
Просмотров 297 тыс.
ROBLOX TRAND AGAIN. Part 7☠️🗿🙋🏻‍♀️
00:16
C++ Debugger for complete beginners
8:35
Просмотров 10 тыс.
My 10 “Clean” Code Principles (Start These Now)
15:12
Is it that difficult to make a game engine?
7:07
Просмотров 97 тыс.
I made the same game in Assembly, C and C++
4:20
Просмотров 753 тыс.
Rust: When C Code Isn't Enough
8:26
Просмотров 170 тыс.