I have been following along this Remark in this documentation article here.
And then, following the default AWheeledVehicle subclass generated when creating a basic C++ vehicle template, I got myself stuck on the line that is casting and checking the VehicleMovementComponentName part. I have placed a comment pointing out the error-causing line of code, so you can’t miss it.
In short, I can’t seem to run pass this line. What should I do?
[HR][/HR]
AMainVehicle::AMainVehicle(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UNormalMovement>(TEXT("NormalMovement"))) {
//Static Mesh
this->StaticMesh = this->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshObject(TEXT("StaticMesh'/Game/Meshes/Test_Cube_Mesh.Test_Cube_Mesh'"));
if (StaticMeshObject.Succeeded()){
this->GetStaticMesh()->SetStaticMesh(StaticMeshObject.Object);
this->SetRootComponent(this->GetStaticMesh());
}
//Wheels
/* CRASHED AT THIS NEXT LINE HERE ------v */
UNormalMovement* NormalMovement = CastChecked<UNormalMovement>(this->GetVehicleMovement(), ECastCheckedType::NullChecked);
//Don't care about the wheel count later on.
//check(NormalMovement->WheelSetups.Num() == 4);
NormalMovement->WheelSetups[0].WheelClass = UFrontWheelComponent::StaticClass();
NormalMovement->WheelSetups[0].AdditionalOffset = FVector(0.0f, -5.0f, 0.0f);
NormalMovement->WheelSetups[1].WheelClass = UFrontWheelComponent::StaticClass();
NormalMovement->WheelSetups[1].AdditionalOffset = FVector(0.0f, 5.0f, 0.0f);
//Spring Arm Component
this->SpringArm = this->CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
this->SpringArm->SetRelativeLocation(FVector(0.0f, 0.0f, 34.0f));
this->SpringArm->SetWorldRotation(FRotator(-20.0f, 0.0f, 0.0f));
this->SpringArm->AttachTo(this->GetRootComponent());
this->SpringArm->TargetArmLength = 150.0f;
this->SpringArm->bEnableCameraLag = false;
this->SpringArm->bEnableCameraRotationLag = false;
this->SpringArm->bInheritPitch = true;
this->SpringArm->bInheritYaw = true;
this->SpringArm->bInheritRoll = true;
//Main Camera
this->MainCamera = this->CreateDefaultSubobject<UCameraComponent>(TEXT("MainCamera"));
this->MainCamera->AttachTo(this->SpringArm, USpringArmComponent::SocketName);
this->MainCamera->SetRelativeRotation(FRotator(20.0f, 0.0f, 0.0f));
this->MainCamera->bUsePawnControlRotation = false;
this->MainCamera->FieldOfView = 90.0f;
}
Update 3:
Even more confusing is this. If I made NormalMovement class be a subclass of UWheeledVehicleMovement4W, and try to cast GetVehicleMovement() to NormalMovement, the casting will fail. I really don’t know why, or how it was failing to cast it.
[hr][/hr]
Update 2:
Someone pointed me out that I need to use a different function for vehicle movement. I changed the MoveForward() and MoveRight() codes to this:
void AMainVehicle::MoveForward(float Value){
if ((this->GetController() != nullptr) && (Value != 0.0f)){
this->GetVehicleMovementComponent()->SetThrottleInput(Value);
LOG((Value < 0.0f ? "Reversing backwards..." : "Throttling forwards..."));
}
}
void AMainVehicle::MoveRight(float Value){
if ((this->GetController() != nullptr) && (Value != 0.0f)){
this->GetVehicleMovementComponent()->SetSteeringInput(Value);
LOG((Value < 0.0f ? "Steering left..." : "Steering Right..."));
}
}
This is what happens after I updated the code:
[hr][/hr]
Update 1:
What I did later on was I called on:
this->GetVehicleMovement()
To obtain UWheeledVehicleMovement, and set the WheelSetups elements one by one through the given pointer. This also means that, unfortunately for me, I cannot make a subclass of UWheeledVehicleMovement and cast the subclass into the class I made in the constructor. Anyone who can point out what I did would be grateful!
Here’s how it looks right now:
And this is my MoveForward and MoveRight functions:
void AMainVehicle::MoveForward(float Value){
if ((this->GetController() != nullptr) && (Value != 0.0f)){
FRotator Rotation = this->GetController()->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
this->AddMovementInput(Direction, Value);
LOG((Value < 0.0f ? "Moving backwards..." : "Moving forwards..."));
}
}
void AMainVehicle::MoveRight(float Value){
if ((this->GetController() != nullptr) && (Value != 0.0f)){
FRotator Rotation = this->GetController()->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
this->AddMovementInput(Direction, Value);
LOG((Value < 0.0f ? "Moving left..." : "Moving Right..."));
}
}
My vehicle is not moving at all. Am I doing it correctly? I am still not giving up, but it would be helpful if someone can point me in the right direction. Thanks in advance.
Hello asperatology,
Maybe I’m a bit late but this info can probably help other members.
I’ve experienced the same problem, and have found the solution to get rid of the crash.
When you write something like
Super(ObjectInitializer.SetDefaultSubobjectClass<UNormalMovement>(TEXT("NormalMovement")))
,
you must use the original name from the inherited component (I think that’s because SetDefaultSubobjectClass can work for another subobjects).
So, you should write
Super(ObjectInitializer.SetDefaultSubobjectClass<UNormalMovement>(AWheeledVehicle::VehicleMovementComponentName))