Structures in C++

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

Did you checkout: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums ?

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

Greetings DarthB

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()”

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

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 ?

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.

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

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!

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

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

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

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!