ue4-todo_list_network

记录一些有待 todo 的事项


Network 相关



启动exe加参数

  • 直接创建一个快捷方式,后面添加启动参数 如 -log

常见错误

  • DefaultEngine.ini 没有加

    1
    2
    [OnlineSubsystem]
    DefaultPlatformService=Null
  • MyNet.Build.cs 没有加

    1
    2
    3
    4
    PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay",             "OnlineSubsystem",
    "OnlineSubsystemUtils" });

    DynamicallyLoadedModuleNames.Add("OnlineSubsystemNull");
  • Run Dedicated Server

    • Not Checked:则可以 Create Session
    • Checked:则不可以 Create Session,原因:NULL: Cannot create session ‘Game’: session already exists.
  • 要继承GameModeBase 而不是 GameMode


api测试

  • UGameplayStatics::GetIntOption
    1
    const int32 BotsCountOptionValue = UGameplayStatics::GetIntOption(Options, GetBotsCountOptionName(), 0);

遍历 world 中所有 APlayerStart 的派生类示例对象。(遍历指场景中的实例对象 是指 拖入 or 动态实例化出来的对象

1
2
3
4
for (TActorIterator<APlayerStart> It(GetWorld()); It; ++It)
{
APlayerStart* TestSpawn = *It;
}

reliable 和 BlueprintNativeEvent 类似都可以 _implementation??

1
2
3
4
5
6
7
8
	/** Starts the online game using the session name in the PlayerState */
UFUNCTION(reliable, client)
void ClientStartOnlineGame();

/** Starts the online game using the session name in the PlayerState */
void AShooterPlayerController::ClientStartOnlineGame_Implementation()
{
}

shared ptr 使用

1
2
3
4
class FShooterIngameMenu : public TSharedFromThis<FShooterIngameMenu>
{...}

TSharedPtr<class FShooterIngameMenu> ShooterIngameMenu = MakeShareable(new FShooterIngameMenu());

虚幻内部socket导入

引擎delegate FGameDelegates

  • 参照 ShooterGameDelegates.h

网络事件触发

  • 参照 ShooterPlayerController.cpp 中的 ClientGameStarted_Implementation()

bool UShooterGameInstance::LoadFrontEndMap(const FString& MapName)

1
2
3
4
5
6
7
8
9
10
11
if (URL.Valid && !HasAnyFlags(RF_ClassDefaultObject)) //CastChecked<UEngine>() will fail if using Default__ShooterGameInstance, so make sure that we're not default
{
BrowseRet = GetEngine()->Browse(*WorldContext, URL, Error);

// Handle failure.
if (BrowseRet != EBrowseReturnVal::Success)
{
UE_LOG(LogLoad, Fatal, TEXT("%s"), *FString::Printf(TEXT("Failed to enter %s: %s. Please check the log for errors."), *MapName, *Error));
bSuccess = false;
}
}

online and session相关

UNetConnection 和 LocalPlayer 详解

Game Session

复制level

  • 随便新建一新level,设置编辑的level为新level,再把就的level复制一下即可

设置输入

  • 在 playercontroller 中
    这里写图片描述

GameplayDebugger Tool

SaveGame

从 PlayerState 索引到 AplayerController,再索引到 Acharacter

  • 因为在 Multiplayer 中,PlayerState 中保存着客户端的玩家的信息

  • 蓝图中获取
    这里写图片描述

  • PlayerState.cpp 的源码中可以看到这样获取 APlayerController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //step 1
    AController* OwningController = Cast<AController>(GetOwner());
    if (OwningController != NULL)
    {
    bIsABot = (Cast<APlayerController>(OwningController) == nullptr);
    }

    //step 2
    FORCEINLINE APawn* GetPawn() const { return Pawn; }

    在 Cast<ACharacter> 到你的派生类

无缝切换 seamless travel

  • https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Travelling/

  • 启用 seamless travel:

    1. 在配置文件 ./Config/DefaultEngine.ini
      1
      2
      [/Script/EngineSettings.GameMapsSettings]
      TransitionMap=/Game/ThirdPersonBP/Maps/LoadingMap
    • 对应源码中的 UGameMapsSettings::TransitionMap,默认为空
    1. 设置 AGameModeBase::bUseSeamlessTravel 为 true
    • 蓝图中直接勾选
      这里写图片描述
    • c++ 中 在构造函数中 bUseSeamlessTravel = true
  • 在PIE模式下不能启用 seamless travel,引擎警告:LogGameMode:Warning: AGameModeBase::CanServerTravel: Seamless travel currently NOT supported in single process PIE.