In any constructor for any unreal engine class I have a problem I do not know how to handle when compiling in c++.
If I have within the constructor, a call to a function and pass a variable to that function from the constructor, I have found that when I have got everything working correctly and to my satisfaction, If I exit my session using Unreal Engine and return to continue, or I clean the solution or my project, Unreal engine crashes when I recompile or restart the editor. I have found that the problem seems to be that the Unreal engine is trying to run a function from the constructor with uninitialised parameters before they are defined in the constructor even though they are initialised before the function is called. I will illustrate with the following pseudo code example.
Class Constructor (){
variable = value;
function01(variable);
}
function01(variable){
unreal engine function(variable);
GEngine->AddOnScreenDebugMessage(-1, 1000.f, FColor::Green, FString::FromInt(variable);
}
If I try and run this when restarting the project, or cleaning the solution and project and recompiling the code, unreal engine editor will crash, or when trying to reopen…
However if I comment out the line
function01(variable);
first in the constructor, recompile, the editor it will open.
Then if I uncomment the line
function01(variable);
recompile, the editor will not crash and the code will operate as expected.
I have a suspicion this has everything to do with the hot reload of UE I have just wasted a lot of time thinking it was my code at fault until I remembered having this problem with using GEngine->AddOnScreenDebugMessage. to display variables and finding I had to comment these out before I leave a session and uncommenting them after restarting a new session, or cleaning the solution or project to continue. This is not desirable and time wasting.
So my question is, what, if anything can be done to stop the UE running functions from the constructor out of sequence of the code that is written and not pass variables without initialising them first as I have written in the code?
Or is this just an unreal engine thing and I have to comment out all function calls as I have found I have to do above to get the editor to open correctly each time I have finished a coding session and before recompiling?
I am sure this is something that is known, but I cannot find a solution online for.
Thanks in advance for any advice or help given. It would save me and perhaps others heaps of wasted time.
Regards