Hi everyone,
I’m currently trying to implement a custom actor class with UE4.20, but I’m having quite a few issues with that.
Beside the tutorials and examples often missing includes or just telling “hey, just use this code […]” without telling where the code is intended to be called so far nothing seems to be working as expected.
For this concrete example I’m using https://docs.unrealengine.com/en-us/Programming/Tutorials/Components/1 and Example AActor-derived class for Unreal Engine 4 · GitHub as references. (I read through a lot of related articles on google about this, but I’m not going to list them all here)
So my class currently looks like this:
OrbActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/StaticMesh.h"
#include "Engine/Classes/Components/SphereComponent.h"
#include "Engine/Classes/Components/StaticMeshComponent.h"
#include "ConstructorHelpers.h"
#include "OrbActor.generated.h"
UCLASS()
class SPACEEXPLORER_API AOrbActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AOrbActor(const FObjectInitializer& ObjectInitializer);
/** Radius of the orb. */
UPROPERTY(EditAnywhere)
float OrbRadius;
/** Physics component of the orb. */
UPROPERTY(EditAnywhere)
class USphereComponent* OrbPhysics;
/** Static mesh component of the orb. */
UPROPERTY(EditAnywhere)
class UStaticMeshComponent* OrbMesh;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
OrbActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "OrbActor.h"
// Sets default values
AOrbActor::AOrbActor(const FObjectInitializer& ObjectInitializer)
{
// Default property values
OrbRadius = 1.0f;
// 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;
// Create physics component
OrbPhysics = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = OrbPhysics;
//OrbPhysics->SetupAttachment(RootComponent);
// Create static mesh component
OrbMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
OrbMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded())
{
OrbMesh->SetStaticMesh(SphereVisualAsset.Object);
OrbMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
OrbMesh->SetWorldScale3D(FVector(OrbRadius));
}
}
// Called when the game starts or when spawned
void AOrbActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AOrbActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
The code as shown compiles fine (tried both starting the compile in Visual Studio or the UE-Editor), but as a result when adding the actor it will neither show the sub components, neither any of the defined UPROPERTY fields. What am I getting wrong here?
Any help would be greatly appreciated.
Edit: Forgot to attach the screenshot of the result: