# Copying FProperty from struct to struct?

**URL:** <https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 26, 2022, 10:38pm UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108 "2022-06-26T22:38:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [June 26, 2022, 10:38pm UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/1 "2022-06-26T22:38:43Z")

</div>

I am looping through properties on a class, then I check if the property is a struct and if so loop through the struct etc. Eventually I reach an FProperty that I want to copy to another struct.

```auto
FProperty TargetProperty = StructProperty->Struct->FindPropertyByName(FName("SomeProperty"));

```

Normally I’d copy like this, if the copy is a simple class member to another class member (no nesting):

```auto
void* TargetAddress = TargetProperty->ContainerPtrToValuePtr<void>(InTargetWidget);
TargetProperty->CopyCompleteValue(TargetAddress, InNewValueAddress);

```

How to do it with a property nested in a UStruct? or even a struct in a struct?

 ![afbeelding](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/d/7/8d7628f22c0805049ccf77ce8fd0093d7b8f9178.png)

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [June 27, 2022, 9:12am UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/2 "2022-06-27T09:12:31Z")

</div>

Get a pointer to the struct instance.  
For properties within struct, the “container” is the struct instance, not the object instance.  
You can go as deep as you want.

```cpp
FStructProperty* StructProp = (FStructProperty*)ObjectClass->FindPropertyByName("MyStruct");
void* StructInst = StructProp->ContainerPtrToValuePtr<void>(Object);
FProperty* TargetProp = StructProp->Struct->FindPropertyByName("SomeProp");
void* TargetAddress = TargetProp->ContainerPtrToValuePtr<void>(StructInst);

```

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [June 29, 2022, 8:36pm UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/3 "2022-06-29T20:36:09Z")

</div>

Thanks for your reply. I am currently implementing the new code in an existing system. I will reply soon.

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [July 1, 2022, 9:34am UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/4 "2022-07-01T09:34:36Z")

</div>

Thanks, this works well.

I have one small problem now that I’m storing struct data in a void pointer. I can get default values from a UObject already but not from a struct, possibly because the cast from void\* back to (what I think should be) FStructProperty fails?

, I can’t cast it back from void to struct without getting a memory violation. Is there a workaround?:

```auto
auto FindNestedProperty = [&InPropertyPath](FProperty*& OutProperty, void*& OutContainer) {
	for (int i = 1; i < InPropertyPath.Num(); i++) {
		if (const FStructProperty* StructProperty = CastField<FStructProperty>(OutProperty)) {
			// Get the struct instance as new OutContainer.
			OutContainer = StructProperty->ContainerPtrToValuePtr<void>(OutContainer);
			
			/// .....
			/// .....
		}
	}
};

//// .....
//// .....

FStructProperty* ContainerStruct = (FStructProperty*)OutContainer;
// Memory violation at ContainerStruct->Struct.
SourceAddress = TargetProperty->ContainerPtrToValuePtrForDefaults<void>(ContainerStruct->Struct, ContainerStruct);

```

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [July 1, 2022, 10:01am UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/5 "2022-07-01T10:01:16Z")

</div>

You are mixing up types.  
Container is not a FStructProperty. FStructProperty is found via UClass/UStruct → FindPropertyByName (or TPropertyIterator).  
FStructProperty doesn’t contain any value, it contains information about the property within its containing class/struct.  
With FStructProperty-\>ContainerPtrToValuePtr you get a pointer to the struct values in memory.

- FStructProperty = the reflection info
- void\* container = the instance values

These two objects are in completely different places in memory.

If your nested property is of type FVector, then you could cast OutContainer to FVector\*.

You probably want to add the iterating StructProperty (or StructProperty-\>Struct) as an out parameter in your lambda, so you get both the struct instance and its reflection data.

Alternatively you can probably access the UScriptStruct back from FProperty via `Cast<UScriptStruct>(OutProperty->GetOuter())` but I’m not 100% sure about that.

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [July 1, 2022, 6:35pm UTC](https://forums.unrealengine.com/t/copying-fproperty-from-struct-to-struct/591108/6 "2022-07-01T18:35:46Z")

</div>

It all works now
