keywords: UE4, Gameplay Development Notes

Switch Level

论坛上有不少资料说切换场景时使用GEngine->Browse():

if (FWorldContext* Context = GEngine->GetWorldContextFromWorld(GetWorld()))
{
    FString Error;
    FURL URL(TEXT("/Game/Demo/scecn_test/scecn_test02"));
    EBrowseReturnVal::Type RS = GEngine->Browse(*Context, URL, Error);
    if (EBrowseReturnVal::Type::Failure == RS)
    {
        WSI->ChangePlayerState(EPlayerState::EP_LoginFailed);
    }
}

GEngine->Browse这种方式第一次打开场景时正常,如果第二次切换场景(比如从别的场景再切回原场景),则会导致程序崩溃。建议用:UGameplayStatics::OpenLevel(GetWorld(), TEXT("/Game/Map/TestMap")); 这种方式来回切场景不会出现崩溃问题。

Check if it’s server or client without WorldContext

Quoted from CoreMisc.h

bool IsRunningDedicatedServer();

bool IsRunningGame();

boo IsRunningClientOnly();
Subsystem (Unreal Stylized Singleton)

Quoted from GameInstance.h:

template <typename TSubsystemClass>
TSubsystemClass* GetSubsystem() const
{
    return SubsystemCollection.GetSubsystem<TSubsystemClass>(TSubsystemClass::StaticClass());
}

How to disable initialization of GameInstanceSubsystem on dedicated server?
Quoted from:
Engine\Plugins\Experimental\CommonUI\Source\CommonUI\Private\CommonUISubsystemBase.cpp

bool UCommonUISubsystemBase::ShouldCreateSubsystem(UObject* Outer) const
{
    return !CastChecked<UGameInstance>(Outer)->IsDedicatedServerInstance();
}

几时归去,作个闲人。对一张琴、一壶酒、一溪云。----宋·苏轼《行香子》