I know C++ really well, but not the Unreal environment yet. Here is my problem (I’m tired so it’s maybe dumb) :
I have a very basic class which is a child of UObject. It is a “base” spell class named USpell, that I want to use as a parent class for all my spells (making a game with different spells and abilities).
So let’s say I want to test the new spell I created, and I want to initialize it in my xxxxxCharacter.cpp so I can use it to call the onCast() function that I made. How am I supposed to initialize it with UE4 exactly? They won’t let me create my own constructors, and if I try to create an object of that class, it gives me an error that the class doesn’t have any default constructor… same if I try to use a pointer and the new operator.
The only way I found that is working is, for exemple :
currentSpell_ = new UStamToHP(FPostConstructInitializeProperties::FPostConstructInitializeProperties());
You can get instace of UClass of any UObject class from static funtion inside it called StaticClass ()
Now yo create UObject object you can use this:
SomeClass::StaticClass()-> GetDefaultObject ();
AActor childs need to be spawn to the world and theres special funtion for that in world instace
()-> SpawnActor (SomeActor::StaticClass ());
If you do that from UObject that is not actor you might have problem accessing world instace, then you might try ho access it from GEngine.
From practices in old unreal engine version, things like spells should be child of actor, even thru it does not have phisical from in world, in similar way as AGameMode is, it helps avoid issues of nonActor UObject limitness.
Hmmm i think i might be wrong getdefutobject willget defultobject that is probably used to get defaults… you might copy instace from defaultobject i guess… ajyway use Actors and SpawnActor
For anyone who gets here through google, use ->SpawnActor() for actors and NewObject() for non-actor classes. Don’t switch to actors just because you need to instantiate a class from script or BP.
For me, saving SomeObject->GetClass() as the TSubclassOf variable, then calling NewObject(this, SavedClass) worked. I was missing the this keyword argument which was preventing the ChildClass constructor from being called. Hope this helps any desperate souls out there.