C++ is like chemistry. You spend a respectable amount of time learning the rules and a ridiculous amount of time learning about exceptions and edge cases.
I myself program in C++ and what you did there is just amazing. Honestly you're approaching legend status in the C++ community. Keep up the incredible work!
@@KleptomaniacJames all I know is every time I post c code for feedback or help(not often I do) I am just told to use c++. So there cohesive about that.
Well done! I've been doing C++ for over a decade at this point, and even participated in the standards committee for about 5 years. Standard reflection is still an ongoing discussion (with multiple drafts and Technical Specifications, and several draft implementations), and it won't even include runtime introspection, so good on ya for not waiting until C++26 (or later! 😅). You mentioned this is on 8 or 9 months of C++ experience? It's impressive 🙂
yeah I’ve seen the proposals/technical specs for reflexpr iirc! it did look a little far off, and that’s not even to mention how long it will actually take to be implemented even if it gets added in C++26 :( EDIT: on the experience bit - I’ve been programming in C for about a decade now, so C++ wasn’t *too* strange for me to pick up
@@jdh Honestly, large features like this are usually implemented in at least one compiler before the standard is ratified, and if clang happens to get it first you'll probably be fine to use it early if you're sticking with just the one compiler. That said, the syntax has changed wildly this past year (reflexpr is out, unary ^ operator is in. Just what we needed. More ambiguous operators 😮💨). The most recent syntax paper is P2320 I believe and it just gets nutty IMO.
@@bruxisma "(reflexpr is out, unary ^ operator is in. Just what we needed. More ambiguous operators 😮💨)" The recent changes to C++ seem to be exactly of the kind that Stroustrup initially warned against - made by people in ivory towers. ReflExpr would be an okish keyword, but a new unary operator that has no current or history connection to reflections and more over has other way stronger connections? Why would they do that? Or now with pattern matching. Conceptually that is an extension of the good old switch-case. It is written similar to that, it behaves similar, it is the extension to switch that people wanted for decades, but instead of extending the functionality of switch they want to add a new keyword, with slightly different syntax, with slightly different behaviour, and the worst ever suggested replacements for a "default"-keyword that could have chosen - either "_" or "__" ...
@@ABaumstumpf The reflexpr to operator change was done because of feedback from field use, from what I understand. Why that operator? Because it's not used in that way anywhere else before and will work under non-ASCII chararcter set platforms because IBM still insists on supporting EBCDIC. An issue I certainly take with the committee, and this especially applies to "the old guard", is that if you mention "well, we want this feature because language X does this" they will vote against said feature simply because it comes from another language. This is the same committee that seems to think Javascript is a "new and young" language despite being created in 1992, only 7 years after C++'s first ever public release, and several years before the first C++ standard was ratified. Bjarne is involved with the upcoming pattern matching syntax, and is directly responsible for several of the design decisions, including the new inspect keywword. I've made several complaints about it, that simply using a different keyword from "case" would solve this "ambiguity", but it fell on deaf ears. If Bjarne ever warned about people making decisions in ivory towers, he sure seemed to forget he sits in one.
@@ABaumstumpf Using an _ for default case is fairly common in other languages and seems fine to me. It's certainly better than reserving "default" as a keyword, thereby preventing anyone from using it as a variable name anywhere. Though if you've already reserved it, you might as well use it. But yeah, the C++ committee, including Bjarne, are remarkably out of touch.
Dude, you’re actually a freaking legend. I code in C++ too; around an year ago I’d asked a question about reflection on SO. Turns out it didn’t exist. ‘Oh well’, I thought to myself and gave up on the thing I needed reflection for. But you actually adding reflection TO THE LANGUAGE. LEGEND FR.
I've been working with C++ since at least 2003 and I can say that this was quite impressive to dive into the intermediate language of the compilers. Some of the best developers and engineers are those who design their own tools! Job well done!
@@puppergump4117 I think a balance needs to be struck. I agree with the sentiment, but not the absolutist statement. There's definitely a lot of planning that goes on in coding. Its not just tapping keys on the keyboard. You draw upon your knowledge and make a decision about what to try next. And it is only after you have made the decision that you start on writing the code for that step. That constitutes planning in my opinion. You don't sort all of the possible solutions in alphabetical order and pick the first one every time. Without any planning, you wouldn't have a reason to be writing code at all. Again, I agree with the sentiment though. Planning too far ahead results in too much wasted time whenever plans change (which they often will).
Wow. I personally am a JavaScript and C# programmer, so I guess I never really thought about all the work that goes into this stuff. I'm honestly really impressed. Honestly, you're one of the best programmers I've seen in a long time. I think we could all learn a bit on your approach to problems. Keep up the good work!
Both C# and JavaScript are infinitely easier to build this in because C# has a runtime that you can inspect and JavaScript and interpreter. C++ is harder because it’s compiled not to byte code that has a runtime but native machine code. You could compare it like at runtime having C# change it’s runtime or JavaScript change it’s interpreter. Most people who were in need of reflection in C/C++ would actually create their own little run time where a structure would be either read from a header file (like I did once to know where to move within the pointer of the object) or created their own types at runtime. Like JavaScript really - then reflection is super trivial.
I've actually also written a janky reflection system too, but my method used extreme preprocessor abuse rather than anything remotely smart like templates or your method of automatically fetching data. I had a macro that took in the name of an existing class, and then a varadic collection of other macros. That main macro created another secret class with a static constructor and some metadata variables. The varadic arguments in that macro then had to be yet more preprocessor macros which were primarily used to list off fields that could be found in the actual class, as well as give them metadata. Through abuse of static constructors (Which ran before main() ), and the typeid(xyz).name(), offsetof_s() and sizeof() functions, as well as just horrific abuse of casting, I had a workable reflection system that met the needs of the project, with each macro essentially building up part of an internal type database that will be fully populated with all relevant types once main() itself runs. Since typeid().name() gave me a string name of the field's type, I could then also slot some type converters into a hashmap to handle serializing/deserializing anything that wasn't an int and void* casting it into the class instance at the right spot thanks to my knowledge of the field offsets and sizes. Later on, the field definition macros even let me add extra metadata that let me add networking support by marking replicated fields, from which a subsystem would grab, compare and generate packets automagically! It even sort of worked! Ah, university... Got to love being given a complex 3D project where the only library you're allowed to use is DirectX and yet it needs a load/save system... No idea if my project would have worked on any compiler other than MSVC, but it got me a high grade, that's for sure.
That is the default approach for most serialization libraries. You just put a bunch of macros, and it generates the necessary code to serialize and deserialize it.
I made my own compile-time typeid().name() because I wanted to (it was for listing arguments of commands for a command processor thing I was working on, which was also 90% compile-time abstractions). I had to do some black-magic template stuff with pointer type parameters, static constexpr locals, and the builtin __FUNC__ macro. What a pain. I also added support for including type specifications/qualifiers/modifiers in the resulting string.
I went with the parser approah for my serialization needs. It worked relatively quickly for the only reason that I only parse header file, and I use the double square brackets to know what to parse [[custom_tag]]. I'm amazed that you went through the compiler route, because when I looked at it, oh boy it was scary. All and all, it seems we (read gamedevs) need a better, more suited langage for games. In the meantime, I guess we can still use C++. Also, it's another banger video. Good job!
@Daryl Barampanze I did something similar, but instead of parsing headers, I stored all struct definitions in XML and generated headers and (de)serialization functions from it. It was easier that way for me, but it depends on the ratio of data to logic.
in rust, this entire serrialization/deserialzation is 1 library and a single line of code for each structure that you want to be able to serialze/deserialize. and you have over a dozen choices of formats to serialze/deserialize to/from
@@l3gacyb3ta21 yea this is the point I would give in and pivot to Rust for its beautiful Serde library. reflection in Rust isn't perfect - you (as a library dev, not a *user* of tools like Serde) literally have to go through a crate that parses Rust code from within Rust itself if you want any codegen because the language designers thought Lisp-style homoiconicity would be a good way to do metaprogramming in a language with syntax severalfold more complex than Lisp - but this is the point I would rather consider rewriting the game than creating an entire compiler plugin. low-level Clang/LLVM stuff is positively ghastly
What would be useful to have in your serialization is to address an occasion when you have a class, and some time later in the development you change field names or datatypes. After that old save files will be unusable. You can serizalize some version number for each type, say by default each version 1, then for each changed class you increments it version. Then in your code you check if version for a class from a save file matches your source code class version, and if not you could implement some per-class conversion function to keep backward-compatibility.
that's exactly the plan :) the serializer can also serialize a schema this way (names/hashes of field names and class bases in the order in which they were written) to allow for smooth upgrades.
Let's see... *Builds an OS from scratch *Has an affinity for old-style cpu's *modifies a popular programming language to suit his needs You're becoming more and more like Terry Davis with every passing day.
@@Perseagatuna He needed a language that could be used without a POSIX runtime. I'm currently building a non-POSIX runtime in C++, but since he also needed a new compiler that runs on his exotic kernel anyways, I guess he decided why not make a new language for it too.
All this to catch an obscure and very rare instance that **MIGHT** happen if the user spills liquid on a specific part of his keyboard, while performing an ancient ritual to summon a spirit that could, potentially, force the RNG gods to grant a special favor when farming a specific item in a completely and unrelated game. The art of code efficiency, sometimes, can take you to a very dark place.
In one of those ironic life moments, i actually just spent a month working on a reflection project in c++... and im genuinely impressed you created an even more convoluted solution. Brilliantly done!
I went back and forth on reflection for the past year and a half precisely for serializing components in my tiny game. At first I did it all by hand, but it became too much repeated code (serialize in, serialize out, add to the imgui inspector...) so I said to myself, it can't be that difficult to do it automatically... Well, down the rabbit hole I went. I looked at all the macro approaches first, and left it like that for a while. Then tried the clang library but was too intimidated by it. Now, since I only need reflection for component data, which should be literal types, I use something similar to the PFR library which decomposes them into structured bindings which can be used like a tuple. You can iterate over them while compiling and save/load everything. The only missing part were field names, so I wrote a quick and dirty python script that generates a header that is optionally included where the components are declared and creates an array of names that you can use in the reflection library. It's not perfect, but it's the best I could do and actually I'm really happy with how it works now for my needs, specially compared to other previous solutions. However, I must say, what you did here is black magic. Knowing firsthand the hurdles of implementing reflection, this was amazing to watch. And everything you accomplished is seriously mind blowing (as are all your c++ experiments to be honest). So wow, thank you so much for making this incredible videos!
I've enjoyed C++ with reflection for 15 years or so now -- Qt's "moc" (meta-object compiler) generates reflection data for classes. I used this back in about 2008 to build a tool to transparently connect signals to methods on a remote instance over a network connection. More recently I've also used it to automatically bind C++ classes to Lua, to allow scripts to control C++ software. And in another project, moc-based reflection lets me expose methods as web API endpoints.
I've been programming "professionally" (getting paid for it lol) in C++ for a bit over two years now, I really like this horrible language because every single day I learn something and I never stop feeling like a beginner, when you said you look at code of programmer 10 times better than you I realized I am indeed a beginner still lol, amazing channel my dude and amazing work.
man this was so overkill for what you're planning to achieve. and i mean this in a good way. i honestly admire the sheer amount of willpower to sit thru this, learn the inner workings of the how the language works. i thinks its great what you're doing and you should keep doing this. if you were able to reach this level of divinity in c++ in just 7 month, i cant even imagine what you'll do in couple of years.
Serialization by reflection is only cool on paper in my experience because it adds a big headache on top of the already existing headache that is migration / data layout changes between versions. thats why i prefer explicit serialization amd for the reflection part i usually just use an enum based type registry.
Don't forget that if you incorporate a library that is GPL (even v2) and distribute it (freely or commercially) you need to make available to the end user any change you made to the source code.
Wrong, only the original source would be under GPL. This is why many companies are able to use GPL’d code with their stuff strapped on top.. it doesn’t make the whole file GPL’d. Would have to make the original GPL’d code available upon request though.
I appreciate that you aren't afraid to show the technical side of game development, and really try to explain your code. I think too many tech related channels really try to hide it.
I salute this level of insanity! My solution to this was just to store the game state in a single struct and then I can do a single file read or write to save or load. I really wish C++ would add reflection, every now and then I want to do something that turns out to be completely impossible.
This was the magic behind Warcraft's save system. Most games had really long load and save times back then (and today, _especially_ if you account for the difference in drive speeds); Warcraft instead had the game state as a single contiguous state in memory that could easily be read or written in a single block. Unfortunately, for Starcraft, they decided to switch to C++... :D It's especially funny when you consider that many game design patterns today lend themselves pretty handily to this approach - coming back to the good old "keep data separate from logic".
@@LuaanTi It's not like you couldn't do it the C way just because you're using C++... (unless you're relying on a parser, but you guys are talking about the total opposite solution).
@@zvxcvxcz It's a bit of a problem if you're using inheritance (and pointers, which Warcraft avoided). Starcraft had to deal with the same problem and they ended up having to remap all the pointers before each save and after each load - which was of course _very_ slow and error prone. Starcraft was buggy as hell :D Of course, you _can_ use as little C++ as possible. And you can still separate data from code in C++. It's just that C++ was kind of built on the exact opposite mindset.
@@LuaanTi That works as long as you don't try to transfer data between architectures. Granted, nowadays many/most PC (not necessarily Windows, just non-console) games are built for x86, including on Mac, but if that save file was copied to, say, an older Mac system, suddenly all your multibyte values are gibberish.
I love this solution. Re-inveting the wheel is somewhat frowned upon by programmers, but I think it should be the opposite, it's precisely what's necessary to get a deep understanding of things.
This is dope. Plugins are crazy powerful and crazy tedious so good on you. Not sure if you’ve seen the circle compiler… tbh I haven’t looked into it much and it of course doesn’t have the backing of the llvm/clang community, but I believe the madman that made circle implemented reflection
It's funny, you're a big inspiration to me in terms of skill but it's also intimidating to see just how far away I am. I still need to learn so much to be able make the software that I dream about making.
Careful: reflection-based serialization is somewhat fragile. Half the work in serialization libraries is making sure you can still load a file from several versions ago, and you still can't really do that automatically. You need to bear in mind backwards compatibility. For example, I add a new field to an existing structure, but then load an old gamesave from before I added that field. Do I have a default value? Does it make sense?
About three months into learning c++ I started writing a gui program to display all the little code snippets I'd written. I wanted the program to run the code in one frame and display it simultaneously in another. I spent about a day trying to figure out how to use reflection to do this, asked about it on stack overflow and then promptly realized it was way beyond my skill level and so copied all my code snippets into strings... it works great.
That’s awesome!! I love when folks are able to go “there’s surely an easier way to do this…” and then roll-out a completely custom system to accomplish what they’re looking for. This “meta” programming is incredibly powerful and I love seeing it when it takes place. Thanks for such a well-documented version! :3 Is this something like Unreal Engine’s reflection system under the hood? Or is this this completely different :o
This is such an insane amount of work to do something you'd get for free if you started in C# or Java or Python or something. I salute your dedication to insanity
Anything that has to do with parsing the mountain of syntax that is C++ is an absolute feat of software engineering. You have my respect. 40 years of stacking features on top of features have made it virtually impossilbe for a single person to know the full syntax of the language.
Crazy skills needed to pull this off in C++. At university way back in the 90s I did make a C-interpreter in a compiler course using lex and yacc. That was pretty fun as I could modify my code in real time as it was running to experiment myself towards the output (but often messing up the data in the process), C is way simpler though so making reflection support for that would have been way easier for sure.
Ooh! I did this too, but for typescript which was waaay easier. I don't think I would have done this for C++ due to it being too fragile. So I have reflection for serialising data but it's purely runtime (and much easier because I define specifically how to save each type, I just made it so I only have to decorate types to make them save/load properly). I also have a compiler plugin that adds proper mixin support. So I feel like I've done both sides, but in easy mode.
I'm in the process of writing a remote DWARF debug info server/parser. Stack unwinding wasn't too bad even if I couldn't use libunwind's built-in remote unwinding for... reasons. Then came the part of parsing actual debug info so I could see where and how variables are stored and... Your comment about edge cases upon edge cases is soo true for this as well 🤣
I hear more recent programming languages like Zig let you do this sort of metaprogramming very easily, and at compile time rather than runtime. Even in C/C++, I've seen people successfully solve this exact problem with code generation (writing C programs that _literally_ write out C programs). This avoids having to update serialisation code manually every time e.g. an entity gets an extra field added, but without having to massively bloat your data structures with runtime type information (which is what I understood to be your approach). Either way, very entertaining video as always.
I had a similar but less successful story 20 years ago. I was making a game in C++ and wanted to save & load all the different objects. The system I built first was inflexible - any time I added a member variable that needed saving, any old files would no longer work. So I thought I'd try saving everything as the code needed to recreate everything. I tried making a scripting system for this purpose but took ages and got nowhere. Then I found an existing open source C++ scripting system, improved it with some asm to do the function calls I wanted. I used plenty of macros to shorten all the things I was doing in every class. These days I use JS and other modern languages. I'm throwing JSON data around all the time. Luxury!
JSON data is gross, not a luxury. Anyway, in the interim we have gained some obvious robust solutions, e.g. there is good serialization in the Boost libraries now, but 20 years ago... you were just like 2-3 years too early.
I've not followed it In a while, but c++23 was supposed to get reflection. At least it was one of the more popular proposals when the c++23 spec was first being announced. But this is awesome and truly amazing work, looking forward to what you do next
I'm genially impressed by your approach, dedication and knowledge. All that said, I don't think writing compiler extension it the best idea :) I think one of the approaches you could take is just use some interaction with Rust, because it's already has decent serialization without much of boilerplate. Also for my personal needs I implemented somewhat similar to Unreal Header Tool, which just scans file for macros/tags and then do runtime serialization, so you not writing whole C++ parser, but a small part of it. I did a very very basic stuff, so only few types and containers are recognizable, it doesn't work well with templates and custom spacing/aligning etc, but I think you can make it pretty generic too.
I do this in C, but I just generate my structs with the preprocessor, which allows me to also generate a reflection database. This allows me to easily iterate over the fields of a struct, query their names and types and call serializers and deserializers on them. This entire system is under 100 lines of code and completely standards compliant, so it'll work on clang, gcc and MSVC.
you should take these detours when writing code! if your goal is to learn and become a better dev, you shouldn't be ashamed of them. you learn a lot more stuff and it's *way more* fun to write code and contribute that way, leading to you becoming a better dev. keep it up jdh!
I wonder, could you take a snapshot of the memory as a dump of sorts, and then allocate that same state again? Another idea could be the use some sort of custom base class or template that could record all needed data. Perhaps with a touch of boilerplate code generation. Not saying every language couldn't use proper reflection features, especially cpp. Anyways, very impressed that you got it working, well done!
This is, by far, the worst, most convoluted, most unmaintainable c++ solution for anything I've ever seen... and it's beautiful. I have no words. They should have sent a poet.
My kind of person. A few months ago I wanted to make an audio processing program because the current ones werent good enough for me. Long story short, as a direct result, im now trying to make my own little x86-64 desktop operating system from scratch.
I've built a C++ parser from first principles before. It was... hard. Very hard. But it parsed its own source code, including the STL includes! It would have eventually become a complete compiler, but the "C++ Grandmaster" class I was taking fell apart because the instructor ghosted.
I have actually looked into this approach for my own project, but decided against it due to platform incompatibility. Since this reflection system is inherently tied to CLang, you cannot use it with, for example, MSVC, which i needed for multi-platform support. I instead made a templatized reflection system that generates type names and some basic information at compile-time from within C++ itself, and then you can explicitly reflect certain additional aspects of your types such as attributes, inheritance, functions, etc. at runtime during plugin initialization
you can actually use this on systems which clang doesn't support - the plugin then will just emit source code which can then be used by the target compiler just as any source would be included in the final build
@@jdh That is true, but that still means that you cant just compile with MSVC from the start. I'm making a game engine so I can't really force the user to run CLang plugins and *then* compile with MSVC :p Also personally, I am just not a fan of external tools for codegen :P, which is why i went with compile-time generation of reflection data. You can actually do a lot of similar reflection stuff at compile-time with templates, like using `__PRETTY_FUNC__` + some lightweight constexpr parsing to generate a demangled string name for any type, generating casts for integral and floating-point types, enum to underlying type casts, type-erased range and tuple-like type binding, type-erased string-like type binding, etc.
If you want to do some weird stuff like that, you might want to consider trying the Scopes programming language. It also has compile time execution. I wonder, if it's possible to create a similar system for scopes without hacking the compiler. Scopes even caches compiled functions into object files.
I really would like a good reflection-extension for C++, but as we also needed to serialise out data (in multiple different formats) we opted for sourcecode-generation for the data-classes. It also has to work together with the database, so now we have code that generates database-sourcecode, which is then used to generate C++ source for the data-classes (and you can also configure them via an xml). And for serialisation we then have an xml/xsd-combo that generates the mapping/marshalling classes for us. The benefit over reflection is that we could just easily change the generated source by hand and we have full IDE support once the classes are generated (with other languages that offer reflection it can be a real pain - if you ever have to debug java and can not find a single functioncall in the entire source cause the method-names are dynamically dispatched -.- ).
I did something similar but for C. I added a NotNull macro to C and then made a python script that uses libclang to check at compile time if a pointer is known to be able to be NULL when it's passed to a function or used. Libclang is pretty cool.
still waiting for jdh to make a new language, with a hand written full compiler and adding it to the game engine, oh and i have a wild idea for the language, i'm sure you have one too
Risks .. compatibility over code changes? - does the seralizer emit to somethign like json or whatever, that uses simple named key/value/recursives, or is it just dumping out binary of the objects in question? which is to say .. do you have the risk that over time, if you release updates to the game, will it invalidate their existing save stuff since it was autogenmerated to match a certain set of code, and now the code coudl be significantly different, but you'd want the saves to be compatible?
I'm also curious about this. If a compiler update can break it, or I can't transfer data between different types of computers this becomes much less useful...
Object de-serialization from a human-readable format is actually quite an interesting topic, as you basically have to write a complier for a non-turing-complete language -- the savefile.
Good lord... This is giving my PTSD of when I worked with C++ xD The absurd complexity of everything yet the surprising lack of "common" features and those compile times... The compile times especially hit me hard, after getting used to regular C, where quite often I'd just recompile whole projects while testing, just because it was fast enough to not bother me. Was also WAY less error prone to the kind of wacky compilation anomalies only C/C++ have (as far as I know).
On the one hand: wow, serious respect, that's an impressive piece of work! On the other hand: if you ever find yourself botching the compiler of a language to fudge in a feature from another language the chances are you're doing it wrong. Better to learn how these things are done in C++ and getting used to these methods that import your familiar way of doing it. If C++ is the language you plan on sticking with, then you need to become familiar with its ways.
This comment describes exactly how i feel. A language is a tool and he tried to use his for something it wasn't meant to do. This sets a terrible precedent and makes it look overly complicated for aspiring C++ developers.
@@LewiLewi52 The C++ Standards Committee is already working on adding this feature to the language in an upcoming version down the road, I wouldn't say it's "not in the spirit of the language".
Necro'ing this to be a semantics stickler. How is it "wrong"? I understand the reasons to follow conventions and the ways of the language, but truthfully I believe "wrong" is funnily enough the wrong word to use here. I see programming languages as a means to accomplishing a goal, no particularly wrong way to utilize them. Are there more efficient ways to serialize his data in C++? Maybe and, honestly, probably, but it doesn't make it an improper usage of the language. I find what he has done to be quite comparable to PHP. Lerdorf took HTML and added to it so it would suit his needs for his personal website; he made alterations to a language with a preexisting specification to suit his needs. Personally, I find nothing wrong with that (other than the fact that he created PHP, I have endured far too much agony working with that monstrosity). At the end of the day, his project gets compiled down to machine code, c'est la vie
Why am I even here? I mentally checked out like three videos ago. I have no idea what's going on. So why am I still so captivated and entertained by this?
Before I start waffling: I guess you didn't have to bother handling anything too weird (like SDL objects, or whatever graphics interface you're using) as you only want to save game data, so I'm guessing you can ignore pointers to external library stuff (more weird than STL) ? Or did you have to write edge case stuff for all those too ? Now to the waffle: Genius ! I looked at the LLVM backend thinking it'd be a great tool to write my own language, but then I decided I didn't have the time (the skill or the insanity). C++ is the first language I learnt (I hesitate to say learnt, does anyone know all it's knoocks and crannies) , it has made learning everything else real easy.. Anyway some fantastic craziness there, delivered with perfect understatement.. well done !!
I've been waiting for your video where you recreate the entire universe and make a computational machine out of photons and gluons, but this'll work for now.
I only understood about 10% of this video, but my janky over engineered code sense was going off the whole time and I know you did some cool, hacky stuff there.
I don't see a huge problem with manually implementing a serialization api for your class. You can even serialize recursively, where the serialization method of one class calls the corresponding serialization method of a manually specified set of non-primitive member variables. Though less generalizable, manually writing serialization code is almost guaranteed to be more stable than hacking the compiler.
C++ is like chemistry. You spend a respectable amount of time learning the rules and a ridiculous amount of time learning about exceptions and edge cases.
Wow, that’s an amazing metaphor
So its basically the French of programming languages?
@@___aZa___ perhaps it's the English of programming languages
@@Snail5008 Nah, French.
@@Ozzianman Fr*nch
I myself program in C++ and what you did there is just amazing. Honestly you're approaching legend status in the C++ community. Keep up the incredible work!
@ThinkHam in no way practical or reusable but nonetheless badass
I find myself doubting the existence of a cohesive c++ community
@@KleptomaniacJames all I know is every time I post c code for feedback or help(not often I do) I am just told to use c++. So there cohesive about that.
💀
@@nickgennady "Hi, I want to C!"
- But have you tried C++ yet?
Messy or no, the fact that you managed to set a goal and achieve it with such unusual means is much more impressive than you give yourself credit for.
Watching this video made my will to learn C++ go from 7 to 0.
That's a bit like watching Rallye and it depleting your will to drive
Dont let it discourage you, what he did was complete overkill and something you should never do anyway.
If anything, this encouraged me to learn more
@@user-ry4ip9ps9x cuz youre gey
That's a good thing. Learn Rust instead, it's a much more sane language.
Well done! I've been doing C++ for over a decade at this point, and even participated in the standards committee for about 5 years. Standard reflection is still an ongoing discussion (with multiple drafts and Technical Specifications, and several draft implementations), and it won't even include runtime introspection, so good on ya for not waiting until C++26 (or later! 😅). You mentioned this is on 8 or 9 months of C++ experience? It's impressive 🙂
yeah I’ve seen the proposals/technical specs for reflexpr iirc! it did look a little far off, and that’s not even to mention how long it will actually take to be implemented even if it gets added in C++26 :(
EDIT: on the experience bit - I’ve been programming in C for about a decade now, so C++ wasn’t *too* strange for me to pick up
@@jdh Honestly, large features like this are usually implemented in at least one compiler before the standard is ratified, and if clang happens to get it first you'll probably be fine to use it early if you're sticking with just the one compiler.
That said, the syntax has changed wildly this past year (reflexpr is out, unary ^ operator is in. Just what we needed. More ambiguous operators 😮💨). The most recent syntax paper is P2320 I believe and it just gets nutty IMO.
@@bruxisma "(reflexpr is out, unary ^ operator is in. Just what we needed. More ambiguous operators 😮💨)"
The recent changes to C++ seem to be exactly of the kind that Stroustrup initially warned against - made by people in ivory towers.
ReflExpr would be an okish keyword, but a new unary operator that has no current or history connection to reflections and more over has other way stronger connections? Why would they do that?
Or now with pattern matching. Conceptually that is an extension of the good old switch-case. It is written similar to that, it behaves similar, it is the extension to switch that people wanted for decades, but instead of extending the functionality of switch they want to add a new keyword, with slightly different syntax, with slightly different behaviour, and the worst ever suggested replacements for a "default"-keyword that could have chosen - either "_" or "__" ...
@@ABaumstumpf The reflexpr to operator change was done because of feedback from field use, from what I understand. Why that operator? Because it's not used in that way anywhere else before and will work under non-ASCII chararcter set platforms because IBM still insists on supporting EBCDIC.
An issue I certainly take with the committee, and this especially applies to "the old guard", is that if you mention "well, we want this feature because language X does this" they will vote against said feature simply because it comes from another language.
This is the same committee that seems to think Javascript is a "new and young" language despite being created in 1992, only 7 years after C++'s first ever public release, and several years before the first C++ standard was ratified.
Bjarne is involved with the upcoming pattern matching syntax, and is directly responsible for several of the design decisions, including the new inspect keywword. I've made several complaints about it, that simply using a different keyword from "case" would solve this "ambiguity", but it fell on deaf ears.
If Bjarne ever warned about people making decisions in ivory towers, he sure seemed to forget he sits in one.
@@ABaumstumpf Using an _ for default case is fairly common in other languages and seems fine to me. It's certainly better than reserving "default" as a keyword, thereby preventing anyone from using it as a variable name anywhere. Though if you've already reserved it, you might as well use it. But yeah, the C++ committee, including Bjarne, are remarkably out of touch.
Dude, you’re actually a freaking legend. I code in C++ too; around an year ago I’d asked a question about reflection on SO. Turns out it didn’t exist. ‘Oh well’, I thought to myself and gave up on the thing I needed reflection for. But you actually adding reflection TO THE LANGUAGE. LEGEND FR.
I've been working with C++ since at least 2003 and I can say that this was quite impressive to dive into the intermediate language of the compilers. Some of the best developers and engineers are those who design their own tools! Job well done!
yep, this will totally work in the long run and wont cause any problems at all...
my thoughts exactly
Code's not about planning for the future, it's about fixing for the now
@@puppergump4117 I think a balance needs to be struck. I agree with the sentiment, but not the absolutist statement. There's definitely a lot of planning that goes on in coding. Its not just tapping keys on the keyboard. You draw upon your knowledge and make a decision about what to try next. And it is only after you have made the decision that you start on writing the code for that step. That constitutes planning in my opinion. You don't sort all of the possible solutions in alphabetical order and pick the first one every time. Without any planning, you wouldn't have a reason to be writing code at all. Again, I agree with the sentiment though. Planning too far ahead results in too much wasted time whenever plans change (which they often will).
@@Elrog3 They were making a joke about bad programming 💀
Wow. I personally am a JavaScript and C# programmer, so I guess I never really thought about all the work that goes into this stuff. I'm honestly really impressed. Honestly, you're one of the best programmers I've seen in a long time. I think we could all learn a bit on your approach to problems. Keep up the good work!
well, for JS you have... JS Object Notation :^)
Javascript? You poor soul. I hope God will some day have mercy with you.
@@graealex fr
Both C# and JavaScript are infinitely easier to build this in because C# has a runtime that you can inspect and JavaScript and interpreter. C++ is harder because it’s compiled not to byte code that has a runtime but native machine code.
You could compare it like at runtime having C# change it’s runtime or JavaScript change it’s interpreter.
Most people who were in need of reflection in C/C++ would actually create their own little run time where a structure would be either read from a header file (like I did once to know where to move within the pointer of the object) or created their own types at runtime.
Like JavaScript really - then reflection is super trivial.
@@graealex JS is a great language and I want to use it in the future
OH MY GOD HE POSTED
I've actually also written a janky reflection system too, but my method used extreme preprocessor abuse rather than anything remotely smart like templates or your method of automatically fetching data.
I had a macro that took in the name of an existing class, and then a varadic collection of other macros. That main macro created another secret class with a static constructor and some metadata variables. The varadic arguments in that macro then had to be yet more preprocessor macros which were primarily used to list off fields that could be found in the actual class, as well as give them metadata. Through abuse of static constructors (Which ran before main() ), and the typeid(xyz).name(), offsetof_s() and sizeof() functions, as well as just horrific abuse of casting, I had a workable reflection system that met the needs of the project, with each macro essentially building up part of an internal type database that will be fully populated with all relevant types once main() itself runs.
Since typeid().name() gave me a string name of the field's type, I could then also slot some type converters into a hashmap to handle serializing/deserializing anything that wasn't an int and void* casting it into the class instance at the right spot thanks to my knowledge of the field offsets and sizes. Later on, the field definition macros even let me add extra metadata that let me add networking support by marking replicated fields, from which a subsystem would grab, compare and generate packets automagically! It even sort of worked!
Ah, university... Got to love being given a complex 3D project where the only library you're allowed to use is DirectX and yet it needs a load/save system... No idea if my project would have worked on any compiler other than MSVC, but it got me a high grade, that's for sure.
That is the default approach for most serialization libraries. You just put a bunch of macros, and it generates the necessary code to serialize and deserialize it.
I made my own compile-time typeid().name() because I wanted to (it was for listing arguments of commands for a command processor thing I was working on, which was also 90% compile-time abstractions).
I had to do some black-magic template stuff with pointer type parameters, static constexpr locals, and the builtin __FUNC__ macro. What a pain. I also added support for including type specifications/qualifiers/modifiers in the resulting string.
I went with the parser approah for my serialization needs. It worked relatively quickly for the only reason that I only parse header file, and I use the double square brackets to know what to parse [[custom_tag]].
I'm amazed that you went through the compiler route, because when I looked at it, oh boy it was scary. All and all, it seems we (read gamedevs) need a better, more suited langage for games. In the meantime, I guess we can still use C++.
Also, it's another banger video. Good job!
have u tried rust? /genq
Yes rust!
@Daryl Barampanze I did something similar, but instead of parsing headers, I stored all struct definitions in XML and generated headers and (de)serialization functions from it. It was easier that way for me, but it depends on the ratio of data to logic.
in rust, this entire serrialization/deserialzation is 1 library and a single line of code for each structure that you want to be able to serialze/deserialize. and you have over a dozen choices of formats to serialze/deserialize to/from
@@l3gacyb3ta21 yea this is the point I would give in and pivot to Rust for its beautiful Serde library. reflection in Rust isn't perfect - you (as a library dev, not a *user* of tools like Serde) literally have to go through a crate that parses Rust code from within Rust itself if you want any codegen because the language designers thought Lisp-style homoiconicity would be a good way to do metaprogramming in a language with syntax severalfold more complex than Lisp - but this is the point I would rather consider rewriting the game than creating an entire compiler plugin. low-level Clang/LLVM stuff is positively ghastly
JDH, you've made an extremely janky, bodged system here that's "temporary."
And I love you for it.
What would be useful to have in your serialization is to address an occasion when you have a class, and some time later in the development you change field names or datatypes. After that old save files will be unusable. You can serizalize some version number for each type, say by default each version 1, then for each changed class you increments it version. Then in your code you check if version for a class from a save file matches your source code class version, and if not you could implement some per-class conversion function to keep backward-compatibility.
that's exactly the plan :) the serializer can also serialize a schema this way (names/hashes of field names and class bases in the order in which they were written) to allow for smooth upgrades.
Let's see...
*Builds an OS from scratch
*Has an affinity for old-style cpu's
*modifies a popular programming language to suit his needs
You're becoming more and more like Terry Davis with every passing day.
Terry Davis utterly despised C++.
@@Cons-Cat isnt that why he made holyc?
@@Perseagatuna He needed a language that could be used without a POSIX runtime. I'm currently building a non-POSIX runtime in C++, but since he also needed a new compiler that runs on his exotic kernel anyways, I guess he decided why not make a new language for it too.
@DannyLeWasTaken I have a video on my channel named LibCat Lecture which explains the motivating fundamental problems at the start.
All this to catch an obscure and very rare instance that **MIGHT** happen if the user spills liquid on a specific part of his keyboard, while performing an ancient ritual to summon a spirit that could, potentially, force the RNG gods to grant a special favor when farming a specific item in a completely and unrelated game.
The art of code efficiency, sometimes, can take you to a very dark place.
In one of those ironic life moments, i actually just spent a month working on a reflection project in c++... and im genuinely impressed you created an even more convoluted solution. Brilliantly done!
I don’t know what I was expecting you to use to make a save system, but I definitely know it wasn’t this, incredible job!
writing a parser for c++ would be a video series of its own
I went back and forth on reflection for the past year and a half precisely for serializing components in my tiny game. At first I did it all by hand, but it became too much repeated code (serialize in, serialize out, add to the imgui inspector...) so I said to myself, it can't be that difficult to do it automatically... Well, down the rabbit hole I went.
I looked at all the macro approaches first, and left it like that for a while. Then tried the clang library but was too intimidated by it. Now, since I only need reflection for component data, which should be literal types, I use something similar to the PFR library which decomposes them into structured bindings which can be used like a tuple. You can iterate over them while compiling and save/load everything. The only missing part were field names, so I wrote a quick and dirty python script that generates a header that is optionally included where the components are declared and creates an array of names that you can use in the reflection library. It's not perfect, but it's the best I could do and actually I'm really happy with how it works now for my needs, specially compared to other previous solutions.
However, I must say, what you did here is black magic. Knowing firsthand the hurdles of implementing reflection, this was amazing to watch. And everything you accomplished is seriously mind blowing (as are all your c++ experiments to be honest). So wow, thank you so much for making this incredible videos!
I've enjoyed C++ with reflection for 15 years or so now -- Qt's "moc" (meta-object compiler) generates reflection data for classes. I used this back in about 2008 to build a tool to transparently connect signals to methods on a remote instance over a network connection. More recently I've also used it to automatically bind C++ classes to Lua, to allow scripts to control C++ software. And in another project, moc-based reflection lets me expose methods as web API endpoints.
I've been programming "professionally" (getting paid for it lol) in C++ for a bit over two years now, I really like this horrible language because every single day I learn something and I never stop feeling like a beginner, when you said you look at code of programmer 10 times better than you I realized I am indeed a beginner still lol, amazing channel my dude and amazing work.
Hey at least you can admit it's a horrible language ;)
You like that feeling, try Rust. Thing I like about Rust is getting away from garbage collection entirely.
you should learn about the difference of , and . ;)
man this was so overkill for what you're planning to achieve.
and i mean this in a good way. i honestly admire the sheer amount of willpower to sit thru this, learn the inner workings of the how the language works. i thinks its great what you're doing and you should keep doing this. if you were able to reach this level of divinity in c++ in just 7 month, i cant even imagine what you'll do in couple of years.
Serialization by reflection is only cool on paper in my experience because it adds a big headache on top of the already existing headache that is migration / data layout changes between versions. thats why i prefer explicit serialization amd for the reflection part i usually just use an enum based type registry.
I love GRPC serialisation. For me there is no other serialisation anymore
I use std::any inputs to a single `serialize()` function and a gigantic switch case. Works pretty well, and is fairly readable, if a bit long.
Don't forget that if you incorporate a library that is GPL (even v2) and distribute it (freely or commercially) you need to make available to the end user any change you made to the source code.
Wrong, only the original source would be under GPL. This is why many companies are able to use GPL’d code with their stuff strapped on top.. it doesn’t make the whole file GPL’d. Would have to make the original GPL’d code available upon request though.
I appreciate that you aren't afraid to show the technical side of game development, and really try to explain your code. I think too many tech related channels really try to hide it.
He also shows the extreme end of the technical side.
I salute this level of insanity! My solution to this was just to store the game state in a single struct and then I can do a single file read or write to save or load. I really wish C++ would add reflection, every now and then I want to do something that turns out to be completely impossible.
This is my exact thought on how to do saves, setup a file to write the save data to on exit by calling a function to write the save information
This was the magic behind Warcraft's save system. Most games had really long load and save times back then (and today, _especially_ if you account for the difference in drive speeds); Warcraft instead had the game state as a single contiguous state in memory that could easily be read or written in a single block. Unfortunately, for Starcraft, they decided to switch to C++... :D
It's especially funny when you consider that many game design patterns today lend themselves pretty handily to this approach - coming back to the good old "keep data separate from logic".
@@LuaanTi It's not like you couldn't do it the C way just because you're using C++... (unless you're relying on a parser, but you guys are talking about the total opposite solution).
@@zvxcvxcz It's a bit of a problem if you're using inheritance (and pointers, which Warcraft avoided). Starcraft had to deal with the same problem and they ended up having to remap all the pointers before each save and after each load - which was of course _very_ slow and error prone. Starcraft was buggy as hell :D
Of course, you _can_ use as little C++ as possible. And you can still separate data from code in C++. It's just that C++ was kind of built on the exact opposite mindset.
@@LuaanTi That works as long as you don't try to transfer data between architectures. Granted, nowadays many/most PC (not necessarily Windows, just non-console) games are built for x86, including on Mac, but if that save file was copied to, say, an older Mac system, suddenly all your multibyte values are gibberish.
Your breaking the laws of physics for a game that might not be released amazing
foreshadowing is a literary device used to hint at future events
Wow, this is amazing. I've written C++ for 10 years now, and would never have come up with generating code with a compiler plugin. Amazing work!
It’s like my favourite tv series posting a new episode this game dev vlog
I haven't seen anything like that in my life. This is what I call persistence, dedication, and devotion.
I think you're a genius man.
I am beyond excited for this game! I’ve always wanted a game like this!
I love this solution. Re-inveting the wheel is somewhat frowned upon by programmers, but I think it should be the opposite, it's precisely what's necessary to get a deep understanding of things.
00:23 a gamer and a philosopher! you're fully of surprises mr jdh.
doing metaprogramming in c++ with templates involved must be one of the most terrifying jobs in CS history..
Template only libraries are fun stuff :P
I love that this guy took a simple idea of saving game state, and went off the deep end with it. Kudos my crazy ass hacker brother!
This is dope. Plugins are crazy powerful and crazy tedious so good on you.
Not sure if you’ve seen the circle compiler… tbh I haven’t looked into it much and it of course doesn’t have the backing of the llvm/clang community, but I believe the madman that made circle implemented reflection
It's funny, you're a big inspiration to me in terms of skill but it's also intimidating to see just how far away I am. I still need to learn so much to be able make the software that I dream about making.
Careful: reflection-based serialization is somewhat fragile. Half the work in serialization libraries is making sure you can still load a file from several versions ago, and you still can't really do that automatically. You need to bear in mind backwards compatibility. For example, I add a new field to an existing structure, but then load an old gamesave from before I added that field. Do I have a default value? Does it make sense?
This will probably not be backwards compatible with older serialization dumps.
Unless you also added [[version=...]] tags
About three months into learning c++ I started writing a gui program to display all the little code snippets I'd written. I wanted the program to run the code in one frame and display it simultaneously in another. I spent about a day trying to figure out how to use reflection to do this, asked about it on stack overflow and then promptly realized it was way beyond my skill level and so copied all my code snippets into strings... it works great.
That’s awesome!! I love when folks are able to go “there’s surely an easier way to do this…” and then roll-out a completely custom system to accomplish what they’re looking for. This “meta” programming is incredibly powerful and I love seeing it when it takes place. Thanks for such a well-documented version! :3
Is this something like Unreal Engine’s reflection system under the hood? Or is this this completely different :o
This is such an insane amount of work to do something you'd get for free if you started in C# or Java or Python or something. I salute your dedication to insanity
lol yeah a game in python
first game to be measured in frames per minute
This is incredible. I don't have anything really productive to add, but I just wanted to say how impressive this is.
Anything that has to do with parsing the mountain of syntax that is C++ is an absolute feat of software engineering. You have my respect. 40 years of stacking features on top of features have made it virtually impossilbe for a single person to know the full syntax of the language.
Your dedication to doing things yourself instead of settling for what others have already made is truly remarkable! Excellent video!
"Babe wake up new jdh video just dropped"
Crazy skills needed to pull this off in C++. At university way back in the 90s I did make a C-interpreter in a compiler course using lex and yacc. That was pretty fun as I could modify my code in real time as it was running to experiment myself towards the output (but often messing up the data in the process), C is way simpler though so making reflection support for that would have been way easier for sure.
The more I watch this video, the more I understand, and the more I commend you for attempting something like this.
I'm rewriting all my C++ in Rust.
It's hard at first, joy afterwards.
Serialization in Rust is very easy.
Can't wait to get this game and have it break my computer due to some weird memory leak or something
Ooh! I did this too, but for typescript which was waaay easier. I don't think I would have done this for C++ due to it being too fragile.
So I have reflection for serialising data but it's purely runtime (and much easier because I define specifically how to save each type, I just made it so I only have to decorate types to make them save/load properly).
I also have a compiler plugin that adds proper mixin support.
So I feel like I've done both sides, but in easy mode.
You’re my hero if you can do this in so much short time
I haven't used C++ in years so i honestly can't remember much but i know this wasn't easy, amazing job
I'm in the process of writing a remote DWARF debug info server/parser. Stack unwinding wasn't too bad even if I couldn't use libunwind's built-in remote unwinding for... reasons. Then came the part of parsing actual debug info so I could see where and how variables are stored and... Your comment about edge cases upon edge cases is soo true for this as well 🤣
I hear more recent programming languages like Zig let you do this sort of metaprogramming very easily, and at compile time rather than runtime.
Even in C/C++, I've seen people successfully solve this exact problem with code generation (writing C programs that _literally_ write out C programs). This avoids having to update serialisation code manually every time e.g. an entity gets an extra field added, but without having to massively bloat your data structures with runtime type information (which is what I understood to be your approach).
Either way, very entertaining video as always.
I had a similar but less successful story 20 years ago. I was making a game in C++ and wanted to save & load all the different objects. The system I built first was inflexible - any time I added a member variable that needed saving, any old files would no longer work. So I thought I'd try saving everything as the code needed to recreate everything. I tried making a scripting system for this purpose but took ages and got nowhere. Then I found an existing open source C++ scripting system, improved it with some asm to do the function calls I wanted. I used plenty of macros to shorten all the things I was doing in every class.
These days I use JS and other modern languages. I'm throwing JSON data around all the time. Luxury!
JSON data is gross, not a luxury. Anyway, in the interim we have gained some obvious robust solutions, e.g. there is good serialization in the Boost libraries now, but 20 years ago... you were just like 2-3 years too early.
I've not followed it In a while, but c++23 was supposed to get reflection. At least it was one of the more popular proposals when the c++23 spec was first being announced. But this is awesome and truly amazing work, looking forward to what you do next
I'm genially impressed by your approach, dedication and knowledge. All that said, I don't think writing compiler extension it the best idea :)
I think one of the approaches you could take is just use some interaction with Rust, because it's already has decent serialization without much of boilerplate.
Also for my personal needs I implemented somewhat similar to Unreal Header Tool, which just scans file for macros/tags and then do runtime serialization, so you not writing whole C++ parser, but a small part of it. I did a very very basic stuff, so only few types and containers are recognizable, it doesn't work well with templates and custom spacing/aligning etc, but I think you can make it pretty generic too.
This game is looking amazing! I'm really looking forward to seeing it
I do this in C, but I just generate my structs with the preprocessor, which allows me to also generate a reflection database. This allows me to easily iterate over the fields of a struct, query their names and types and call serializers and deserializers on them. This entire system is under 100 lines of code and completely standards compliant, so it'll work on clang, gcc and MSVC.
You should share an example or article explaining this more.
Qt is c++, with reflection via its metacompiler. I use it for all my desktop applications where I need performance.
You did in a few weeks what hasn't been done for years by the C++ standard committee. Very impressive!!
you should take these detours when writing code! if your goal is to learn and become a better dev, you shouldn't be ashamed of them.
you learn a lot more stuff and it's *way more* fun to write code and contribute that way, leading to you becoming a better dev.
keep it up jdh!
I wonder, could you take a snapshot of the memory as a dump of sorts, and then allocate that same state again? Another idea could be the use some sort of custom base class or template that could record all needed data. Perhaps with a touch of boilerplate code generation. Not saying every language couldn't use proper reflection features, especially cpp.
Anyways, very impressed that you got it working, well done!
Omg relatable hyperfixation keep doing what u do
This is, by far, the worst, most convoluted, most unmaintainable c++ solution for anything I've ever seen... and it's beautiful.
I have no words. They should have sent a poet.
My kind of person.
A few months ago I wanted to make an audio processing program because the current ones werent good enough for me. Long story short, as a direct result, im now trying to make my own little x86-64 desktop operating system from scratch.
Who else doesn't understand tf he's talking about but just likes how calm his voice and the music is..
✋.
God, please have mercy for C++ programmers, they need to handle that instead of fread/fwrite with POD C struct :D
I've built a C++ parser from first principles before. It was... hard. Very hard. But it parsed its own source code, including the STL includes! It would have eventually become a complete compiler, but the "C++ Grandmaster" class I was taking fell apart because the instructor ghosted.
I have actually looked into this approach for my own project, but decided against it due to platform incompatibility. Since this reflection system is inherently tied to CLang, you cannot use it with, for example, MSVC, which i needed for multi-platform support.
I instead made a templatized reflection system that generates type names and some basic information at compile-time from within C++ itself, and then you can explicitly reflect certain additional aspects of your types such as attributes, inheritance, functions, etc. at runtime during plugin initialization
you can actually use this on systems which clang doesn't support - the plugin then will just emit source code which can then be used by the target compiler just as any source would be included in the final build
@@jdh That is true, but that still means that you cant just compile with MSVC from the start. I'm making a game engine so I can't really force the user to run CLang plugins and *then* compile with MSVC :p
Also personally, I am just not a fan of external tools for codegen :P, which is why i went with compile-time generation of reflection data.
You can actually do a lot of similar reflection stuff at compile-time with templates, like using `__PRETTY_FUNC__` + some lightweight constexpr parsing to generate a demangled string name for any type, generating casts for integral and floating-point types, enum to underlying type casts, type-erased range and tuple-like type binding, type-erased string-like type binding, etc.
omg this game came so far since i last watched one of your devlogs!!!!!!
This is exactly why I respect game devs. Appreciate the video being seasoned with memes💯
I think I would have written a custom serializer instead and just used reflection for property attributes, but bravo... you're a better man than I am!
i wish i could understand anything said in this video, being able to do that myself would probably bring me a lot of joy
I program in C++ and find your work incredible.
If you want to do some weird stuff like that, you might want to consider trying the Scopes programming language.
It also has compile time execution. I wonder, if it's possible to create a similar system for scopes without hacking the compiler.
Scopes even caches compiled functions into object files.
I really would like a good reflection-extension for C++, but as we also needed to serialise out data (in multiple different formats) we opted for sourcecode-generation for the data-classes.
It also has to work together with the database, so now we have code that generates database-sourcecode, which is then used to generate C++ source for the data-classes (and you can also configure them via an xml). And for serialisation we then have an xml/xsd-combo that generates the mapping/marshalling classes for us.
The benefit over reflection is that we could just easily change the generated source by hand and we have full IDE support once the classes are generated (with other languages that offer reflection it can be a real pain - if you ever have to debug java and can not find a single functioncall in the entire source cause the method-names are dynamically dispatched -.- ).
I did something similar but for C. I added a NotNull macro to C and then made a python script that uses libclang to check at compile time if a pointer is known to be able to be NULL when it's passed to a function or used. Libclang is pretty cool.
Ok this was just absolutely ridiculous and amazing thank you for blessing us with this
still waiting for jdh to make a new language, with a hand written full compiler and adding it to the game engine, oh and i have a wild idea for the language, i'm sure you have one too
This is Terry Davis. Oh, he even wrote an OS on top of that.
Risks .. compatibility over code changes? - does the seralizer emit to somethign like json or whatever, that uses simple named key/value/recursives, or is it just dumping out binary of the objects in question? which is to say .. do you have the risk that over time, if you release updates to the game, will it invalidate their existing save stuff since it was autogenmerated to match a certain set of code, and now the code coudl be significantly different, but you'd want the saves to be compatible?
I'm also curious about this. If a compiler update can break it, or I can't transfer data between different types of computers this becomes much less useful...
Really impressive, keep up the good work 💪💪
15:30 wow that was a blast from the past
Object de-serialization from a human-readable format is actually quite an interesting topic, as you basically have to write a complier for a non-turing-complete language -- the savefile.
Good lord... This is giving my PTSD of when I worked with C++ xD
The absurd complexity of everything yet the surprising lack of "common" features and those compile times...
The compile times especially hit me hard, after getting used to regular C, where quite often I'd just recompile whole projects while testing, just because it was fast enough to not bother me. Was also WAY less error prone to the kind of wacky compilation anomalies only C/C++ have (as far as I know).
Most great coders make things look easy.
JDH makes things look hard.
No hate.
Thank you jdh you inspired me to write C code.
On the one hand: wow, serious respect, that's an impressive piece of work!
On the other hand: if you ever find yourself botching the compiler of a language to fudge in a feature from another language the chances are you're doing it wrong. Better to learn how these things are done in C++ and getting used to these methods that import your familiar way of doing it. If C++ is the language you plan on sticking with, then you need to become familiar with its ways.
This comment describes exactly how i feel. A language is a tool and he tried to use his for something it wasn't meant to do. This sets a terrible precedent and makes it look overly complicated for aspiring C++ developers.
@@LewiLewi52 The C++ Standards Committee is already working on adding this feature to the language in an upcoming version down the road, I wouldn't say it's "not in the spirit of the language".
Necro'ing this to be a semantics stickler. How is it "wrong"? I understand the reasons to follow conventions and the ways of the language, but truthfully I believe "wrong" is funnily enough the wrong word to use here. I see programming languages as a means to accomplishing a goal, no particularly wrong way to utilize them. Are there more efficient ways to serialize his data in C++? Maybe and, honestly, probably, but it doesn't make it an improper usage of the language.
I find what he has done to be quite comparable to PHP. Lerdorf took HTML and added to it so it would suit his needs for his personal website; he made alterations to a language with a preexisting specification to suit his needs. Personally, I find nothing wrong with that (other than the fact that he created PHP, I have endured far too much agony working with that monstrosity). At the end of the day, his project gets compiled down to machine code, c'est la vie
Why am I even here? I mentally checked out like three videos ago. I have no idea what's going on.
So why am I still so captivated and entertained by this?
Before I start waffling:
I guess you didn't have to bother handling anything too weird (like SDL objects, or whatever graphics interface you're using) as you only want to save game data, so I'm guessing you can ignore pointers to external library stuff (more weird than STL) ? Or did you have to write edge case stuff for all those too ?
Now to the waffle:
Genius ! I looked at the LLVM backend thinking it'd be a great tool to write my own language, but then I decided I didn't have the time (the skill or the insanity). C++ is the first language I learnt (I hesitate to say learnt, does anyone know all it's knoocks and crannies) , it has made learning everything else real easy.. Anyway some fantastic craziness there, delivered with perfect understatement.. well done !!
I love understanding nothing in these kinds of videos to picking up bits an pieces
see, i think i know programming and then i watch videos like this and i'm convinced i don't know anything at all
you are a genius holy lord of c++ 🙏😭
I've been waiting for your video where you recreate the entire universe and make a computational machine out of photons and gluons, but this'll work for now.
You should add all kinds of lights and cool crafting recepies for furniture and stuff
jdh, i beg you to publish your exact theme and syntax highlighting files.
Please.
I only understood about 10% of this video, but my janky over engineered code sense was going off the whole time and I know you did some cool, hacky stuff there.
I don't see a huge problem with manually implementing a serialization api for your class. You can even serialize recursively, where the serialization method of one class calls the corresponding serialization method of a manually specified set of non-primitive member variables. Though less generalizable, manually writing serialization code is almost guaranteed to be more stable than hacking the compiler.
Your work is incredibly impressive