Hi,
I am currently trying to make a simple program, where everything is done within C++; that’s why I am avoiding using the input options within the editor. The problems that I’m having however are:
-
What parent class should I be choosing to create a C++ program that allows for a model to be loaded and changed through input from the keyboard?
-
If I can use a “World Settings” parent class, what is the reason for the following code not working as expected?
Here is the .h file currently, the code to load a model is the same from the previous version that worked with an Actor parent class:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/WorldSettings.h"
#include "Input.generated.h"
/**
*
*/
UCLASS()
class KEYPRESSING_API AInput : public AWorldSettings
{
GENERATED_BODY()
public:
AInput();
~AInput();
void SetupInput();
void Released_One();
void Released_Two();
void Released_Three();
//Changed to a UBoxComponent
UPROPERTY(EditAnywhere)
UBoxComponent* Root;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* mesh;
UPROPERTY(EditAnywhere)
UStaticMesh* model1;
UPROPERTY(EditAnywhere)
UStaticMesh* model2;
UPROPERTY(EditAnywhere)
UStaticMesh* model3;
};
The .cpp file currently:
// Fill out your copyright notice in the Description page of Project Settings.
#include "KeyPressing.h"
#include "Input.h"
AInput::AInput()
{
// 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;
//Created box component and setting it as the root component
Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
RootComponent = Root;
//Creating the mesh component and attaching it to the root component
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
mesh->AttachTo(RootComponent);
//Grabbing the mesh I'd like to use
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh1(TEXT("/Game/Models/model1"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh2(TEXT("/Game/Models/model2"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh3(TEXT("/Game/Models/model3"));
//Setting the mesh
model1 = Mesh1.Object;
model2 = Mesh2.Object;
model3 = Mesh3.Object;
mesh->SetStaticMesh(model1);
}
AInput::~AInput()
{
}
void AInput::SetupInput()
{
InputComponent->BindKey(EKeys::One, IE_Released, this, &AInput::Released_One);
InputComponent->BindKey(EKeys::Two, IE_Released, this, &AInput::Released_Two);
InputComponent->BindKey(EKeys::Three, IE_Released, this, &AInput::Released_Three);
}
void AInput::Released_One()
{
GEngine->AddOnScreenDebugMessage(-1, 4, FColor::Blue, TEXT("Pressed 1"));
mesh->SetStaticMesh(model1);
}
void AInput::Released_Two()
{
GEngine->AddOnScreenDebugMessage(-1, 4, FColor::Blue, TEXT("Pressed 2"));
mesh->SetStaticMesh(model2);
}
void AInput::Released_Three()
{
GEngine->AddOnScreenDebugMessage(-1, 4, FColor::Blue, TEXT("Pressed 3"));
mesh->SetStaticMesh(model3);
}
Any help would be appreciated, thank you.
Edit: I changed the title, so that it was a more open question.