Announcement

Collapse
No announcement yet.

How to spawn a sphere?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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...s/1/index.html But I guess I am doing somethign wrong.

    Code:
    .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!

    #2
    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
    Code:
    UStaticMeshComponent * SphereMesh;
    .cpp
    Inside constructor:

    Code:
    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
    Last edited by danielvmacedo; 08-03-2015, 02:27 AM.

    Comment


      #3
      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...

      Code:
      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

      Comment


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

        try this:

        Code:
        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.

        Comment


          #5
          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?

          Comment


            #6
            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.

            https://wiki.unrealengine.com/First_...2B%2B_Tutorial

            Comment


              #7
              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!

              Comment


                #8
                Yeap, matthew_clark 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 matthew_clark.

                Cheers

                Comment

                Working...
                X