The Hot reload and Live Coding in UE5(New Live Coding)
Preface
A better hot loading mechanism has arrived
When I used to work in the past, I had a headache when writing Java, which was when there were bugs in the development business. Every time I tested, I had to close the running program and restart it to apply the new logic. But later on, Java introduced a hot reload mechanism, allowing you to directly load newly written code into the application logic without closing the running program, greatly improving development efficiency.
In UE4, Eipc also added hot reload, mainly allowing developers to compile and load newly written logic into the engine during the project encoding process without the need to close the engine editor. But you need to close the running project first and then run it again to see the added logic results.
UE5 has made improvements to this by introducing a new hot reload that does not require you to close running project projects. After adjustments are made, new logic can be directly compiled and loaded into the program, which can effectively improve development efficiency.
Operation
1、Changes in compilation methods
In UE5, the hot reload function is enabled by default, which brings new problems. If you start the engine (non debugging mode) through Rider (JetBrains Rider 2022.2.1 Build # RD-222.3739.37), the compiled code will prompt the following error:
|
|
By reviewing the log details, you can see the relevant instructions, mainly stating that the shortcut keys "Ctrl+Alt+F11" are needed to complete the compilation.
|
|
2、Use the new hot reload mechanism
For example, create a new class of Userwidget, and add a function, reflect function onto blueprint, the function is as follows:
|
|
In the engine, inherit the UMG class and add it to the viewport, and call the CallFun function through the button. Run the project, test and click the button. You will see the output log in the console
LogTemp: Hello
Note that at this point, without closing the running program, directly modify the logic inside the CallFun function in the source code, as follows:
|
|
Then use the shortcut keys Ctrl+Alt+F11 to compile, click the button again, and the console will output logs
LogTemp: Hello Jam
This is the new hot reload, you don't need to close the running program to load the new execution logic! This is very useful when modifying bugs in complex logic design!
3、Close hot reload
The method about closing hot reload is very simple, find option of "Live Coding" in the editor preferences, then uncheck "Enable Live Coding". Alternatively, you can find the settings next to the compile button in the lower right corner of the editor or close them. Please refer to the following figure for details:
After closing Livecoding, in rider, the previous compilation method can be restored, and the compilation errors will be disappeared.Summarize
When using hot reload, it is advisable to avoid the following operations as they may result in hot reload failure or become invalid:
- You can add regular functions, but avoid adding reflective functions (annotated with UFUNCTION).
- You can add regular properties, but avoid adding reflective properties (annotated with UPROPERTY).
- You can delete regular functions and properties, but you cannot delete reflective functions and properties (marked with UFUNCTION and UPROPERTY).
- You can add new UClass instances, but do not delete already instantiated UClass instances.
- You can add regular classes, but do not delete classes that are in use (instantiated).
- Avoid modifying the return type, function name, or parameter list of compiled reflective functions (UFUNCTION annotation).
- Avoid modifying the name or type of compiled reflective properties (UPROPERTY annotation).
- Do not modify the name or specifier of UClass.
- Adding new member functions (regular) may result in identifier not found errors after the first compilation, but this can be resolved by recompiling.
- You can modify the name, return type, and parameter list of regular functions. You can also modify the type and parameter names of regular properties.
Finally, a reminder: During the testing process, it was found that adjusting ordinary properties and functions may lead to errors. Overall, it is recommended to only adjust the logical part and not the structural part.
Engine version:5.2
Rider version:JetBrains Rider 2022.2.1 Build #RD-222.3739.37