# Structures in C++

**URL:** <https://forums.unrealengine.com/t/structures-in-c/305853>\
**Category:** Blueprint\
**Tags:** data-structures, Structure, unreal-engine, UE4, Blueprint, question, CPP\
**Created:** [February 3, 2015, 2:39pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853 "2015-02-03T14:39:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![3HMonkey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@3HMonkey](https://forums.unrealengine.com/u/3HMonkey)\
**Post date:** [February 3, 2015, 2:39pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/1 "2015-02-03T14:39:34Z")

</div>

Hello guys, I have a question…

How I will be able to create a struct like a blueprint structure in c++ ? I cannot find a working documentation about this topic. I am using 4.7 preview 4.  
Are there also any non outdated tutorials in using c++ inside UE4 ?

Ty very much

---

<div class="post-metadata">

**Author:** ![DarthB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DarthB](https://forums.unrealengine.com/u/DarthB)\
**Post date:** [February 3, 2015, 2:49pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/2 "2015-02-03T14:49:09Z")

</div>

Did you checkout: [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/Structs,_USTRUCTS(),_They%27re_Awesome) ?

I cannot imagine the folks at epic did changes that break the code snippets shown in the wiki entry.

Greetings DarthB

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@3HMonkey](https://forums.unrealengine.com/u/3HMonkey)\
**Post date:** [February 3, 2015, 3:04pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/3 "2015-02-03T15:04:31Z")

</div>

Hey and thank you for your reply…

A BP structure is kind of “UserDefinedStruct” which has the classbody:

```
UCLASS()
class INVENTORY_API Uteststruct : public UUserDefinedStruct
{
	
	GENERATED_BODY()
	
};

```

and not something like

```
USTRUCT([Specifier, Specifier, ...])
struct StructName
{
    GENERATED_USTRUCT_BODY()
};

```

which is strange to me.

when I change the generate line to something like this:

```
GENERATED_USTRUCT_BODY()

```

is there an error because he is not knowing “GENERATED\_USTRUCT\_BODY()”

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [February 3, 2015, 3:37pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/4 "2015-02-03T15:37:08Z")

</div>

Hi!

This a simple struct, it is usable in C++ and in BluePrint and it also has default values suing the CPP macro. The following would be the content of a header called MyTypes.h which you would add to your PreCompiledHeader file.

```
#pragma once

#include "MyTypes.generated.h"

USTRUCT(BlueprintType)
struct YOURGAME_API FSimpleRange
{
        GENERATED_USTRUCT_BODY()

        /** Min range value */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Range)
        float MinValue;

        /** Max range value */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Range)
        float MaxValue;

#if CPP
        FSimpleRange() :
                MinValue(0.f), MaxValue(0.f)
        {}
#endif

        /** Clamp a range */
        float ClampRange(const float Value)
        {
                return FMath::Clamp(Value, MinValue, FMath::Max(MinValue, MaxValue));
        }
};

```

So what is the PCH stuff all about. Where I would suggest to put all the code for the struct into would be in your main game header, the one that is created automatically and which you will add in most of your cpp files. Of course this greatly depends on the usage of the struct.

If you want to add it into a real PCH file just make sure you have a header file in your Private directory that uses PCH as it’s suffix (Source/Private/MyProjectPCH.h), it will then have to #include the header file where the struct is defined in. One thing to note would be that you are only allowed to include a PCH file into a compilation unit (cpp files).

Cheers,  
Moss

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@3HMonkey](https://forums.unrealengine.com/u/3HMonkey)\
**Post date:** [February 3, 2015, 5:59pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/5 "2015-02-03T17:59:06Z")

</div>

So is it created out of the studio by using the “Add Code to Project” Button or need I create a fresh class without a parent class inside VS ? What class I generally need to chose ?

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [February 3, 2015, 6:27pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/6 "2015-02-03T18:27:51Z")

</div>

A struct is not a class so what I usually do is to create a simple .h file that has that struct in it and I will add the .h to my PreCompiledHeader. I have edited the sample to reflect a full header file.

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@3HMonkey](https://forums.unrealengine.com/u/3HMonkey)\
**Post date:** [February 3, 2015, 6:36pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/7 "2015-02-03T18:36:41Z")

</div>

Thank you really much. I am a Java,C#, PHP, HTML programmer which is training C++ with UE4…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_15dd2896](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_15dd2896](https://forums.unrealengine.com/u/anonymous_user_15dd2896)\
**Post date:** [April 17, 2015, 7:37pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/8 "2015-04-17T19:37:45Z")

</div>

Hey, Moss, what “PreCompiledHeader” file are you referring to? That’s the part that Rama’s post (which is otherwise great) doesn’t talk about. I get the syntax of structs, I just don’t see any complete explanation of how to add them to your project. Thanks!

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [April 20, 2015, 10:05am UTC](https://forums.unrealengine.com/t/structures-in-c/305853/9 "2015-04-20T10:05:21Z")

</div>

I’ll edit the answer with an example how to add the struct to the PCH file this afternoon 😃

---

<div class="post-metadata">

**Author:** ![a\_najafi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/a_najafi/32/579584_2.png) [@a\_najafi](https://forums.unrealengine.com/u/a_najafi)\
**Post date:** [May 1, 2015, 6:01pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/10 "2015-05-01T18:01:27Z")

</div>

hey there Moss I just wanted to thank you because this answer just thought me alot but i don’t know what PreCompiledHeader file you are referring to

and top of that i would actually like to know more about these kind of things regarding the level in which the code is being processed in. like i would really like to be able to create codes i can run inside the editor (not at runtime) being able to access default values and properties of an actor without spawning it etc.

thanks alot again

---

<div class="post-metadata">

**Author:** ![a\_najafi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/a_najafi/32/579584_2.png) [@a\_najafi](https://forums.unrealengine.com/u/a_najafi)\
**Post date:** [May 2, 2015, 10:31am UTC](https://forums.unrealengine.com/t/structures-in-c/305853/11 "2015-05-02T10:31:39Z")

</div>

can you please give the example on how to add to the procompiled header file  
thank you

---

<div class="post-metadata">

**Author:** ![Jibberish](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibberish](https://forums.unrealengine.com/u/Jibberish)\
**Post date:** [July 25, 2015, 8:51pm UTC](https://forums.unrealengine.com/t/structures-in-c/305853/12 "2015-07-25T20:51:04Z")

</div>

I’m really trying to get my head around the use of USTRUCT right now, and this is the first time I’ve ever come across an example with the section **#if CPP** and **#endif** inside the USTRUCT definition. Could someone please elaborate on what these preprocessor directives are accomplishing in this example? Thanks!
