Download

How do i make a struct and access it via blueprint ?

Consider i’m starting from the “File:add code to project” step.
Can someone guide me through the steps? Thanks.

1 Like

If using the Add Code To Project wizard, you would choose a plain c++ class as the type, name it whatever you want it to be named.

Once visual studio has finished loading your new files, delete the definition that was auto-created (i.e. the class, the constructor and destructor, etc) in both the header (.h file) and the source (.cpp file).

Now redefine your struct within the header as follows: (using the name you wish)




#include "MyStruct.generated.h"

USTRUCT(BlueprintType)
struct FMyStruct
{
    FMyStruct()        // this is your default constructor which is required for a USTRUCT definition
    {
        // initialize things here if desired or required
    }

    UPROPERTY(BlueprintReadWrite)
    int32 SomeNumber;
}


This creates a struct names FMyStruct with a single int32 property that is accessible via blueprints.

The #include “MyStruct.generated.h” is required so that the auto-generated stuff from the USTRUCT, UPROPERTY, etc macros gets included for the compiler. The “MyStruct” part would be the name of the file, not just the name of the class/struct, since the type doesnt have to have the same name as the file.
The F appended to the start of the struct’s name is just a naming convention used within UE4. An appended F generally denotes a type that does not derive from UObject or AActor.
The USTRUCT(…) macro above the struct adds support for the Unreal Engine 4 reflection system as well as many other goodies including replication support, a bunch of default operator defines, etc.
The BlueprintType specifier within the USTRUCT macro declares this as a type that can be accessed within blueprints.
The UPROPERTY(…) macro above the SomeNumber property declares SomeNumber as a reflected property (to be included and accessible to/from UBT-generated metadata.
The BlueprintReadWrite specifier within the UPROPERTY macro allows the property to be both read and written to within blueprints.

There are several other specifiers that can greatly change the accessibility that are well documented within the UE4 docs. The above is just a quick and simple example.

Also keep in mind that not all types are able to be exposed to blueprints directly.

1 Like

Thanks.

It says it requires “GENERATED_USTRUCT_BODY()” at the beginning, so i add it, and then it tells me:

Error 1 error code: OtherCompilationError (2) C:\OldSchoolNightmare\Intermediate\ProjectFiles\Error OldSchoolNightmare

Error 2 error MSB3073: The command ““D:\Program Files\Epic Games\4.7\Engine\Build\BatchFiles\Build.bat” OldSchoolNightmareEditor Win64 Development “C:\OldSchoolNightmare\OldSchoolNightmare.uproject” -rocket” exited with code -1. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets 38 5 OldSchoolNightmare

My head looks now like this:


#pragma once

USTRUCT(BlueprintType)
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()

	FMyStruct()        // this is your default constructor which is required for a USTRUCT definition
	{
		// initialize things here if desired or required
	}

	UPROPERTY(BlueprintReadWrite)
		int32 SomeNumber;
};

and my cpp file like this:


// Fill out your copyright notice in the Description page of Project Settings.

#include "OldSchoolNightmare.h"
#include "MyStructs.h"

Ah yeah sorry that was my fault. It’s been a long night lol.

Yes you need to add the GENERATED_USTRUCT_BODY() macro like you said and then in the source file you need to have first the shared precompiled header file of your game/module #include and then the header of the type #include.



#include "MyStruct.generated.h"

USTRUCT(BlueprintType)
struct FMyStruct
{
    GENERATED_USTRUCT_BODY()

    FMyStruct()        // this is your default constructor which is required for a USTRUCT definition
    {
        // initialize things here if desired or required
    }

    UPROPERTY(BlueprintReadWrite)
    int32 SomeNumber;
}


and the cpp



#include "MyGame.h"
#include "MyStruct.h"


1 Like

Cool now it works! Thanks! I’m sure a lot of new c++ users will find this thread useful :slight_smile: