Fundamentally - you need a UWorld
to spawn an actor in. Where you get your world from depends how you are calling your code. If you are writing a static function you are going to call from blueprint (i.e. from a slate button event handler), you need to pass in the WorldContextObjcet explicitly.
To do this, you need to make your method signature look like this:
UFUNCTION(BlueprintCallable, Category = "Game", meta = (WorldContext = "WorldContextObject"))
static AActor* MySpawnFunction(UObject* WorldContextObject, int MyOtherParameter);
Then inside your function body, you can call WorldContextObject->GetWorld()
to get the UWorld
to spawn in. The markup in the UFUNCTION
line inside the meta
section means you won’t have to manually provide the object providing you call the function within a UObject
(e.g. a Widget, Actor, Component etc).