函式宣告 UE4中有兩種函數, 一種是傳統的C++函數, 一種是UFunction. UFunction通過加入一些描述符來支援一些特殊功能
UFUNCTION([specifier, specifier, ...], [meta(key=value, key=value, ...)])ReturnType FunctionName([Parameter, Parameter, ...])
主要有幾種應用: 在Blueprint中調用 做為delegate, 如與按鍵訊息進行綁定 網路回調和資料同步 做為命令列的指令執行, 同UE3的exec關鍵字
函數說明符 Blueprint相關 BlueprintAuthorithOnly: 沒有網格授權的話不會在blueprint中執行 BlueprintCallable: 可以在blueprint中執行 BlueprintCosmetic: 表現函數, 不會在伺服器執行 BlueprintImplementableEvent: 可以在blueprint中重寫 BlueprintNativeEvent: 設計為被blueprint重寫, 但是仍然包含native的實現 BlueprintPure: 不會影響自身對象 Network相關 Client: 只在用戶端執行 Server: 只在服務端執行 NetMulticast: 會在伺服器和所有用戶端執行 Reliable: 函數執行通過網路進行複製, 並且是可靠的 Unreliable: 函數在複製執行時可能失敗 Editor相關 Category 其它 CustomThunk: 提供自訂的execFoo, 而不是產生的 Exec: 可以在命令列中被執行
中繼資料說明符 BlueprintInternalUseOnly BlueprintProtected: 只在blueprint中調用 DeprecatedFunction DeprecationMessage UnsafeDuringActorConstruction
函數參數說明符 Out: 傳引用 Optional: 可選, 取預設值
委託(Delegate)
DECLARE_DELEGATE_OneParam( FStringDelegate, FString );
FSharedRef< FLogWriter > LogWriter( new FLogWriter() );WriteToLogDelegate.BindSP( LogWriter, &FLogWriter::WriteToLog );
WriteToLogDelegate.Execute( TEXT( "Delegates are spiffy!" ) );
建議Delegate傳遞時使用引用, 避免傳值引起記憶體配置 multi-cast與event的區別只有聲明event的類本身可以調用event的Broadcast, IsBound, Clear函數 dynamic delegate是用函數名進行綁定, 所以可以進行序列化, 但速度相對較慢
定時器(Timer)
GetWorldTimerManager().SetTimer(this, &AMatineeActor::CheckPriorityRefresh, 1.0f, true);
GetWorldTimerManager().ClearTimer(this, &AMatineeActor::CheckPriorityRefresh);
GetWorldTimerManager().IsTimerActive(this, &APlayerController::DelayedPrepareMapChange)
Timer不是安全執行緒的, 只能在遊戲線程使用