Announcement

Collapse
No announcement yet.

Reading values using reflection.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.
    Code:
    #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);
    };
    Code:
    #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);
    }

    #2
    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

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

    Comment


      #3
      Not really sure what you're trying to do
      Yes.
      If ever you need it, here is answer :
      Code:
      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);

      Comment

      Working...
      X