How to find an actor variable by name?

Hi All!

I’ve found a bit of code that shows how to find a property by it’s name:

bool UKilnCodeFunctions::GetBoolByName(UObject* Target, FName VarName, bool& outBool)
{
    if (Target)
    {
        bool FoundBool;
        UBoolProperty* BoolProp = FindField<UBoolProperty>(Target->GetClass(), VarName);  
        if (BoolProp)
        {
            FoundBool = BoolProp->GetPropertyValue_InContainer(Target);
            outBool = FoundBool;
            return true;
        }
    }
    return false;

}

The base of the above code is from this website (I just changed it for a bool): http://shootertutorial.com/2016/03/20/get-set-variables-by-name/

Of course this example is for a bool, but after a few hours I’ve been unable to take this principle and extend it to an Actor variable. Any help would be great :slight_smile:

Wondering what is the use case where you don’t know what a class property name is? :face_with_monocle:

Data migration from property A to property B. If you get the value of property A by GetPropertyValue_InContainer you can copy the memory over to any other bool property

Unsure what you mean by this, do you get any errors in VS? I never used FindField but instead I use a property iterator which results in similar logic.

for (TFieldIterator<FProperty> It(GetClass()); It; ++It) {
		FProperty* Property = *It;
}

From there you can get the cpp name, type and value like you do in your example. a property has to be marked UPROPERTY to be visible to the reflection system.

1 Like

I’m making a simple editor widget where you just type in a variable name for a blueprint (picked from the level or found by class when playing) then it displays it in a list for debugging purposes. It was just meant to be a simple way of debugging variables whilst playing without having to bother going into that specific BP and printing to screen on the Tick.

Hey, I actually saw your other post in the forums and ended up with something like this:

FObjectProperty* ObjectProp = FindObjectSafe<FObjectProperty>(Target->GetClass(), *VarName, false);
		
        void* ValueAddress = ObjectProp->ContainerPtrToValuePtr< void >(Target);

		UObject* PropertyObjectValue = ObjectProp->GetObjectPropertyValue(ValueAddress);

        if (PropertyObjectValue != nullptr)
		{
			return true;
		}

(I simply want to know if the actor variable is valid for debugging purposes, as described in by previous post)

The problem is that I need to get the value of the property by a string. So I think what I’m looking for instead of “FindPropertyByName” but FindOBJECTPropertyByName, which doesn’t exist and I’m not well versed enough in UE4 C++ to figure out the alternative!

Found the solution I was looking for:

bool UKilnCodeFunctions::GetActorByName(UObject* Target, FName VarName)
{
    if (Target)
    {
		UProperty* PropertyByName = Target->GetClass()->FindPropertyByName(VarName);
        FObjectProperty* ObjProp = Cast <FObjectProperty>(PropertyByName);
		UObject* thisObj = ObjProp->GetObjectPropertyValue_InContainer(Target);

        if (ObjProp)
        {
			if (thisObj)
			{
				return true;
			}
        }

    }
    return false;
}

Turns out I was just a little dim and didn’t think of looking for the equivalent “GetPropertyValue_InContainer” for FObjectProperty, which ended up being, you guessed it, “GetObjectPropertyValue_InContainer”