Download

Reading values using reflection.

Hi, I try to read value of variable, but got something other.
Perhaps someone will be kind enough to explain to me what I do wrong.
Here a small class, with one variable and method reading it.


#pragma once

#include "Object.h"
#include "MyObject.generated.h"

UCLASS(BlueprintType, Blueprintable)  
class PROPTEST_API UMyObject : public UObject
{
	GENERATED_BODY()
	
public:
	UMyObject();

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test")
	int intVariable = 42;

	UFUNCTION(BlueprintCallable, Category = "Test")
	void readVarable();

protected:
	void TraceInt(int n);
	void TraceText(FString str);
};



#include "propTest.h"
#include "MyObject.h"

UMyObject::UMyObject(){}

void UMyObject::readVarable()
{
	FName variableName = TEXT("intVariable");
	UProperty* property = GetClass()->FindPropertyByName(variableName);

	const UNumericProperty* intProperty = Cast<const UNumericProperty>(property);
	int readValue = intProperty->GetSignedIntPropertyValue(this);

	TraceInt(readValue);
}

void UMyObject::TraceText(FString fstring)
{
	UE_LOG(LogTemp, Log, TEXT("%s"), *fstring);
}

void UMyObject::TraceInt(int n)
{
	FString fstring = FString::FromInt(n);
	TraceText(fstring);
}

Not really sure what you’re trying to do, but it seems like you’re going through hoops and loops when a simple code like below accomplishes the same


UMyObject* MyObject = NewObject<UMyObject>();
MyObject->TraceInt(MyObject->intVariable);

Yes.
If ever you need it, here is answer :



FName variableName = TEXT("intVariable");
UProperty* property = GetClass()->FindPropertyByName(variableName);
void* ptr = property->ContainerPtrToValuePtr<void>(this);
const UNumericProperty* intProperty = Cast<const UNumericProperty>(property);
int readValue = intProperty->GetSignedIntPropertyValue(ptr);