# How to get value of some USTRUCT, while only having access to its UScriptStruct and memory adress?

**URL:** <https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339>\
**Category:** C++\
**Tags:** unreal-engine, question, CPP\
**Created:** [February 1, 2023, 12:25pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339 "2023-02-01T12:25:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzemzmcdonalda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dzemzmcdonalda/32/1214680_2.png) [@dzemzmcdonalda](https://forums.unrealengine.com/u/dzemzmcdonalda)\
**Post date:** [February 1, 2023, 12:25pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/1 "2023-02-01T12:25:01Z")

</div>

_ **Context:** _  
At some point in code, I want to get the value of struct (USTRUCT) which type is known at compile time, but during runtime I have only access to its UScriptStruct\* and void\* of its initialized memory adress.

* * *

_ **Example:** _  
With additional access to the FStructProperty of this struct (e.g. during call of CustomThunk from FFrame Stack), I could probably use something like this:

```auto
template<class T>
T GetStructValue(const FStructProperty* StructProperty, const void* StructContainer)
{
	T StructValue = *StructProperty->ContainerPtrToValuePtr<T>(StructContainer);
	return StructValue;
}

```

Where `FStructProperty::ContainerPtrToValuePtr` does the whole job.

* * *

_ **My Problem:** _  
As mentioned before, in my case I don’t have access to FStructProperty, and I’m only left with:

```auto
template<class T>
T GetStructValue(const UScriptStruct* StructType, const void* StructContainer)
{
	/**
	* How to find StructValue in this case?
	*/
	
	return StructValue;
}

```

Is there a way to do that with what I pass to this function (UScriptStruct\*, void\*)?  
Can I somehow instantiate `FStructProperty`, or use any equivalent of `FStructProperty::ContainerPtrToValuePtr` which would work in this case?

* * *

I’m rookie when it comes to working on structs, FProperty’ies and Reflection data, so I’m probably missing something here. Please let me know in case more context or any other info is needed.

I’ll be thankful for any kind of help 🙂

---

<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:** [February 1, 2023, 12:51pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/2 "2023-02-01T12:51:26Z")

</div>

As long as the property is “reflected” (UPROPERTY) then maybe something similar to this:

> [@Safely find a reflected property name?](https://forums.unrealengine.com/t/safely-find-a-reflected-property-name/442607):
>
> Hi all… I need to access a UPROPERTY of a UCLASS through reflection in C++. It shouldn’t present any challenge in itself, something along the lines of: MyObject-\>GetClass()-\>FindPropertyByName(TEXT("MyPropertyName")); However I’m absolutely certain that there’s a preprocessor macro that provides safe way to reference a property name in C++, that will throw a compile time error in the event that the name changes. Something like this: FName PropertyName = MAGIC\_PROPERTY\_MACRO(MyClass::StaticCl…

You would need the name of the struct property though. You can’t possibly make the code know what struct to pick from the container only by a type.

---

<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:** [February 1, 2023, 12:57pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/3 "2023-02-01T12:57:58Z")

</div>

Other more general examples of how to work with them:

```auto
FProperty* SomeProperty = StructProperty->Struct->FindPropertyByName(InPropertyPath);

```

```auto
void* SourceAddress = TargetProperty->ContainerPtrToValuePtrForDefaults<void>(OutStructProperty->Struct->GetClass(), ContainerPtr);

```

---

<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:** [February 1, 2023, 3:16pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/4 "2023-02-01T15:16:04Z")

</div>

You don’t need an FStructProperty, `void* StructContainer` _ **is** _ the struct value.  
All you can do is this (I recommend sticking with pointer rather than returning by value tho)

```cpp
template<class T>
T* GetStructValue(const UScriptStruct* StructType, const void* StructContainer)
{
	return (T*)StructContainer;
}

```

But this is essentially useless, just cast directly from calling code

```cpp
FSomeStruct* SomeStruct = GetStructValue<FSomeStruct>(StructType, Container);
// same as this
FSomeStruct* SomeStruct = (FSomeStruct*)Container;

// if u really want to dereference to value
FSomeStruct SomeStruct = *(FSomeStruct*)Container;

```

* * *

In your first example there’s a misunderstanding - an FStructProperty describes the position of a struct within a larger container, like an UClass or another encompassing struct.

* * *

If that’s not the answer you are looking for, I’m gonna need more context to understand what you’re trying to do as it doesn’t make sense otherwise.

---

<div class="post-metadata">

**Author:** ![dzemzmcdonalda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dzemzmcdonalda/32/1214680_2.png) [@dzemzmcdonalda](https://forums.unrealengine.com/u/dzemzmcdonalda)\
**Post date:** [February 2, 2023, 9:35am UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/5 "2023-02-02T09:35:55Z")

</div>

@Roy_Wierer.Seda145  
The function won’t have access to any UPROPERTY of the desired type.  
So this approach won’t work in my case.  
Thank You however, it’s good to know that It could be done in such way.

* * *

@Chatouille  
Yes, this… this works… I cannot believe it was already there, right in front of me, all the time…  
I thought that memory initialized by UScriptStuct differs from the actual cpp type.  
I’m shocked it actually works in this way, but I guess I’ll take it as it is 😃

> [@Chatouille](#):
>
> In your first example there’s a misunderstanding - an FStructProperty describes the position of a struct within a larger container

That explains a lot, much thanks!

One more quick question:

> [@Chatouille](#):
>
> (I recommend sticking with pointer rather than returning by value tho)

Why tho?  
I expect this struct to be pretty small almost all the time. Is there an overhead that I’m missing? or some type safety concern?

* * *

Either way, thanks for help. I’m closing the thread.

---

<div class="post-metadata">

**Author:** ![dzemzmcdonalda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dzemzmcdonalda/32/1214680_2.png) [@dzemzmcdonalda](https://forums.unrealengine.com/u/dzemzmcdonalda)\
**Post date:** [February 2, 2023, 9:40am UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/6 "2023-02-02T09:40:12Z")

</div>

Also this is a full function that uses the solution:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/f/3/cf3c5ac470897147c1ef2266639930242fd96283.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:** [February 2, 2023, 10:53am UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/7 "2023-02-02T10:53:38Z")

</div>

Hmm, I see what you are doing, but it is a bit odd.

The only reason I could think of, that you would be doing this sort of thing, is if StructType was a derived struct type that you don’t know at compile time, and returning it as a base type which you do know. But you are doing `check(StructType == T::StaticStruct())` which nullifies that statement. You DO know the struct type at compile time.

Maybe you have good reason to do so which eludes me, you don’t have to answer this question, but is there any reason you’re not simply doing this ?

```cpp
template<class T>
T GetDecodedValue() const
{
    T StructValue;
    SetStructFromBinary(StructType, &StructValue, *this);
    return StructValue;
}

```

---

<div class="post-metadata">

**Author:** ![dzemzmcdonalda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dzemzmcdonalda/32/1214680_2.png) [@dzemzmcdonalda](https://forums.unrealengine.com/u/dzemzmcdonalda)\
**Post date:** [February 2, 2023, 1:28pm UTC](https://forums.unrealengine.com/t/how-to-get-value-of-some-ustruct-while-only-having-access-to-its-uscriptstruct-and-memory-adress/761339/8 "2023-02-02T13:28:45Z")

</div>

> [@Chatouille](#):
>
> you don’t have to answer this question

I will 😃

However, I don’t want to spoil the whole idea behind it. At this point I’m just experimenting. I just need the most generic type that would be possible to store and replicate. I also need this to be blueprint friendly. This is why I’m using structs.  
Not sure if this will work as I want it though.

> [@Chatouille](#):
>
> The only reason I could think of, that you would be doing this sort of thing, is if StructType was a derived struct type that you don’t know at compile time

That’s right and false at the same time.  
`T GetDecodedValue() const` is public and will possible to use outside of class.  
I want the “gameplay coder” to define the type on his own by creating USTRUCT and by stroring it’s `::StaticStruct`, but the the back-end code won’t know anything about it.

> [@Chatouille](#):
>
> But you are doing `check(StructType == T::StaticStruct())` which nullifies that statement.

I’m just checking here for pure human error. e.g. If coder calls this function with `float` or `void` the code won’t compile. And if he calls it with wrong struct type, it will crash at runtime.

> [@Chatouille](#):
>
> but is there any reason you’re not simply doing this

Actually NO. You are right here.  
This is a leftover. I forgot I don’t have to use it anymore.  
BUT I’ll have to use a similiar setup somewhere else to handle pure blueprint logic.

* * *

Hope this answers your curiosity.  
Probably, there is indeed a beter way to do all this.
