- Unreal Engine 4.x Scripting with C++ Cookbook
- John P. Doran William Sherif Stephen Whittle
- 273字
- 2025-02-28 12:28:30
How to do it...
We'll create a structure called FColoredTexture in C++ code to contain a texture and a modulating color:
- From Visual Studio, right-click on the Games/Chapter_02/Source/Chapter_02 folder and select Add | New item.... From the menu, select a Header file (.h) and then name the file ColoredTexture.h (not FColoredTexture).
- Under Location, make sure that you select the same folder as the other script files in the project (in my case, C:\Users\admin\Documents\Unreal Projects\Chapter_02\Source\Chapter_02) that is not the default:

- Once created, use the following code in ColoredTexture.h:
#pragma once
#include "ObjectMacros.h"
#include "ColoredTexture.generated.h"
USTRUCT(Blueprintable)
struct CHAPTER_02_API FColoredTexture
{
GENERATED_USTRUCT_BODY()
public:
UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = HUD )
UTexture* Texture;
UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = HUD )
FLinearColor Color;
};
- Use ColoredTexture.h as a UPROPERTY() in a blueprintable UCLASS(), using a UPROPERTY() declaration such as this:
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "ColoredTexture.h"
#include "UserProfile.generated.h"
/**
* UCLASS macro options sets this C++ class to be
* Blueprintable within the UE4 Editor
*/
UCLASS(Blueprintable, BlueprintType)
class CHAPTER_02_API UUserProfile : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
float Armor;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
float HpMax;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
FString Name;
// Displays any UClasses deriving from UObject in a dropdown
// menu in Blueprints
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Unit)
TSubclassOf<UObject> UClassOfPlayer;
// Displays string names of UCLASSes that derive from
// the GameMode C++ base class
UPROPERTY(EditAnywhere, meta=(MetaClass="GameMode"), Category = Unit )
FStringClassReference UClassGameMode;
// Custom struct example
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
FColoredTexture Texture;
};
- Save your script and compile your changes. Upon entering your object blueprint, you should notice the new properties:
