How do I spawn an actor in world via c++ plugin?

Hi everyone,

I am currently trying to spawn in a custom actor via a plugin that I have created in c++.
I have been trying to use the UWorld->SpawnActor() method but have been having major issues getting the Uworld from anywhere.

I am implementing this logic in a plugin module derived from IModuleInterface and it is supposed to spawn an actor in upon pressing a slate button. As I said I have had some major issues attempting to get the UWorld in any fashion and was wondering if someone could give me some direction in terms of spawning my custom actor in.

Thanks.

Hello Agent, If by world you mean map, I would take a look at this link. I hope this helps and good luck :D.

Hi TheDT,

Thanks for the link, although I have already come across this before. Unfortunately I cannot call SpawnActor within the cpp that I need to.

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).