Creating UWidgetComponent using C++


Im trying to create a WidgetComponent as part of my spawn code in order to generate some 3D HUD elements. I have already added my widget using blueprints, but now I wanted to instantiate it via c++ so that I can reference it from c++ code

When I do the following

AFFCharacter::AFFCharacter(const class FObjectInitializer& PCIP)
	: Super(PCIP.SetDefaultSubobjectClass<UFFCharacterMovement>(ACharacter::CharacterMovementComponentName))
	CapsuleHeight = 96.0f;
	CapsuleRadius = 45.0f;
	// Set size for collision capsule
	GetCapsuleComponent()->InitCapsuleSize(CapsuleHeight, CapsuleRadius);
	// set our turn rates for input
	BaseTurnRate = 45.f;
	BaseLookUpRate = 45.f;

	// Default offset from the character location for projectiles to spawn
	GunOffset = FVector(100.0f, 30.0f, 10.0f);

	// Create a CameraComponent	
	PlayerCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("PlayerCamera"));
	PlayerCameraComponent->AttachParent = GetCapsuleComponent();
	PlayerCameraComponent->AttachSocketName = FName(TEXT("HeadCameraSocket"));
	//PlayerCameraComponent->RelativeLocation = FVector(0, 0, 64.f); // Position the camera

	UMeshComponent* PlayerMesh = GetMesh();
	PlayerMesh->SetOnlyOwnerSee(false);			// only the owning player will see this mesh
	PlayerMesh->AttachParent = PlayerCameraComponent;
	PlayerMesh->bCastDynamicShadow = true;
	PlayerMesh->CastShadow = true;

	// Create a first person mesh, so we can switch between first and third person
	FirstPersonMesh = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("FirstPersonMesh"));
	FirstPersonMesh->SetOnlyOwnerSee(true);			// only the owning player will see this mesh
	FirstPersonMesh->AttachParent = PlayerCameraComponent;
	FirstPersonMesh->RelativeLocation = FVector(0.f, 0.f, -150.f);
	FirstPersonMesh->bCastDynamicShadow = false;
	FirstPersonMesh->CastShadow = false;

	// Create a component for doing a 3d pointer to the source of the damage
	DamageSourceWidget = PCIP.CreateDefaultSubobject<UWidgetComponent>(this, TEXT("DamageSource"));
	DamageSourceWidget->AttachParent = PlayerCameraComponent;
	DamageSourceWidget->RelativeLocation = FVector(220.0f, -50.0f, 0.0f);
	DamageSourceWidget->RelativeRotation = FRotator(40.0f, 0.0f, 90.0f);

	// Set that we allow crouching in this game
	GetCharacterMovement()->GetNavAgentProperties()->bCanCrouch = true;

	// Note: The ProjectileClass and the skeletal mesh/anim blueprints for Mesh1P are set in the
	// derived blueprint asset named MyCharacter (to avoid direct content references in C++)
	WallRunInitialClimbSpeed = 200;
	WallRunGravityScale = 0.5;

	Health = 100;
	DamageScaling = 1.0f;

	DoubleTapToDiveTime = 0.5f;


It gives me the component in the components list of my character but there are no editable properties associated with it. Is this because its still not developed enough, or do I need to do something else to make it work?

The blueprint added widget (non-native) works as I’d expect.

Does anyone have any idea what to add to allow me to edit the properties of my native widget?

This is still the same in 4.7. Does something else need to be added to get this working?

UWidgetComponent can’t even be initalized by that line of code anymore:

PCIP.CreateDefaultSubobject<UWidgetComponent>(this, TEXT("DamageSource"));

Nor does that code work which would be used as of 4.7:


I phrased my question a different way and got what I needed.

See it in action here - Using material on UMG images - UI - Unreal Engine Forums

Much appreciated, I’ll have a look at it!