How to set a vector variable by name? [C++]

Sorry, noobs question.
Cant found property class for vector (vector3).

Example with float:

bool UShooterFunctions::SetFloatByName(UObject * Target, FName VarName, float NewValue, float & outFloat)
{
    if (Target)
    {
        float FoundFloat;
        UFloatProperty* FloatProp = FindField<UFloatProperty>(Target->GetClass(), VarName);
        if (FloatProp)
        {
            FloatProp->SetPropertyValue_InContainer(Target, NewValue); //this actually sets the variable
            FoundFloat = FloatProp->GetPropertyValue_InContainer(Target);
            outFloat = FoundFloat;
            return true;
        }
    }
    return false;
}

But how to do this with vector properties?
Thx for reply.

Hey Mr.Lachetti-

Are you referring to trying to set the X,Y,and Z after line 5 or are you referring to the code used in line 6?

If you’re tying to set the XYZ valuesthen you should be able to do [vectorVar].X [vectorVar].Y and [vectorVar]z=Z. If you are attempting to get another piece of information from the variable, please indicate exactly what you’re looking for.

Hi ,

I sent you a PM, respond whenever you have a chance.
Thanks.

Hey ,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will follow up.

Cheers

For anyone who’s still curious, here’s an example of setting a vector property (or any other struct) in C++:

FName InPropName = "MyVector";
// Ensure InObject is valid
if (InObject)
{
	Prop = InObject->GetClass()->FindPropertyByName(InPropName);
	// Ensure InObject has this property
	if (Prop)
	{
		void* ValuePtr = Prop->ContainerPtrToValuePtr<void>(InObject);

		// Ensure this property is a struct
		if (UStructProperty * StructProp = Cast<UStructProperty>(Prop))
		{
			// Ensure this struct is a Vector
			if (StructProp->Struct->GetName() == "Vector")
			{
				// Set Vector to value
				const FVector NewValue = FVector(1234, 3452.938475, -34);
				StructProp->CopyCompleteValue(ValuePtr, &NewValue);
			}
		}
		// Commit changes
		Prop->PostEditChange();
	}
}
1 Like

Here is my code based on FoxInFreefall’s code:

bool UFunctionLibrary::GetVectorByName(UObject *Target, FName VarName, EGABPLibraryReturnExecEnum &ExecBranches, FVector &outVector)
{
	ExecBranches = EGABPLibraryReturnExecEnum::Failed;

	if(Target)
	{
		UProperty *Prop = Target->GetClass()->FindPropertyByName(VarName);

		if(Prop)
		{
			void* StructAddress = Prop->ContainerPtrToValuePtr<void>(Target);

			if(StructAddress)
			{
				// Ensure this property is a struct
				if(UStructProperty * StructProp = Cast<UStructProperty>(Prop))
				{
					// Ensure this struct is a Vector
					if(StructProp->Struct->GetName() == "Vector")
					{
						StructProp->CopyCompleteValue(&outVector, StructAddress);
						ExecBranches = EGABPLibraryReturnExecEnum::Success;
						return true;
					}
				}
			}

		}
	}
	return false;
}

bool UFunctionLibrary::SetVectorByName(UObject *Target, FName VarName, const FVector &Value, EGABPLibraryReturnExecEnum &ExecBranches)
{
	ExecBranches = EGABPLibraryReturnExecEnum::Failed;

	if(Target)
	{
		UProperty *Prop = Target->GetClass()->FindPropertyByName(VarName);

		if(Prop)
		{
			void* StructAddress = Prop->ContainerPtrToValuePtr<void>(Target);

			if(StructAddress)
			{
				// Ensure this property is a struct
				if(UStructProperty * StructProp = Cast<UStructProperty>(Prop))
				{
					// Ensure this struct is a Vector
					if(StructProp->Struct->GetName() == "Vector")
					{
						// Set Vector to value
						StructProp->CopyCompleteValue(StructAddress, &Value);

						// Commit changes
						Prop->PostEditChange();

						ExecBranches = EGABPLibraryReturnExecEnum::Success;
						return true;
					}
				}
			}
		}
	}
	return false;
}
1 Like

I realized later an alternative to

if (StructProp->Struct->GetName() == "Vector")

might be

if (StructProp->Struct == TBaseStructure<FVector>::Get())

That way it’s a little easier to refactor or search your codebase. It should work, as per [this question][1].