How do I set the RootComponent of Actor?

I’ve created a new class, derived from APawn, it’s a spaceship that can be moved using input and has collision based on a USphereComponent. The problem comes when setting the RootComponent.

  • If I don’t set the root component
    at all, the Pawn has no Transform
    component and I can’t move it
    around.
  • If I set the RootComponent as a SceneComponent, I get
    the transform, but can’t use
    functions such as
    AddImpulse/AddForce to the root and
    things get weird, because if I use
    AddForce to the SphereComponent, it
    moves relative to the object, so the
    root never moves, only the
    SphereComponent inside of it.
  • If I set the root as the
    SphereComponent, an exception is
    thrown everytime I play the game
    in-editor:
    Ensure condition failed: PrimComp->IsRegistered()
    Components must be registered in order to be used in a ComponentOverlapMulti call. PriComp: SphereComponent TestActor: Default__MyShipBP2_C
    Collision and physics work, but I can’t change the position of the Actor by using “SetLocation”, because the SphereComponent does not have a transform. On top of that, it does not matter where I put the PlayerStart, it always starts on 0,0,0.

I don’t want to set the root as a StaticMesh, because I want to set the mesh on a Blueprint, inside editor.
So… Why is this so hard? I’m simply trying to create an Actor that:

  1. Has collision
  2. Has physics
  3. Can be moved using force/impulse
  4. Can be moved using SetPosition
  5. Has a static mesh defined on blueprints

but I’m never able to achieve all of it. What is the correct way to do it?

Where are you setting the root component? You need to set it from the constructor. Sphere components should work fine as root components, and we use capsule components often. Here’s a capsule example from fortnite:

TouchCapsule = PCIP.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("CollisionCylinder"));
TouchCapsule->InitCapsuleSize(30.0f, 80.0f);
TouchCapsule->SetCollisionEnabled(ECollisionEnabled::NoCollision);
TouchCapsule->SetCollisionResponseToAllChannels(ECR_Ignore);
TouchCapsule->SetCollisionResponseToChannel(FCC_Trace_Interaction, ECR_Block); 
TouchCapsule->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
RootComponent = TouchCapsule;

I was doing it inside the constructor, pretty much like Ben answered, without the SetCollision* methods. But there was something wrong with that, because a blueprint based on that class could not even be dragged to the scene without throwing the exception I described on the original question and sometimes the actor would not be placed at all. After going through a lot of variables and documentation I realized that “bCollideWhenPlacing=false” gets rid of the exception.

Hello, I’m new. Pass a training course Get Started (https://docs.unrealengine.com/latest/INT/Programming/QuickStart/7/index.html)
I created a class:

#include "TestUProject.h"
#include "MyActorTest.h"


AMyActorTest::AMyActorTest(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
    MyNumber = 12;
}

void AMyActorTest::BeginPlay()
{
    Super::BeginPlay();
    
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World!"));
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(MyNumber));
    }
    
}

I have a problem that I can not move in Editor to AActor after placing it in ViewPort. I that I was missing RootComponent, but I do not understand how to add it. Can help you have my source code to solve my problem?
This code is doing in terms of training.

Hello,

Thank you for your report. We were not able to investigate this on the engine version you reported, but there have been many version changes to UE4 since this question was first posted. With a new version of the Engine comes new fixes and it is possible that this issue has changed or may no longer occur. Due to timetable of when this issue was first posted, we are marking this post as resolved for tracking purposes.

If you are still experiencing the issue you reported in the current engine version, then please respond to this message with additional information and we will investigate as soon as possible. If you are experiencing a similar, but different issue at this time, please submit a new report for it.

Thank you