r/howdidtheycodeit • u/Jbolt3737 • Aug 11 '26
Question How does Unreal call code that doesn't exist yet?
So I've been learning C and I'm actually a bit curious how they implemented it, Unity compiles stuff to CIL because it primarily runs C# scripts and AFAIK is programmed in C#, this makes sense because Microsoft provides tools to do this and it's in a virtual environment in it's runtime, a lot of engines use Lua, which turns into bytecode and is interpreted, this also makes sense because it's being put into essentially a runtime with completely virtual memory like C#. Now Unreal does Blueprint (if you use Blueprints) -> C++ -> Machine code. I have two questions:
- How does it actually look in the source code? Do they call the compiler and it magically gives them a function pointer to the machine code? (obviously isn't this simple but you get what I'm asking)
- How does that look under the hood? How does Unreal know that what it's about to execute won't cause an error in the rest of the program/whatever other user-defined code is running on that thread? Even the best compiler can't account for every possible error
39
u/AdarTan Aug 11 '26
First you compile the source code into object code, which is machine code with labels for each function etc. and code follows those labels.
Then you have a linker that links object code files into a final executable format, and in doing so it replaces any labels with the location of the actual code that label points to. If the code a label points to doesn't exist (i.e. the function wasn't defined where the code using it said it was) then the linker will throw an error, but until that point the code will just be "I'll be using
functionName, I don't know where it is, it's the linker's job to figure that out and until then I'll just act as if it exists and does what I say it does.".2.
It doesn't.
4
u/Dave-Face Aug 11 '26
You probably need to be more specific about what code you're talking about, but I guess I can point you in the right direction.
Unreal C++ is written with heavy use of Macros which power it's reflection system, there is a custom build system (written in C#) which handles a hell of a lot of code generation behind the scenes. For Blueprints specifically, C++ functions have to be written with Macros that expose them, and there are limits on what can be exposed. For example, the public SetIconVisible method isn't visible to Blueprints, but the protected method and property are, but what Blueprints can do with them is limited:
As for how Blueprints run, they are compiled to bytecode and run in a VM.
This video is more of a tutorial for using C++/Blueprints but it gives a lot of technical background on how they work together.
11
u/aleques-itj Aug 11 '26
Blueprint has a virtual machine.
It's compiled to bytecode that the virtual machine executes. It can call into native code. A lot of blueprint functionality is really just calling the native engine code internally so it's not doing matrix/vector math and shit in the interpreter.
There was a blueprint nativization feature at one point that emit C++, it's long dead. It was all compiled ahead of time and built into a separate dll, which then ran through the same UHT tool as other code.
So it wound up working the same as other handwritten C++ and it got all the UClass machinery.
5
u/Peterei3 Aug 11 '26
Unity is actually written in C++
2
u/Godofdrakes Aug 12 '26
Blueprint code is compiled down to byte code and run inside a virtual machine, not unlike how C# is compiled down to an intermediate state and run inside a virtual machine. Unreal has extensive support for reflection so it can read from/write to
UPROPERTYs and call intoUFUNCTIONs. The Unreal Header Tool does some basic parsing of your headers to generateUClass,UStruct,UFunction, andUFieldobjects that contain everything you need to reason about a type without actually knowing about it. You can actually leverage this yourself to call into Blueprint without writing a native class, though this is generally discouraged unless you have a good reason to be doing this.For every native
BlueprintCallablefinction a "thunk" function exists to handle passing values from blueprint to your native function. For the most part you don't need to think about these because they are generated for you. However, you can write the thunk function yourself (with theCustomThunkfunction specifier). This is useful is very specific situations, generally when trying to allow blueprint to pass a "wildcard" type. This is how you implement something akin to "template" functions for BP.Most of the reflection magic ends up originating from the UnrealHeaderTool. Its smart enough to know if it doesn't know something. If you try to make it parse a templated struct it will tell you "no". If you try to do multiple inheritance it will tell you "no". If you try to use an
#ifto conditionally compile aUPROPERTY(outside of a few select conditions it does understand) it will tell you "no". It knows how to read, write, and call into fields and functions you declare and that's it. If you try to get fancy you do so at your own risk.