Component documentation out of date?

I got Beta 5 and have finally started to dig into C++. I’m trying to add a component to my newly created actor.

The documentation tells me to use the ConstructorHelpers::CreateComponent function, but this function doesn’t seem to exist. Intellisense can’t find it, and a search through the engine’s header files yields no result.

Is something missing or has the process to create component changed?

Thanks.

The documentation is definitely outdated. The correct method of creating components in the constructor is shown below:

NewComponent = PCIP.CreateDefaultSubobject(ComponentOuter, ComponentName);

Where ComponentClass is the class of the component you want to create like USkeletalMeshComponent, ComponentOuter is the object that contains the component, and ComponentName is the FName to use to identify the component.

An example from the ACharacter class constructor for creating the movement component:

CharacterMovement = PCIP.CreateDefaultSubobject(this, ACharacter::CharacterMovementComponentName);
if (CharacterMovement)
{
	CharacterMovement->UpdatedComponent = CapsuleComponent;
	CharacterMovement->MaxStepHeight = 45.f;
	CrouchedEyeHeight = CharacterMovement->CrouchedHalfHeight * 0.80f;

	CharacterMovement->GetNavAgentProperties()->bCanJump = true;
	CharacterMovement->GetNavAgentProperties()->bCanWalk = true;
	CharacterMovement->SetJumpAllowed(true);

	Components.Add(CharacterMovement);
}

There are also other similar functions like:

  • CreateOptionalDefaultSubobject
  • CreateAbstractDefaultSubobject
  • CreateEditorOnlyDefaultSubobject

These can be found in UObjectGlobals.h (assuming you have access to that in rocket).

In your example, what type is CharacterMovement?

CreateDefaultSubobject returns a TSubobjectPtrConstructor, and Components.Add is expecting a UActorComponent pointer, so your example doesn’t quite make sense.

CharacterMovement is a UCharacterMovementComponent pointer (TSubobjectPtr to be exact). CreateDefaultSubObject() returns a TSubObjectPtrConstructor, which is just a fancy pointer to an instance of ComponentClass. So, in the example above, it is returning a pointer to a UCharacterMovementComponent. Since UCharacterMovementComponent is a UActorComponent, this is a UActorComponent pointer, which as you say is exactly what Components.Add() is expecting.