# Constucting a struct variable in c++

**URL:** <https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644>\
**Category:** Blueprint\
**Tags:** Struct, construction, unreal-engine, UE4, CPP, Blueprint, question\
**Created:** [September 28, 2017, 10:19am UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644 "2017-09-28T10:19:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WeiLiang](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WeiLiang](https://forums.unrealengine.com/u/WeiLiang)\
**Post date:** [September 28, 2017, 10:19am UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/1 "2017-09-28T10:19:22Z")

</div>

Given a function such as one below

```
UFUNCTION(BlueprintCallable)
void Func( UStruct* Struct );

```

How can I construct a struct from the UStruct passed in as parameter? The struct will be passed in from Blueprint.

Edit :  
I am trying to construct a struct in the scope of the function as though I am creating a new object, for example, calling

```
NewObject<SomeObjectClass>( nullptr )

```

However, even though UStruct is derived from UObject, I cannot use NewObject() to construct a new struct based on the UStruct\* param.

---

<div class="post-metadata">

**Author:** ![Semitable](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Semitable](https://forums.unrealengine.com/u/Semitable)\
**Post date:** [September 28, 2017, 11:29am UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/2 "2017-09-28T11:29:30Z")

</div>

I am not sure what you are asking, but I’ll give it a try.  
Are you trying to define a new UStruct? Or you have one already?

If you are trying to define a ustruct then on on your .h file add a definition like so:

```
USTRUCT(BlueprintType)
struct FMyUStruct
{
    GENERATED_BODY()
    UPROPERTY()
    int32 MyInt;
}

```

then you will have to pass a variable like this to your function:

```
FMyStruct myStruct;
Func(myStruct);

```

but the definition must be as follows:

```
     UFUNCTION(BlueprintCallable)
     void Func( FMyStruct& Struct ); 
    // notice I am using a reference because 
    // pointers to structs are not supported in blueprint

```

Now, if the function is hardcoded and you already have the struct you should try to cast your pointer:

```
Cast<UStruct>(myStruct)

```

but I am not sure that would work. Also you can try a `reinterpret_cast()`

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [September 28, 2017, 2:15pm UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/3 "2017-09-28T14:15:29Z")

</div>

Take a look at `FStructOnScope`. This handles creating and initialising a struct instance from a `UStruct`.

If you need the struct instance to outlive your current scope then you’ll either have to put `FStructOnScope` into a smart pointer, or copy what it does internally and make it work for what you need.

---

<div class="post-metadata">

**Author:** ![WeiLiang](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WeiLiang](https://forums.unrealengine.com/u/WeiLiang)\
**Post date:** [September 28, 2017, 4:11pm UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/4 "2017-09-28T16:11:31Z")

</div>

I want to keep my function generic. Thanks for trying though 🙂

---

<div class="post-metadata">

**Author:** ![WeiLiang](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WeiLiang](https://forums.unrealengine.com/u/WeiLiang)\
**Post date:** [September 28, 2017, 4:29pm UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/5 "2017-09-28T16:29:58Z")

</div>

Thanks for the prompt reply! Looking at the documentation of FStructOnScope, I do not understand how to access the properties in the struct instance. Is it right of me to assume hat SampleStructMemory is the struct instance? If so, how can I access the properties in the struct instance?

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [September 28, 2017, 4:36pm UTC](https://forums.unrealengine.com/t/constucting-a-struct-variable-in-c/405644/6 "2017-09-28T16:36:27Z")

</div>

Correct.

The `UStruct` defines the properties on the struct, and you can use that information to modify the correct memory within the struct instance. `FStructOnScope::GetStruct()` can get you the `UStruct` it was created with, and `FStructOnScope::GetStructMemory()` will get you the instance data.

You’re dealing with completely type-erased data, so you’re reliant on the properties to let you interpret it correctly. See `UProperty::ContainerPtrToValuePtr`, along with the various forms of `ExportText` and `ImportText` that are on `UProperty`. Property sub-classes also often have extra functions to let you more easily deal with the type of data that the property represents.

You can use `TFieldIterator<UProperty>` to iterate all the properties on a struct.
