Download

Adding a capsule component results in crashing

Heya guys!

In a prototype of my character, I use a second capsule component to do fancy stuff in my blueprints.
However it is time to start transferring to C++… but I can’t get adding this capsule in code to work!

human.h:



UPROPERTY(VisibleDefaultsOnly)
		class UCapsuleComponent* CapsuleComp;


human.cpp:



CollisionComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComp"));
	CollisionComp->InitCapsuleSize(90, 170);
	CollisionComp->BodyInstance.SetCollisionProfileName("OverlapAll");
	CollisionComp->AttachParent = CharMesh;


Whenever I run this code, the entire unreal engine crashes. D:
Anyone know what the problem is? I’m guessing a capsule isn’t run with CreateDefaultSubobject, but idk how. :confused:

Thanks!
-Josh

Hey you forgot to pass the instance of the class.
Also always good to check your pointers.


CollisionComp = CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("CapsuleComp"));
if (CollisionComp)
{
	CollisionComp->InitCapsuleSize(90, 170);
	CollisionComp->BodyInstance.SetCollisionProfileName("OverlapAll");
	CollisionComp->AttachParent = CharMesh;
}

And i can`t see your Object Initializer for your constructor.

in YourActor.h
Under Generated Body macro.


GENERATED_BODY()
public:
    AYourActor(const class FObjectInitializer& PCIP);

And in YourActor.cpp


AYourActor::AYourActor(const class FObjectInitializer& PCIP)
	: Super(PCIP)
{
	CollisionComp = PCIP.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("CapsuleComp"));
	if (CollisionComp)
	{
		CollisionComp->InitCapsuleSize(90, 170);
		CollisionComp->BodyInstance.SetCollisionProfileName("OverlapAll");
		CollisionComp->AttachParent = CharMesh;
	}
}

WCode

I already had the objectinitializer, but never did the whole PCIP.CreateDefaultSubobject. Thanks.
Alas, it still crashes.

I notice that even when I remove the code from the .cpp file, it still results in a crash.
So could it be possible that this piece of code in the header file is to blame?



UPROPERTY(VisibleDefaultsOnly)
		class UCapsuleComponent* CollisionComp;



UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Your Category, Sub Category")
    UCapsuleComponent* CapsuleComp;

Also just to be safe make sure you don`t have a second component with the same name.
But if you are deriving your class from ACharacter:: it should have a capsul component setup out of the box.

I’m needing a second capsule to detect overlaps so I can do edge grabbing…
And yeah, when I posted it I changed the name. So it should work! But… even if I do exactly what you tell me!!!
What could be the problem??? This is really bugging me.

EDIT: HUZZAH! It worked! It seems that adding class in front breaks it! YAY! Thanks so much!!