【问题标题】:UE4 How to load level in commandlet?UE4 如何在命令行开关中加载关卡?
【发布时间】:2022-11-10 15:44:18
【问题描述】:

我想用命令行开关记录所有参与者,所以我写了这样的代码。

#include "MyCommandlet.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/StaticMeshActor.h"
#include "MyCharacter.h"

int32 UMyCommandlet::Main(const FString& Params)
{
    TArray<AActor*> arrActors;
    UWorld* World = GetWorld();

    if (World)
    {
        UGameplayStatics::GetAllActorsOfClass(GetWorld(), AMyCharacter::StaticClass(), arrActors);

        for (int i = 0; i < arrActors.Num(); i++)
        {
            AMyCharacter* pCharacter = dynamic_cast<AMyCharacter*>(arrActors[i]);

            UE_LOG(LogTemp, Display, TEXT("%d"), pCharacter->TestValue);

        }   
    }

    return 0;
}

但是 GetWorld() 返回 NULL。我认为原因是当调用命令行开关时,没有加载任何级别。

实际上虚幻引擎文件说 “Commandlet 在“原始”环境中执行,其中不加载游戏,不加载客户端代码,不加载关卡,也不存在任何参与者。” 链接:https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/Commandlets/UCommandlet/#:~:text=UCommandlet%20%3A%20public%20UObject-,Remarks,-Commandlet%20

那么如何在命令行开关中加载 level 或 getworld() ......?

【问题讨论】:

    标签: c++ unreal-engine4 unreal-engine5


    【解决方案1】:

    尝试这个:

    FString levelAssetPath = TEXT("/Game/Maps/SomeLevel");
    GEditor->GetEditorSubsystem<ULevelEditorSubsystem>()->LoadLevel(levelAssetPath);
    

    【讨论】:

      【解决方案2】:

      也许你可以创建一个 FAutoConsoleCommandWithWorldAndArgs 来绑定你的函数,它会传入一个 UWorld。

      FAutoConsoleCommandWithWorldAndArgs AbilitySystemToggleDebugHUDCommand(
      TEXT("Your Command"),
      TEXT("Your Help Text"),
      FConsoleCommandWithWorldAndArgsDelegate::CreateStatic(Your_Function)
      );
      
      static void Your_Function(const TArray<FString>& Args, UWorld* InWorld);
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      这个世界在命令行开关中不应为 NULL:

      UWorld* World = GEditor-&gt;GetEditorWorldContext().World();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-03
        • 1970-01-01
        • 2021-10-20
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-02
        相关资源
        最近更新 更多