C++ Adding Components To Class & Blueprints

From Unreal Wiki
Jump to navigation Jump to search


Note: See bugs section, for bugs regarding refreshing and updating the c++ code for Child blueprint classes


<syntaxhighlight lang="cpp">

UCLASS() class ININO0_API AIn0_Item : public AActor { GENERATED_BODY()

public: // Sets default values for this actor's properties AIn0_Item();

public:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Details") UStaticMeshComponent *ItemMesh; };

</syntaxhighlight>


Inside the CPP file you have to initalize the Component and attach it to the Root Component(Or another component of your choice)


<syntaxhighlight lang="cpp"> AIn0_Item::AIn0_Item() {

	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = true;

this->ItemMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("TheItemMesh")); this->ItemMesh->SetupAttachment(RootComponent);

} </syntaxhighlight>


This will make it appear in The blueprint component list if you create a child blueprint class


Bugs

I had to open and close the editor and restart it for the Component to update, for a blueprint Child class

it seems regular variables can update immediately with Recompiles and reload c++ code on the fly, or just recompiling the c++ binary. However for Components I had to shutdown and reopen the editor.


One of the issues this bug caused was a empty details panel

When you select The component from the cpp file