How to spawn a sphere?

Hey guys, I have been looking for hours trying to find a way to do this and it’s starting to get me angry,
I was trying to follow this https://docs.unrealengine.com/latest/INT/Programming/Tutorials/Components/1/index.html But I guess I am doing somethign wrong.



.cpp:
ASpawnFactory::ASpawnFactory(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	Sphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
	Sphere->InitSphereRadius(5.0f);

}

.h:
	USphereComponent* Sphere;


It’s not allowing me to spawn a sphere, I don’t know what to do or if I am on the right track, also is that just a component sphere, would I have to spawn a mesh as well?
Even though I appreciate answers please try to answer the question instead of advice or redirection, seeing how once a thread has a reply no one checks it after. Thanks so much!

USphereComponent is not a “real” sphere. It is an invisible sphere used for collisions and physics. You should look for UStaticMeshComponent and assign a Sphere to it. It will look something like this:

.h


UStaticMeshComponent * SphereMesh;

.cpp
Inside constructor:


this->SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereMesh"));
this->SphereMesh->AttachTo(GetRootComponent());

static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
this->SphereMesh->SetStaticMesh(SphereMeshAsset.Object);

PS:
TEXT(“StaticMesh’/Engine/BasicShapes/Sphere.Sphere’”) is the default sphere used by the engine

Thanks so much! So I think either somethings wrong with unreal or I am stupid. This spawns a sphere named “SphereMesh” in the scene, correct? It’s still not working for me…


ASpawnFactory::ASpawnFactory()
{
	UE_LOG(LogTemp, Warning, TEXT("MyCharacter's second is f"));

	
	this->SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereMeshz"));
	this->SphereMesh->AttachTo(GetRootComponent());

	static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
	this->SphereMesh->SetStaticMesh(SphereMeshAsset.Object);
}

LogActor:Warning: GameSession /Game/FlyingBP/Maps/UEDPIE_0_FlyingExampleMap.FlyingExampleMap:PersistentLevel.GameSession_19 has natively added scene component(s), but none of them were set as the actor’s RootComponent - picking one arbitrarily
LogActor:Warning: GameNetworkManager /Game/FlyingBP/Maps/UEDPIE_0_FlyingExampleMap.FlyingExampleMap:PersistentLevel.GameNetworkManager_19 has natively added scene component(s), but none of them were set as the actor’s RootComponent - picking

looks like you are creating a brand new Actor… you will need a RootComponent.

try this:


ASpawnFactory::ASpawnFactory()
{
	UE_LOG(LogTemp, Warning, TEXT("MyCharacter's second is f"));

	
	this->SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereMeshz"));
	//this->SphereMesh->AttachTo(GetRootComponent());
        this->SetRootComponent(this->SphereMesh);

	static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
	this->SphereMesh->SetStaticMesh(SphereMeshAsset.Object);
}

this way your SphereMesh will be the root component.

This is all strange to me, I understand the logic but it’s still not working for me. Is this supposed to spawn a spehere in the game scene?

This will not spawn anything in the game, but the code that danielvmacedo provided will make it so that the ASpawnFactory class will have a root component, and this root component is the SphereMesh that you’re creating. If you wish to spawn something in the scene, you would want to use SpawnActor in the BeginPlay function.

I would suggest following this tutorial to learn some more about spawning actors/meshes, specifically the spawning projectiles section.

Thanks so much, I will for sure look at that. I’m coming from Unity so Understanding prefabs is in it’s own little world with that.

It’s a shame I could not find that when searching for spawning actors. Thanks guys!

Yeap, is right. The code that I posted to you is only to create a base Actor class that has a sphere mesh as its root. To spawn a actor in your world you need to call SpawnActor method from the World. You can see how its done in the link suggested by .

Cheers