Hello!
I am having difficulties trying to use an AActor derived class as data object in the GameMode class of my project.
What I am doing is declaring the class like:
class MySecondaryClass mySecClassName*
and in the GameMode constructor:
mySecClassName =
ObjectInitializer.CreateDefaultSubobject<MySecondaryClass >(this, TEXT(“Something”));
(The constructor of MySecondaryClass is default - not initialized with FObjectInitializer)
After building the solution with Visual 2013, I try to enter the editor, and at 83% it crashes.
Since I am new to UE4, there’s a big chance I am missing something.
Thanks in advance for any help or advice:D
Hello,
Please note, that FObjectInitializer::CreateDefaultSubobject is a templated function:
template<class TReturnType, class TClassToConstructByDefault>
TReturnType * CreateDefaultSubobject (UObject * Outer, FName SubobjectName, bool bTransient)
Thus, in this situation you should use it like this:
mySecClassName = ObjectInitializer.CreateDefaultSubobject<UMySecondaryClass>(this, TEXT("My custom component"));
Hope this helped! Cheers!
Oops, my bad, I forgot to put that in the question. I’ am already doing that in the code ;). Sorry!
Have you tried rebuilding the project in VS?
Yes. I tried rebuilding it in VS, it successfully builds it, but when I try to open my project from UE4 Launcher, it goes to 73~83% and crashes.
Can you please tell me, what version of the Engine are you using? Are you able to open other projects?
I can open other projects with the editor, except the bugged one. As for the version, I’m using 4.7, but not sure which one (4.7.x… ). I can find out later today when I get home which one it is.
I believe Actor classes need a world (UWorld instance) to be spawned in. Initializing it in the GameMode (which get initialized before the gameworld exists - or where it is at least in a initialization phase) will probably fail.
Please consider using UObjects for data objects. If you like to learn more, please check out this turorial:
http://thehumgame.com/tutorials-from-the-hum-1-using-c-uobjects-as-data-objects-unreal-engine-4/
Cheers!