Download

How can i spawn an actor via static function ( using BPFuncitonLibrary ).

I’ve tried this one and it generates error : ‘UWorld::UWorld’ : no appropriate default constructor available


h:
static AActor* SpawnActorTemplate(UClass * class_, FVector location_, FRotator rotation_,AActor* owner_, APawn* instigator_, AActor* iemplate_);


cpp:
AActor* UMyBlueprintFunctionLibrary::SpawnActorTemplate(UClass * class_, FVector location_, FRotator rotation_, AActor* owner_, APawn* instigator_, AActor* template_)
{
	 FActorSpawnParameters SpawnParameters_;
	 SpawnParameters_.Owner = owner_;
	 SpawnParameters_.Instigator = instigator_;
	 SpawnParameters_.Template = template_;
	 FVector* locationPtr = &location_;
	 FRotator* rotationPtr = &rotation_;
	 return (AActor*) UWorld()->SpawnActor(class_, locationPtr, rotationPtr, SpawnParameters_);
}


UWorld() would be constructing an instance of World and then calling the function on that pointer.

What you need to do is pass in a world context to your function and then call spawn actor on that. You can see some examples of this in GameplayStatics.h:



UFUNCTION(BlueprintCallable, Category="Spawning", meta=(WorldContext="WorldContextObject", UnsafeDuringActorConstruction = "true", BlueprintInternalUseOnly = "true"))
static class AActor* BeginSpawningActorFromClass(UObject* WorldContextObject, TSubclassOf<AActor> ActorClass, const FTransform& SpawnTransform, bool bNoCollisionFail = false);


This indicates that the argument WorldContextObject will be invisible and auto-populated with the self of the calling context unless that calling context is unable to provide a world in which case the pin will be visible and required to be hooked up.

Then in the implementation of the function you can get the World


UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);

Also note the UnsafeDuringActorConstruction … you’ll want that specified as it is not valid to call SpawnActor during construction scripts, so this will prevent it from being placed/called in the construction script.

Thanks a lot for the great explanation ! I will check this out soon :slight_smile:

TArray<APlayerController*> PCarr;

GEngine-&gt;GetAllLocalPlayerControllers(PCarr);

//if (pLP)
if ((PCarr.Num()&gt;0) && (PCarr[0]))
{    

use PCarr[0]