Unreal Engine, from C++ spawn Blueprint inherited from C++ class, very small example

From Andreida
Revision as of 13:23, 6 May 2024 by Andreas (talk | contribs) (Created page with "=== Prepare Project === ==== Create Project ==== * Games / Blank ** C++ ** Desktop ** Maximum ** Starter Content: no ** Raytracing: no ** Location: your choice ** Project Name: ShowActor * Create ==== Change Map to Basic ==== When the UE-Editor is open: * File / New Level / Basic / Create * File / Save Current Level as ... / Basic01 * Edit / Project Settings / Maps & Modes / Editor Startup Map: Basic01 * Edit / Project Settings / Maps & Modes / Editor Game Default Map:...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Prepare Project

Create Project

  • Games / Blank
    • C++
    • Desktop
    • Maximum
    • Starter Content: no
    • Raytracing: no
    • Location: your choice
    • Project Name: ShowActor
  • Create

Change Map to Basic

When the UE-Editor is open:

  • File / New Level / Basic / Create
  • File / Save Current Level as ... / Basic01
  • Edit / Project Settings / Maps & Modes / Editor Startup Map: Basic01
  • Edit / Project Settings / Maps & Modes / Editor Game Default Map: Basic01

Create Classes

Actor

  • Tools / New C++ Class
    • Parent Class: Actor
    • Name MiniActor
    • Create Class
Header File: MiniActor.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MiniActor.generated.h"

UCLASS(Blueprintable)
class SHOWACTOR_API AMiniActor : public AActor
{
	GENERATED_BODY()
	
public:	

	AMiniActor();

protected:

	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere, Category = "Structure", meta = (DisplayName = "Mesh"))
	UStaticMesh* m_pMesh;

	UPROPERTY(EditAnywhere, Category = "Structure", meta = (DisplayName = "Scene Component Root"))
	USceneComponent* m_pScRoot;

	UPROPERTY(EditAnywhere, Category = "Structure", meta = (DisplayName = "Static Mesh Component of Root"))
	UStaticMeshComponent* m_pSmcRoot;
};
Source File
#include "MiniActor.h"

AMiniActor::AMiniActor()
{
	m_pScRoot = CreateDefaultSubobject<USceneComponent>("Root");
	RootComponent = m_pScRoot;

	m_pSmcRoot = CreateDefaultSubobject<UStaticMeshComponent>("SmcRoot");

	FAttachmentTransformRules transformRule = FAttachmentTransformRules::SnapToTargetIncludingScale;
	m_pSmcRoot->AttachToComponent(RootComponent, transformRule);

	PrimaryActorTick.bCanEverTick = false;
}

void AMiniActor::BeginPlay()
{
	Super::BeginPlay();

	if (m_pMesh != nullptr)
	{
		m_pSmcRoot->SetStaticMesh(m_pMesh);
	}
}

Build

Now build the project. Nothing works so don't even try to test it in the UE-Editor, but the code should compile and link. This is a class that is complete as it is.