官术网_书友最值得收藏!

Creating a USTRUCT

You may want to construct a Blueprints editable property in UE4 that contains multiple members. The FColoredTexture struct that we will create in this chapter will allow you to group together a texture and its color inside the same structure for inclusion and specification in any other UObject derivative, Blueprintable class:

The FColoredTexture structure does have the visual within Blueprints appearance as shown in the figure above.

This is for good organization and convenience of your other UCLASS UPROPERTIES(). You may want to construct a C++ structure in your game using the keyword struct.

Getting ready

A UObject is the base class of all UE4 class objects, while an FStruct is just any plain old C++ style struct. All objects that use the automatic memory management features within the engine must derive from this class.

Tip

If you' recall from the C++ language, the only difference between a C++ class and a C++ struct is that C++ classes have default private members, while structs default to public members. In languages like C#, this isn't the case. In C#, a struct is value-typed, while a class is reference-typed.

How to do it...

We'll create a structure FColoredTexture in C++ code to contain a texture and a modulating color:

  1. Create a file ColoredTexture.h in your project folder (not FColoredTexture).
  2. ColoredTexture.h contains the following code:
    #pragma once
    
    #include "Chapter2.h"
    #include "ColoredTexture.generated.h"
    
    USTRUCT()
    struct CHAPTER2_API FColoredTexture
    {
      GENERATED_USTRUCT_BODY()
      public:
      UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = HUD )
      UTexture* Texture;
      UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = HUD )
      FLinearColor Color;
    };
  3. Use ColoredTexture.h as a UPROPERTY() in some Blueprintable UCLASS(), using a UPROPERTY() declaration like this:
    UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = HUD )
    FColoredTexture* Texture;

How it works...

The UPROPERTY() specified for the FColoredTexture will show up in the editor as editable fields when included as UPROPERTY() fields inside another class, as shown in step 3.

There's more…

The main reason for making a struct a USTRUCT() instead of just a plain old C++ struct is to interface with the UE4 Engine functionality. You can use plain C++ code (without creating USTRUCT() objects) for quick small structures that don't ask the engine to use them directly.

主站蜘蛛池模板: 元江| 金堂县| 灌南县| 含山县| 三门峡市| 建水县| 堆龙德庆县| 鸡泽县| 朝阳区| 太原市| 冕宁县| 扎赉特旗| 巩留县| 南安市| 吐鲁番市| 黎川县| 大宁县| 青河县| 历史| 南城县| 和林格尔县| 错那县| 霍州市| 交城县| 桦南县| 英德市| 白玉县| 工布江达县| 新昌县| 泰和县| 绍兴市| 韶山市| 大连市| 开原市| 商洛市| 阜南县| 弥勒县| 新余市| 买车| 岳西县| 温泉县|