How to set value for a variable from a data table?

I have this:

USTRUCT(BlueprintType)
struct FST_CameraHeight : public FTableRowBase 
{
	GENERATED_BODY()

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	float Height; 
};
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = CameraHeight, meta = (AllowPrivateAccess = true))
class UDataTable* DT_CameraHeight;

I want to get the float variable Height and set its value to another variable.
Height = NewHeight;

I don’t want to change the values in data table, but I want to read them and assign them to another variable.
in my case, the float type variable Height is defined under the data table with the value 100
and I want to assign this value 100 to another variable NewHeight , so Newheight = 100

Try it , not sure it will work or not , I am not at home, will try it myself too.

FST_CameraHeight* Row = DT_CameraHeight->FindRow<Height>(TEXT("1"), TEXT(""));
if (Row)
{
    Height = NewHeight;
}

I don’t want to change the values in data table, but I want to read them and assign them to another variable.
in my case, the float type variable Height is defined under the data table with the value 100
and I want to assign this value 100 to another variable NewHeight, so Newheight = 100

not working :frowning:

Triedy this still not working:

FST_CameraHeight* Row = DT_CameraHeight->FindRow<FST_CameraHeight>(TEXT("Height"), TEXT(""));
			if (Row)
			{
				Row->Height =  NewHeight;
			}

change (TEXT(“Height”) to FName(TEXT(“Height”)

again just try, not sure will work or not

1 Like

Not working, please send me a working solution when you have it working on your end. Thank You

Replace the path to your datatable and the name of the desired row in the table.

if (const UDataTable* DataTable{ LoadObject<UDataTable>(GetWorld(), TEXT("DataTable'/Game/Path/To/MyDataTable.MyDataTable'")) })
	if (const FST_CameraHeight* Row{ DataTable->FindRow<FST_CameraHeight>("MyRowName", "") })
		float MyHeight{ Row->Height };
2 Likes

I declared the variable float MyHeight in .h file as a global variable, I am doing it wrong?

You can declare it anywhere, in my example, I declared it inside the last scope, but you can use a member property.

1 Like

Ok, thank You, I will try it once my engine build completed, for some reason it starts rebuilding the engine… I mistakenly starts typing in the engine.h file, and quickly undo the changes, started to build the project and now the engine want to be build :frowning: it will take 1 hour.

For example:

DT_CameraHeight = LoadObject<UDataTable>(GetWorld(), TEXT("DataTable'/Game/Path/To/MyDataTable.MyDataTable'"));

if (DT_CameraHeight)
	if (const FST_CameraHeight* Row{ DT_CameraHeight->FindRow<FST_CameraHeight >("MyRowName", "") })
		Height = Row->Height;
3 Likes

Very Understandable, I hope it will work. Thank You )

Finally I managed to re-Install and rebuild the engine from source, Just tested your example and it works perfectly but only with pointer and direct assignment seems to be not finding the data table.
Now it looks like this in my code according to your pointer version example and working:

if (const UDataTable* DT_CameraHeight{ LoadObject<UDataTable>(GetWorld(), TEXT("DataTable'/Game/Datasbase/DataTables/Camera/DT_CameraHeight.DT_CameraHeight'")) })
			{
				if (const FST_CameraHeight* Row{ DT_CameraHeight->FindRow<FST_CameraHeight >("Crouch_Normal_Move", "") })//character camera position should change according to the pose.
				{
					NewHeight = Row->Height;
					printf("The new value of NewHeight is: %f", NewHeight);
				}
			}
			else
			{
				printf("The value for NewHeight has not been modified: %f", NewHeight);
			}

so, printf(“The new value of NewHeight is: %f”, NewHeight); prints the value assigned from Height
This is just an amazing approach in my learning process and thank you very much bro for helping me out.