ue4-官方文档阅读笔记

看官网文档的一点点记录


TSubclassOf

In addition to this UPROPERTY safety, you get type safety at the C++ level too. If you try to assign incompatible TSubclassOf types to each other you’ll get a compilation error. In the case you are trying to assign a generic UClass, it will perform a runtime check to verify that it can do the assignment. If the runtime check fails, the resulting value is nullptr.

1
2
3
4
5
UClass* ClassA = UDamageType::StaticClass();
TSubclassOf<UDamageType> ClassB;
ClassB = ClassA; // Performs a runtime check
TSubclassOf<UDamageType_Lava> ClassC
ClassB = ClassC; // Performs a compile time check

Delegates

1. 单个代理

  • 官网地址:https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Delegates/index.html

  • 使用方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    //declare
    DECLARE_DELEGATE_OneParam(FStringDlg, const FString&);

    FStringDlg mWriteLogDlg;
    void WriteLog(const FString& str);
    void MyTest::WriteLog(const FString& str)
    {
    UE_LOG(LogMyTest, Warning, TEXT("--- log:%s"), *str);
    }

    //bind
    mWriteLogDlg.BindUObject(this, &MyTest::WriteLog);

    //execute
    FString str = FString::Printf(TEXT("hello world-%d"), 123);
    mWriteLogDlg.Execute(str)

2. 多个代理

  • 官网地址:
    https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Delegates/Multicast/index.html#bindingmulti-castdelegates

  • 使用方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    //declare
    DECLARE_MULTICAST_DELEGATE_OneParam(FStringMultiDlg,const FString&);

    FStringMultiDlg mWriteLogMultiDlg;
    void WriteLog1(const FString& str);
    void WriteLog2(const FString& str);
    void MyTest::WriteLog1(const FString& str)
    {
    UE_LOG(LogMyTest, Warning, TEXT("--- log1:%s"), *str);
    }
    void MyTest::WriteLog2(const FString& str)
    {
    UE_LOG(LogMyTest, Warning, TEXT("--- log2:%s"), *str);
    }

    //bind
    mWriteLogMultiDlg.AddUObject(this, &MyTest::WriteLog1);
    mWriteLogMultiDlg.AddUObject(this, &MyTest::WriteLog2);

    //execute
    FString str = FString::Printf(TEXT("hello world-%d"), 123);
    mWriteLogMultiDlg.Broadcast(str)

Assertions


Components


Blueprint Function Libraries

Creating a Blueprint Function Library is very similar to exposing functions to Blueprints using the UFUNCTION() macro. Instead of deriving from an Actor or directly from UObject all Blueprint Libraries inherit from UBlueprintFunctionLibrary. They should also contain only static methods. The code below is a snippet from the Analytics Blueprint Library, showing how to setup your library class.
全局(蓝图和c++)可以调用,一般用来做util或获取全局所有单例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
UCLASS()
class UGolbalFunc : public UBlueprintFunctionLibrary
{
GENERATED_BODY()

public:
UGolbalFunc();
virtual ~UGolbalFunc();
virtual void BeginDestroy() override;

UFUNCTION(BlueprintCallable, Category = "GolbalFunc")
static UResMgr* GetResMgr() { return GetMgr<UResMgr>(); }

UFUNCTION(BlueprintCallable, Category = "GolbalFunc")
static UEffectMgr* GetEffectMgr() { return GetMgr<UEffectMgr>(); }


String Handling