protected: virtualvoidBeginPlay()override; voidSetHUDTime(); voidPollInit(); /** * Sync time between client and server */
// Requests the current server time, passing in the client's time when the request was sent UFUNCTION(Server, Reliable) voidServerRequestServerTime(float TimeOfClientRequest);
// Reports the current server time to the client in response to ServerRequestServerTime UFUNCTION(Client, Reliable) voidClientReportServerTime(float TimeOfClientRequest, float TimeServerReceivedClientRequest);
float ClientServerDelta = 0.f; // difference between client and server time
/// @brief 在客户端与服务器建立连接后,向服务器请求时间同步 voidABlasterPlayerController::ReceivedPlayer() { // Called after this PlayerController's viewport/net connection is associated with this player controller. Super::ReceivedPlayer(); if (IsLocalController()) { // 请求服务器时间时,传入客户端的当前的时间 ServerRequestServerTime(GetWorld()->GetTimeSeconds()); } }
/** Possible state of the current match, where a match is all the gameplay that happens on a single map */ namespace MatchState { extern ENGINE_API const FName EnteringMap; // We are entering this map, actors are not yet ticking extern ENGINE_API const FName WaitingToStart; // Actors are ticking, but the match has not yet started extern ENGINE_API const FName InProgress; // Normal gameplay is occurring. Specific games will have their own state machine inside this state extern ENGINE_API const FName WaitingPostMatch; // Match has ended so we aren't accepting new players, but actors are still ticking extern ENGINE_API const FName LeavingMap; // We are transitioning out of the map to another location extern ENGINE_API const FName Aborted; // Match has failed due to network issues or other problems, cannot continue
// If a game needs to add additional states, you may need to override HasMatchStarted and HasMatchEnded to deal with the new states // Do not add any states before WaitingToStart or after WaitingPostMatch }
AGameMode 提供很方便的 Get 和 Set MatchState 的接口,并且提供一个 OnMatchStateSet 的回调函数,用于处理切换到某个 State 时要实现的功能。
1 2 3 4 5 6 7 8 9
/** Returns the current match state, this is an accessor to protect the state machine flow */ UFUNCTION(BlueprintCallable, Category="Game") FName GetMatchState()const{ return MatchState; }
/** Updates the match state and calls the appropriate transition functions */ virtualvoidSetMatchState(FName NewState);
/** Overridable virtual function to dispatch the appropriate transition functions before GameState and Blueprints get SetMatchState calls. */ virtualvoidOnMatchStateSet();
ABlasterGameMode::ABlasterGameMode() { // Whether the game should immediately start when the first player logs in. // Affects the default behavior of ReadyToStartMatch bDelayedStart = true; }
if (MatchState == MatchState::WaitingToStart) { CountdownTime = WarmupTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; if (CountdownTime <= 0.f) { // Transition from WaitingToStart to InProgress. // You can call this manually, will also get called if ReadyToStartMatch returns true StartMatch(); } } }