# Set/Get UProperty inside of USTRUCT directly

**URL:** <https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239>\
**Category:** Editor Scripting\
**Tags:** UProperty, ustruct, unreal-engine, UE4, question, CPP\
**Created:** [April 16, 2015, 10:34am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239 "2015-04-16T10:34:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![szyszek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@szyszek](https://forums.unrealengine.com/u/szyszek)\
**Post date:** [April 16, 2015, 10:34am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/1 "2015-04-16T10:34:18Z")

</div>

Hi,

I’d like to set&get ustruct property value. What I have is

`UProperty* Property` - property that I want to get (float)

`UObject* PropertyOwner` - owner of this property (UStruct in this case)

For example: I’m trying to get `FilmContrast` value from `FPostProcessSettings` struct inside camera component.

So normally I would do something like this: `*Property->ContainerPtrToValuePtr(PropertyOwner)` to get this property value. But because `PropertyOwner` is a `UStruct` the line above crashes.

I’ve seen some examples in the source code that uses `ContainerPtrToValuePtr` method but I’m not sure if this is the right way to get into ustruct properties and how exactly can i do it.

Any help/advice will be greatly appreciated,

Cheers

---

<div class="post-metadata">

**Author:** ![MickD777](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MickD777](https://forums.unrealengine.com/u/MickD777)\
**Post date:** [April 16, 2015, 10:55am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/2 "2015-04-16T10:55:57Z")

</div>

I’m not sure of what you are trying to do, you want to get the value from a default property of a struct? Or just access the property value from an instance of a struct? Or something else?

---

<div class="post-metadata">

**Author:** ![szyszek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@szyszek](https://forums.unrealengine.com/u/szyszek)\
**Post date:** [April 16, 2015, 10:59am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/3 "2015-04-16T10:59:11Z")

</div>

I want to get/set current value of the property in this struct instance, not the default value.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_7a9aff171](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_7a9aff171](https://forums.unrealengine.com/u/anonymous_user_7a9aff171)\
**Post date:** [July 28, 2015, 2:41am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/4 "2015-07-28T02:41:25Z")

</div>

Have you tried casting it to UFloatProperty and then calling GetPropertyValue\_InContainer?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_10b75a231](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_10b75a231](https://forums.unrealengine.com/u/anonymous_user_10b75a231)\
**Post date:** [May 4, 2017, 2:10am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/5 "2017-05-04T02:10:14Z")

</div>

Structs are a little different than UObjects, but you can get and set their members like so:

```
float GetSetVectorX(UObject * ObjectWithVector)
{
	FName PropName = FName("MyVector");
	FName VectorMemberName = FName("X");
	float NewValue = 42.0;
	
	// Get property representing struct
	UProperty * Prop = ObjectWithVector->GetClass()->FindPropertyByName(PropName);
	
	// Ensure ObjectWithVector actually has a myVector member
	if (Prop)
	{
		// Get struct address
		void * StructAddress = Prop->ContainerPtrToValuePtr<void>(ObjectWithVector);
		
		// Ensure MyVector really is a vector
		if (UStructProperty * StructProp = Cast<UStructProperty>(Prop))
		{
			// We'll get struct properties through a UScriptStruct, not a UObject
			// ScriptStructs are just templates and don't hold any of your data
			UScriptStruct * ScriptStruct = StructProp->Struct;
			
			// Get the vector's "X" property
			UProperty * ChildProp = ScriptStruct->FindPropertyByName(VectorMemberName);

			// Cast to FloatProperty
			if (UFloatProperty * ChildFloatProp = Cast<UFloatProperty>(ChildProp))
			{
				// Get
				float OldValue = ChildFloatProp->GetFloatingPointPropertyValue(StructAddress);
				
				// Set
				ChildFloatProp->SetFloatingPointPropertyValue(StructAddress, NewValue);
				
				return OldValue;
			}
		}
	}
	return 0;
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_10b75a231](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_10b75a231](https://forums.unrealengine.com/u/anonymous_user_10b75a231)\
**Post date:** [May 4, 2017, 2:36am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/6 "2017-05-04T02:36:02Z")

</div>

I should note that (at least in my environment) Structs created in blueprints have invisible extensions added to their member names. So a member called “CrazyName” might look like this:

CrazyName\_2\_8879DD6E49624206B15E6DB35EFD0E09

It’s not exactly elegant, but I parse all the Struct’s members into a map beforehand:

```
UScriptStruct * ScriptStruct = StructProp->Struct;

// FriendlyNames => BlueprintNames
TMap<FString, FString> BlueprintNames;

// Is this Struct not made with Cpp?
if (!ScriptStruct->IsNative())
{
	// Get Struct's first member
	UField * Field = ScriptStruct->Children;
	while (Field)
	{
		// Get Blueprint name
		FString FieldName = Field->GetName();
		
		// Chop off the second-to-last underscore (_)
		int index = FieldName.Len();
		for (int i = 0; i < 2; ++i)
		{
			index = FieldName.Find(FString("_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd, index);
		}
		
		// Get the friendly name
		FString MemberName = FieldName.Left(index);

		// Add to map
		BlueprintNames.Add(MemberName, FieldName);

		// Go to next member
		Field = Field->Next;
	}
}

```

Then I get and set the Struct’s members as normal:

```
// Only difference:
FString LiteralName = FString("CrazyName");
FName MemberName = FName(*(ScriptStruct->IsNative() ? LiteralName : BlueprintNames[LiteralName]));

// Normal business
UProperty * Prop = ScriptStruct->FindPropertyByName(MemberName);
// ... Get / Set logic ...

```

---

<div class="post-metadata">

**Author:** ![dbrueck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dbrueck](https://forums.unrealengine.com/u/dbrueck)\
**Post date:** [November 21, 2017, 8:28pm UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/7 "2017-11-21T20:28:07Z")

</div>

I know this is an old question, but I arrived here while trying to do something similar, so I’ll add what worked for me. If the object in question has a UPROPERTY that is a struct, then you can just cast it to a UStructProperty and then ContainerPtrToValuePtr does what you need.

For example, for an object with an FVector property inside it:

```
    if (UStructProperty *p = Cast<UStructProperty>(Property))
    {
        FVector *v = p->ContainerPtrToValuePtr<FVector>(PropertyOwner);
        v->X = 10.0;
        v->Y = 12.0;
        v->Z = 3.0
    }
```

---

<div class="post-metadata">

**Author:** ![TheGreenMat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheGreenMat](https://forums.unrealengine.com/u/TheGreenMat)\
**Post date:** [December 5, 2018, 3:14am UTC](https://forums.unrealengine.com/t/set-get-uproperty-inside-of-ustruct-directly/315239/8 "2018-12-05T03:14:53Z")

</div>

If for some reason you need to be able to serialize and deserialize your UStructProperty, or any other type of property for that matter, regardless of what type it is, then you can use the following to get its value as a byte array.

```
TArray<uint8> ByteArray;
FMemoryWriter MemoryWriter(ByteArray);
FObjectAndNameAsStringProxyArchive Ar(MemoryWriter, false);
Ar.ArNoDelta = true;

Property->SerializeBinProperty(Ar, PropertyOwner);

```

In theory you should be able to then type cast it to your struct type if you know what it ought to be

Then to load the property from your byte array you could do

```
FMemoryReader MemoryReader(ByteArray);
FObjectAndNameAsStringProxyArchive ReadAr(MemoryReader, false);
ReadAr.ArNoDelta = true;
			
Property->SerializeBinProperty(ReadAr, PropertyOwner);

```

This is mostly useful if you’re creating something like a save system
