Could you provide the full code from your .h and .cpp files that you are using?
This could potentially be a naming conflict, because each time you are spawning the actor, you are giving it the name “1”. Try commenting this line out, and see if you get the same result.
I was able to solve your issue by making a few changes to the code.
UWorld* AMyActor::gtW(AActor* actor)
{
//Check if the actor is valid
if (actor)
{
UWorld* const World = actor->GetWorld();
if (World)
{
return World;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "World - false");
return NULL;
}
}
//If the actor is not valid, return NULL
else
{
return NULL;
}
}
void AMyActor::SpawndA(class UClass* clas, class AActor* actor)
{
FActorSpawnParameters param;
FVector loc = FVector(0, 0, 0);
FRotator rot = FRotator(0, 0, 0);
if (clas == NULL)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "clas - false");
if (gtW(actor) != NULL)
gtW(actor)->SpawnActor(clas, &loc, &rot, param);
}
So the first thing I did was remove your AMyActor variable from the SpawndA function, as it was unnecessary and was never being initialized. After this, I took the a.gtW out, and just called gtW(actor), as it would use the MyActor class to call GetWorld().
Finally, I implemented a check to see if the actor being passed in was valid, because if it wasn’t then GetWorld would always fail.
Feel free to let me know if there are any further questions.