First of all, I don’t understand why strings are not an obvious default datatype in C++. If I were programming this in Game Maker, I could select a sprite from a set of pre-loaded sprites based on an integer value, and I don’t have to worry about declaring datatypes beforehand.
Here, I have 8 different meshes that I could potentially assign a Fence obstacle object. My idea is that I declare an integer HeightMarker in the constructor in the *.h file. I can assign this integer value externally through another object upon creation, but when the BeginPlay() method is called, I want to assign the mesh.
Here is my code at present. Ideally, I would like to just have a variable that stores the reference path inside of the TEXT field, and then call the constructor helper function thereafter. My compiler is complaining right now based on the structure of my code. Any help is appreciated.
FenceObstacle.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/BoxComponent.h"
#include "FenceObstacle.generated.h"
UCLASS()
class SCREECHTEST_API AFenceObstacle : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFenceObstacle();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Set a component to check for collisions
UPROPERTY(VisibleAnywhere)
UBoxComponent* BoxComponent;
//Create a visible mesh so we can see the fence
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisibleMesh;
//Set the height of the fence. Strider has noted that rather than stretch the Z scale of the fence from one mesh, we
// have a discreet number of possible heights to choose from. This will be denoted by a single integer.
int HeightMarker;
};
FenceObstacle.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "FenceObstacle.h"
// Sets default values
AFenceObstacle::AFenceObstacle()
{
// 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 the box component for collisions
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
check(BoxComponent != nullptr);
}
// Called when the game starts or when spawned
void AFenceObstacle::BeginPlay()
{
Super::BeginPlay();
//Create a visible mesh for the fence and set it to the appropriate reference
if (!VisibleMesh) {
VisibleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisibleMesh"));
try {
switch (HeightMarker) {
case 0:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_1'"));
break;
case 1:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_2'"));
break;
case 2:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_3'"));
break;
case 3:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_4'"));
break;
case 4:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_5'"));
break;
case 5:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_6'"));
break;
case 6:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_7'"));
break;
case 7:
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Game/Meshes/sm_obstacle_fence_size_1.sm_obstacle_fence_size_8'"));
break;
default:
throw(HeightMarker);
break;
}
if (Mesh.Succeeded()) {
VisibleMesh->SetStaticMesh(Mesh.Object);
}
}
catch (int height) {
check(GEngine != nullptr);
//Display a debug message for five seconds
//The -1 "Key" vale argument prevents the message from being updated or refreshed.
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Invalid fence height value: " << height));
}
}
}
// Called every frame
void AFenceObstacle::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}