Set variable from parent class through it's child class

Hi, C++ newbie here but experienced Blueprinter. What I’m trying to do is simply understand how I should do the simplest thing possible through the C++. With blueprint experience behind I know the logic of what I want to do, but I have lack of coding experience, especially at specific UE4 API.


So my goals are:

-create children class from main class,

-set default value for inherited variable

-add one more new value,

-make another inherited variable be always set as this new one.


Doing such things via blueprints is extremely easy task to me, because I see what happens without worrying of syntax and order of things, but I faced the situation where I need go beyond blueprints and have to solve the task with C++.

What I already found
is tons of abstract examples or something not related to inherited variables and setting the values. I found how to add new variable to inherited class, but it’s not what I’m looking for actually (from my current perspective and experience).
So if it’s possible, make some real example on real UE4 class, not something abstract, it will help not only to me but to those who searching for the same thing as well, I’m sure of it. Please, help if it’s easy for you. If possible, pick some simple class, vehiclewheel for example, it’s very short and simple to understand.

Thanks in advance!
Best regards.

-create children class from main class,

The easiest way to do this is to use the UE4 Editor. Choose “New C++ Class…” from the File menu. Then choose that parent class you want to inherit from.

-set default value for inherited variable

Here is how to set a default value for an inherited variable. The default value is 5. You can also set default values in your base class constructor. This code goes in your .h file. I am using “protected” here which allows the variable to be modified in inherited classes.

protected:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCategory")
		int32 MyInt = 5;

If you want to do it in the constructor instead add MyInt = 5; to the constructor in your cpp file.

// Sets default values
ARPGChatManager::ARPGChatManager()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	MyInt = 5;
}

-add one more new value,

Not sure what this means. If you want to add a variable to the child class, but not the parent, you can do that the same why I defined the protected variable above. Just add the code to the child and not the parent.

-make another inherited variable be always set as this new one.

You will have to be more clear. This doesn’t make sense.

Thanks a lot for such structured answer and your input!! Also I came back here with the answer for my questions, and hope this will help someone else as well as it helped to me.

So basically I tried to get and set different variables to the base parent class ‘VehicleWheel.h’ . There was no options to get current value of ‘DebugLongSlip’ for example through the blueprints directly. Also there is no real way to set any parameter via blueprint, for example set the LongStiffValue.

Here how I solved this (with mighty help of my friend):

  1. Created derived class based on VehicleWheel.h , as you mentioned in your answer

  2. Any new variables can be easily added with such structure:

    class CPPTest_API UMyVehicleWheel : public UVehicleWheel
    {

    public:

     UPROPERTY(EditAnywhere, BlueprintReadWrite)
     float PirogsTestValue = 5;
    

    };

This allowed me to add a float variable “PirogsTestValue” with default value of 5. Already great (to me :wink: )

  1. To get something from the parent class I’ve created .cpp, placed it in the same folder as .h file, and added those strings at the beginning:

    #include “CPPTest.h”
    #include “MyVehicleWheel.h”
    #include “Vehicles/VehicleWheel.h”
    #include “…/Public/MyVehicleWheel.h”

then, in .h file I’ve added simple function:

#include "Vehicles/VehicleWheel.h"
#include "MyVehicleWheel.generated.h"

/**
 * 
 */


UCLASS()
class CPPTest_API UMyVehicleWheel : public UVehicleWheel
{
	GENERATED_BODY()
	
	UFUNCTION(BlueprintPure, Category = "Dynamic Read-Only")
	float GetDebugLongForce();

public:

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float PirogsTestValue = 5;


};

and added execution part to the .cpp file, to correspond this function and to be able to get the value from the parent class:

#include "CPPTest.h"
#include "MyVehicleWheel.h"
#include "Vehicles/VehicleWheel.h"
#include "../Public/MyVehicleWheel.h"

float UMyVehicleWheel::GetDebugLongForce()
{
	return DebugLongForce;
}

And as continuation: to set the value of the parent class, I’ve added few more things.

It was another one function at the .h file, just below the ‘get’ (previous one):

	UFUNCTION(BlueprintCallable, Category = "MovementParameters")
	void SetMaxBrakeTorque(float value);

and this went to the .cpp file, which is ‘action’ for the function:

void UMyVehicleWheel::SetMaxBrakeTorque(float value)
{
	MaxBrakeTorque = value;
};

So now I’m able to ‘set’ and to ‘get’ the variables, which was not ‘opened’ to the blueprints interface.

http://puu.sh/tjTfE/6c61f73bcf.jpg

I hope my answers will help novice programmers as me to create more complex and interesting things :slight_smile:
Best regards!

can GetClassDefaults return the default values of an inherited class?