Download

Trying to call protected function from derivated class

Hi,

i know, these are maybe C++ basics, but i can’t find a solution or maybe i just don’t understand it.
My goal is to change the FinalRatio of a Gearbox ingame (for a Low- and High-Gear Setup).

At first, i tried this:


Vehicle4W->TransmissionSetup.FinalRatio = 8.0;

But this doesn’t work.

I searched a little bit and found an answer in the answer hub. Click.
So i tried this:



VehicleMovementComponent4W_Setup.h:
#pragma once

#include "Vehicles/WheeledVehicleMovementComponent4W.h"
#include "VehicleMovementComponent4W_Setup.generated.h"

UCLASS()
class VEHICLE_API UVehicleMovementComponent4W_Setup : public UWheeledVehicleMovementComponent4W
{
	GENERATED_BODY()
	void UpdateTransmission(const FVehicleTransmissionData& NewGearSetup);	
};

VehicleMovementComponent4W_Setup.cpp:
#include "Vehicle.h"
#include "VehicleMovementComponent4W_Setup.h"

void UpdateTransmission(const FVehicleTransmissionData& NewGearSetup)
{
	UpdateTransmissionSetup(NewGearSetup);
}


The “UpdateTransmissionSetup()” function is declared as protected (Vehicles\WheeledVehicleMovementComponent4W.h). My thought was that this will update the Transmission - but with the code above i’m not able to access this function. But why? Google says a child can access the protected function, and that is also what my C++ book said (sadly its not going deeper into this), but the compiler throws an error: “Source\Vehicle\VehicleMovementComponent4W_Setup.cpp(7): error C3861: ‘UpdateTransmissionSetup’: identifier not found”

Any help? Or Ideas? Thanks in advance.

You need to change your UpdateTransmission definition to be:


void UVehicleMovementComponent4W_Setup::UpdateTransmission(const FVehicleTransmissionData& NewGearSetup)
{
	UpdateTransmissionSetup(NewGearSetup);
}

You’re missing the UVehicleMovementComponent4W_Setup:: which marks it as a definition for that class, instead of just some static method somewhere, which is why it can’t find the UpdateTransmissionSetup parent method.

You are right… ****, thats stupid. Tripped so often over this… Missing the forest for the trees…

Thanks a lot!

Happens to all of us. Occam’s Razor tends to win out more often than not when debugging. :slight_smile: Cheers.

Tried the last hour to figure it out by myself, but i’m stuck again.
Replacing this:


UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());

with that:


UVehicleMovementComponent4W_Setup* Vehicle4W = CastChecked<UVehicleMovementComponent4W_Setup>(GetVehicleMovement());

Results in:

I’m working btw with the C++ Advanced Vehicle Example.
Would be nice, if you (or someone else) could explain me whats wrong here?

The cast is failing, whatever object GetVehicleMovement is returning, isn’t of type UVehicleMovementComponent4W_Setup.