C++/UObjects

Actor Components: Spawn Component

If you need to spawn a new Component to/within an Actor by a certain class, here is an example on how to do it with a Box Collision:

UActorComponent* NewActorComponent =
this->AddComponentByClass(UBoxComponent::StaticClass(), false, FTransform(), false);


UBoxComponent* NewBoxComponent = Cast<UBoxComponent>(NewActorComponent);

NewBoxComponent->AttachToComponent(

this->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);

In this example we are spawning a BoxComponent that is (because its parent class is) an ActorComponent. It's also not automatically attached to something. Then we make sure to cast to the Box Component from the Actor Component to get its information (for example so that later we can change the box extend and other values),  As a last step we manually attach it to the Root Component of its actor.

Scene Components: Parent for Blueprint

If you need to create a C++ Scene Component that can be used as a Blueprint Parent Class, make sure to add "BlueprintType", "Blueprintable" and "BlueprintSpawnableComponent".
An example:

UCLASS(

ClassGroup=(EUSU),

BlueprintType,

Blueprintable,

meta=(

  BlueprintSpawnableComponent,

  DisplayName = "Scene Component System Damage",

  ToolTip = "Add to Box Component to define Damage Zones."

  )

)

Scene Components: Detach Component

Detaching a Component in C++ can be rather tricky at first. Use this as an example:

this->DetachFromComponent(FDetachmentTransformRules(EDetachmentRule::KeepRelative, true));

Other ways did not work. And instead of "EDetachmentRule::KeepRelative" you can use "KeepWorkd" instead.
What that bool does I do not yet know, sadly.

Actors: Spawn Actor

If you want to spawn a new Actor that is based on the AActor* class, the following is a good example:

ACPP_IconManager IconManager;

const FVector Location {0.f, 0.f, 0.f};

const FRotator Rotation {0.f, 0.f, 0.f};

FActorSpawnParameters SpawnParameters;

SpawnParameters.Owner = APlayer; // any AActor* class will do

SpawnParameters... // change other settings


IconManager = GetWorld()->SpawnActor<ACPP_IconManager>(ACPP_IconManager::StaticClass(), Location, Rotation, SpawnParameters);

This is just an example used by myself to spawn a certain Manager Actor.

Actors: Default Component

If you want to add a default component to an Actor, use the following as an example to add a Camera.
.h

// Camera Reference.

UPROPERTY(

   BlueprintReadWrite,

   Category="EUSU|DoF")

UCameraComponent* CamDoF;


For the cpp file, make sure to put the code into the Set Defaults Value Event.
.cpp

CamDoF = CreateDefaultSubobject<UCameraComponent>(FName("Camera"));

Classes: Reparenting Blueprints to new Parent Class results in Access Violation Errors

If trying to re-parent a Blueprint Class to another Class (Blueprint, C++), the Engine might crash giving a not very helpful Access Violation Error.
1. Create/Open a completely empty map and save it.
2a. Open the Blueprint and re-parent it. Now it should work.
2b. If it doesn't, try making the empty Map the default editor loading map. Re-open the editor and try 2a again.
3. Save everything, close the Editor and re-start it. Now it should work fine.

UInterface: Define Interface

Create a new Class based on "UInterface". Just an .h file is enough and as an example, this is what it could look like:

// Copyright Info


#pragma once


#include "CoreMinimal.h"

#include "UObject/Interface.h"


#include "CPPI_UnitActions.generated.h"


// This class does not need to be modified.

UINTERFACE(MinimalAPI, Category="MyCategory")

class UCPPI_UnitActions : public UInterface

{

GENERATED_BODY()

};


class MYCLASS_API ICPPI_UnitActions

{

GENERATED_BODY()


// INTERFACE FUNCTIONS

#pragma region InterfaceFunctions

public:


UFUNCTION(BlueprintCallable, BlueprintNativeEvent)

void InterfaceUnitActions_MoveToLocation(

const FVector Coordinates);


#pragma endregion InterfaceFunctions

};

UInterface: Implement Interface

Once you have defined an Interface, to implement it into another class, do the following:

UCLASS()
class MyClass_API MyActor : public AActor, // Unreals class definition as an example
public ICPPI_UnitActions // this is the actual implementation

UInterface: Call Interface

As an example, we have an Interface called UCCPI_UnitActions (class) that will be implemented via ICCPI_UnitActions:

AActor* Unit;

if (const auto Interface = Cast<ICPPI_UnitActions>(Unit))

{

Interface->Execute_InterfaceUnitActions_MoveToLocation(

Unit,

FVector(2534.f, 12.f, 21.f);

}

To do the same from a Blueprint Interface, the following should work (untested):

AActor* Unit;


if (Unit->GetClass()->ImplementsInterface(ICPPI_UnitActions::StaticClass()))

{

ICPPI_UnitActions::Execute_InterfaceUnitActions_MoveToLocation(

Unit,

FVector(2534.f, 12.f, 21.f);

}

Thanks to Leszek Górniak from the Unreal Forum.