批次執行Console commands

 有的時候我們會想要執行一連串的console command,才能得到結果,可是又不想要為這一串console command專門寫一個console command把他們集合起來。怎麼辦?


作法(只需要做一次)

把功能加在CheatManager

在你繼承的CheatManager.h加上函式

UFUNCTION(exec)
void ExecComboCommand(const FString& ComboCommandName);


在你繼承的CheatManager.cpp實作函式

void UMyCheatManager::ExecComboCommand(const FString& ComboCommandName)
{
    FConfigSection* CommandsToRun = GConfig->GetSectionPrivate(*ComboCommandName, false, true, GGameIni);
    if (!CommandsToRun)
    {
        return;
    }
    if (!IsValid(GetOuterAPlayerController()))
    {
        return;
    }
    for (FConfigSectionMap::TIterator It(*CommandsToRun); It; ++It)
    {
        GetOuterAPlayerController()->ConsoleCommand(*It.Value().GetValue());
    }
}


使用組合指令


在ini裡面製作要批次執行的指令

例如在

[Project]\Saved\Config\Windows\Game.ini

新增

[Debug.ComboCommand1]

+Cmds=stat fps

+Cmds=stat unit

(新增的時候請先關閉Editor,以免修改被Editor蓋掉)


如果要上平台也能測試,可能就要新增在

[Project]\Config\DefaultGame.ini

內不過記得最後要拔掉。


然後打開你的Editor或是執行目標平台的遊戲,先輸入

EnableCheats

再輸入

ExecComboCommand Debug.ComboCommand1

就成功了


優點

不用再記各個詳細的指令怎麼打,只要記別名就好

有很多的command不用一直重複輸入

不用怕指令忘記,因為ini會記得(除非刪掉)

靠字串組合,所以程式碼只要寫一次(一個函式),就能支援多種指令ComboCommand1,ComboCommand2, ...


缺點

還是要執行EnableCheats

因為靠CheatManager,所以應該只能在Game中執行,編輯模式不確定是否有效。

為了開發方便可能會想要記在[Project]\Config\DefaultGame.ini,最後要記得拔掉。


留言

這個網誌中的熱門文章

UE4 除錯技巧分享 (一)

UE4 GameplayAbilityTask介紹

UE4 Navigation Mesh 心得