I've been trying to spawn an actor in C++ for the last 8 hours, please help.

Can someone please show me a 5-10 step tutorial for spawning an actor properly according to standard Unreal Engine methodology? Are actors supposed to be spawned from player controller, character or actor?

Is there a ten minute tutorial on youtube that shows you how to spawn an actor at FVector (0,0,0) when a key is pressed?

I have a twitch video showing me and twenty other people trying to figure this out, it’s extremely difficult. I’m so tired.

My background is Unity & C#. In Unity, spawning an actor from C# code was a trivial task. Don’t get me wrong, Unreal Engine is BEAUTIFUL, but it’s just hard to use.



void AriseCPPPlayerController::OnPlaceObjectPressed()
{

//F KEY PRESS WORKS
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, ("At least the F key press works! Now let's spawn an actor."));

FActorSpawnParameters SpawnInfo;

FRotator myRot(0, 0, 0);
FVector myLoc(0, 0, 100);

//just spawn the god **** actor
AmySphere mySphere =  GetWorld()->SpawnActor<AmySphere>(this->GetClass(), myLoc, myRot, SpawnInfo);


//AActor* MySpawnedActor = World->SpawnActor<AmyWallSegmentCPP>(myWallSegmentClass, SpawnLocation, SpawnRotation);
//AmyWallSegmentCPP* const SpawnedPickup = World->SpawnActor<AmyWallSegmentCPP>(myWallSegmentClass, SpawnLocation, SpawnRotation, SpawnParams);
}



How would I get the above code to work at the most basic level of Unreal Engine C++? Let’s say from a keypress triggered from player controller.

I’m trying, I really am, I’m so tired.




**cpp**
#include "myObject.h"
.....
AmyObject* myObject = World->SpawnActor<AmyObject>(BlueprintVar, SpawnLocation, SpawnRotation);

**h**
UPROPERTY(EditDefaultsOnly, Category=Projectile)
TSubclassOf<class AmyObject> BlueprintVar;


I made a BP of myObject too.

Can someone please provide step by step or youtube video? Need sleep.

So is the keypress working, and it’s just the spawning that isn’t? I assume so from your comments.

In the first example, you’re passing this->GetClass() as the class of actor that you want to be spawned. This isn’t what you want (this->GetClass() will return the runtime class object for AriseCPPPlayerController), nor will it do anything because whatever class you provide must be the same as or derived from the class you specify as the template argument - in this case, AmySphere. Replace it with AmySphere::StaticClass(), or just remove that argument entirely, and it will do the same thing by just using the template argument.

The second option should work, provided you have set BlueprintVar inside the editor, on your blueprint object.

One other thing to be aware of is that spawning can fail if the object you are creating would overlap/collide with something already there. You can configure this behaviour with the actor spawn parameters.

I’m going to experiment more today with the information you provided.

Its in the docs.

Did you see those? or was it something else you didn’t understand? The “staticclass” things can be a bit confusing. But the actual usage once you understand the parameters is pretty simple.

I switched to blue prints.

The biggest problem with c++ UE4 is the lack of explanation on how classes communicate with each other. For example, how can classPlayerController access public functions/vars/objects from classSpawnManager or how classGameState can access functions/vars/objects from classPlayerController. When you finally do get communication established between classes, half the time the method you’re calling upon won’t perform what you expected it to.

I finally got the actor to spawn, but as soon as I put the same exact code into a different class, it just stopped working. Too much work with little return.

Unity C# is faster and more productive than UE4 C++, but UE4 blue prints are faster and more productive than Unity C#, just my two cents.

I strongly recommend switching to blueprints.

In my experience it is
C# beats blueprints and C++ beats C#. To each its own, as they say.

Actor spawning was explained in C++ First Person Tutorial.
https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial

Also, blueprints use same data structures and same methods as C++, so there shouldn’t be any difference.

Anyway, suit yourself.

Classes communicate with each other the same way they do in any program, you just need a way to reference the instantiated class to call its methods. However, the key thing to realize here is that UE4 relies heavily on a singleton patterns and a master instanced object list. This is a great thing, because you can acquire a reference to any of the singleton classes in your project such as gamemode, cast it to your subclassed gamemode if you made one, and call any method on it you need… or, more importantly, grab any child object off of it. Further, if you really need to find something not referenced from one of the singletons, you can call the object and actor iterators on the world, which just iterate over the master instance list, and cast your objects appropriately. The API documentation is all there, but I agree there is a shortage of examples in context. However, once you understand the pattern it is pretty straightforward. Spawning an actor is just adding to that master instance list… and either saving a reference to it that can be reached via one of the other objects, or iterating over your world later to find it… or maybe even not caring about it ever again after setting its location in the world.

I watched your stream for a while in the first few days and I thought you were making great progress, though twitch is a terrible medium for collaboration and people kept sending you down the wrong path. That being said, if you find blueprints more intuitive and it gets you making your content faster, and thus gets you into a place where you are enjoying yourself faster, by all means use blueprints. You can always extend your project with C++ classes later if you find the need. Maybe you’ll never need to for your game. Who knows? It really doesn’t matter. Do what works for you.

A better description of the actual problem would help… assuming it compiles and there are no errors in the log at runtime and that you see the debug printout out.

When spawning there are certain conditions you can set regarding collisions. Make sure you are setting AlwaysSpawn