《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
整體模塊module分析:
打開StrategyGame.uproject來看:
{"FileVersion": 3,"Version": 0,"VersionName": "0.0","EngineVersion": "0.0.0-0","PackageFileUE4Version": 226,"PackageFileLicenseeUE4Version": 0,"EngineAssociation": "","FriendlyName": "","Description": "","Category": "","CreatedBy": "","CreatedByURL": "","Modules": [{"Name": "StrategyGame","Type": "Runtime","LoadingPhase": "Default"},{"Name": "StrategyGameLoadingScreen", //這個是額外增加的module"Type": "Runtime","LoadingPhase": "PreLoadingScreen" //從代碼中找到解釋, 這個module必須設置成這種類型, 否則不會 “hook in time”, 從字面意思來看,是預加載一直存在于內存中的; }],"EpicSampleNameHash": "0","TargetPlatforms": ["Android","IOS","MacNoEditor","WindowsNoEditor"] }可以看到這里面定義了兩個模塊, 名字,類型,還有一個”LoadingPhase”的屬性;
這個文件里面的內容是自己定義的, 還是自動生成的 ?
CONTINUE ... ...
?
這個demo從整體模塊來講可以看成有兩個, 一個模塊這里指的是生成dll的個數, 除了主模塊StrategyGame之外,還有一個StrategyGameLoadingScreen:
- StrategyGameLoadingScreen: 作為子模塊, 會生成一個dll, UE4Editor-StrategyGameLoadingScreen.dll(只是在editor里面編譯運行過游戲); 具體實現的時候添加的內容有:
文件StrategyGameLoadingScreen.h/cpp
FStrategyGameLoadingScreenModule : IStrategyGameLoadingScreenModule : public IModuleInterface
利用這樣的繼承關系實現一個新的module;
其具有自己的build文件: StrategyGameLoadingScreen.Build.cs文件:
using UnrealBuildTool;// This module must be loaded "PreLoadingScreen" in the .uproject file, otherwise it will not hook in time!public class StrategyGameLoadingScreen : ModuleRules {public StrategyGameLoadingScreen(TargetInfo Target){PrivateIncludePaths.Add("../../StrategyGame/Source/StrategyGameLoadingScreen/Private");PublicDependencyModuleNames.AddRange(new string[] {"Core","CoreUObject","Engine"});PrivateDependencyModuleNames.AddRange(new string[] {"MoviePlayer","Slate","SlateCore","InputCore"});} }? ??可以看到其內部定義了該模塊的額外include path, 以及使用那些引擎的原生模塊; 認為, 可能是: public為屬性的模塊可以和其他模塊進行接口之間調用的交互,而private的只能是自己當前模塊使用的(參考RulesCompiler.cs);
??? 這個模塊的主要功能是實現開始菜單的部分,并且一些UI元素如背景圖等是代碼寫出來的(class SStrategyLoadingScreen 的Construct 函數 ), 在此發現SNew和SAssignNew是UI 進行new專用的;
? ? 但是對于這個demo的UI而言,這個module里面的內容還不夠, 猜測主模塊StrategyGame里面對于UI的處理是結合這個模塊的;
- 主模塊StrategyGame:
這個模塊對應了一個UE4Editor-StrategyGame.dll; 其內部使用了上面那個StrategyGameLoadingScreen模塊:
?
在StrategyGame.Build.cs文件里面除了增加其他module,還有:
?
?
PrivateDependencyModuleNames.AddRange(new string[] {"StrategyGameLoadingScreen"});- 主模塊StrategyGame和StrategyGameLoadingScreen模塊的交互:
?這個demo中,主要是主模塊調用子模塊函數然后讓其顯示菜單背景圖等, 需要包含子模塊的頭文件, 顯式加載子模塊, 調用子模塊的具體實現:
void AStrategyMenuHUD::ShowLoadingScreen() {IStrategyGameLoadingScreenModule* LoadingScreenModule = FModuleManager::LoadModulePtr<IStrategyGameLoadingScreenModule>("StrategyGameLoadingScreen");if( LoadingScreenModule != nullptr ){LoadingScreenModule->StartInGameLoadingScreen();} }這個StrategyGameLoadingScreen模塊作為具有PreLoadingScreen屬性的模塊,在游戲啟動時候會專門先加載(LaunchEngineLoop.cpp, EnginePreInit(…)):
// Load up all modules that need to hook into the loading screenif (!IProjectManager::Get().LoadModulesForProject(ELoadingPhase::PreLoadingScreen) || !IPluginManager::Get().LoadModulesForEnabledPlugins(ELoadingPhase::PreLoadingScreen)){return 1; }關于模塊加載函數LoadModulesForProject其參數是:
enum Type{/** Loaded before the engine is fully initialized, immediately after the config system has been initialized. Necessary only for very low-level hooks */PostConfigInit,/** Loaded before the engine is fully initialized for modules that need to hook into the loading screen before it triggers */PreLoadingScreen,/** Right before the default phase */PreDefault,/** Loaded at the default loading point during startup (during engine init, after game modules are loaded.) */Default,/** Right after the default phase */PostDefault,/** After the engine has been initialized */PostEngineInit,/** Do not automatically load this module */None,// NOTE: If you add a new value, make sure to update the ToString() method below! Max };所以可以知道,不同的module屬性會在加載的時候具有不同的時機;
看后面可以發現會逐一加載后面幾個屬性的module(LaunchEngineLoop.cpp):
bool FEngineLoop::LoadStartupModules() {FScopedSlowTask SlowTask(3);SlowTask.EnterProgressFrame(1);// Load any modules that want to be loaded before default modules are loaded up.if (!IProjectManager::Get().LoadModulesForProject(ELoadingPhase::PreDefault) || !IPluginManager::Get().LoadModulesForEnabledPlugins(ELoadingPhase::PreDefault)){return false;}SlowTask.EnterProgressFrame(1);// Load modules that are configured to load in the default phaseif (!IProjectManager::Get().LoadModulesForProject(ELoadingPhase::Default) || !IPluginManager::Get().LoadModulesForEnabledPlugins(ELoadingPhase::Default)){return false;}SlowTask.EnterProgressFrame(1);// Load any modules that want to be loaded after default modules are loaded up.if (!IProjectManager::Get().LoadModulesForProject(ELoadingPhase::PostDefault) || !IPluginManager::Get().LoadModulesForEnabledPlugins(ELoadingPhase::PostDefault)){return false;}return true; }?
?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Blueprint和code布局總覽:
比如,在game mode的代碼和blueprint里面都定義當前的角色, 那么當前使用的角色到底是哪個?
想來,如果在代碼的BeginPlay里面定義一些事情,再在blueprint里面的beginPlay節點后面連接定義一些事情,那么估計應該是先走代碼里面的,再走blueprint里面的邏輯;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GameMode部分:
本demo中gamemode C++類沒有創建對應的blueprint資源, 認為,對于一些不需要美術,或者不經常改動的變量,可以不暴露給editor,這樣就不需要額外的blueprint, 純C++類即可, 特別對于一些單件類可能更是如此;
GameMode主要是包含一些游戲性相關的接口,比如AllowCheats, InitGame, InitGameState, GetDefaultPawnClassForController, StartPlay, SetPause, ResetLevel, StartToLeaveMap, PreLogin, ???
CanSpectate(這個好像是是否freecamera)等, 在本demo中, 只是重新實現了InitGameState, RestartPlayer函數, 新增一些如ModifyDamage, ReturnToMenu, FinishGam, ExitGme這樣的函數, 新增的函數如果允許blueprint來調用可以加上屬性”BlueprintCallable”;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GameState部分:
GameStateBase is a class that manages the game's global state, and is spawned by GameModeBase.
這個demo里面這部分沒有對這個類進行特定的blueprint資源;
有個類APlayerState:public AInfo 于文件PlayerState.h, 屬于引擎原生文件, 如playername, playerID, starttime 等;
Demo中這個類添加了一些如敵人(這是個塔防游戲)個數(數組存儲),OnCharPawn(供AI部分代碼調用spawn出新的敵人); SetGamePaused供blueprint調用;
小地圖指針也存儲于此類: TWeakObjectPtr<AStrategyMiniMapCapture> MiniMapCamera;
?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MiniMap部分:
左下角的小地圖有類: class AStrategyMiniMapCapture : public ASceneCapture2D[該父類自帶一個DrawFrustum的component, 是個camera]? 增加一些小地圖寬高,AudioListener的FrontDir,RightDir[但在這里沒用,應該只是存儲,然后更新立體聲時候從這里拿]等,以及輔助capture的變量; 在其BeginPlay函數里面存儲this到GameState中; 根據tick函數來進行capture的更新; ASceneCapture2D自帶GetCaptureComponent2D()->UpdateContent()[內部實現:{ CaptureSceneDeferred(); }];用于更新自身rendertarget的內容;
該類具有blueprint實例, 實例里面定義了當前使用哪個RenderTarget資源;
至于這個類里面的render target,在這個demo里面是自定義的成員變量:
在BeginPlay()里面將其給了父類中rendertarget:
MiniMapView = NewObject<UTextureRenderTarget2D>();MiniMapView->InitAutoFormat(MiniMapWidth,MiniMapHeight);GetCaptureComponent2D()->TextureTarget = MiniMapView;注意這里雖然是New出來的,但是沒有顯式析構; 其定義是?? UPROPERTY() UTextureRenderTarget2D* MiniMapView; 應該是這樣加上屬性令UE4管理其析構的;
這里有點懷疑, 不使用額外的自定義的rendertarget應該也可以, 而在blueprint里面賦值的也只是這個TextureTarget, 而不是類中新增加的MiniMapView;
如此懷疑: ?blueprint與C++代碼的交互是: 代碼的BeginPlay()先走, 給TextureTarget賦值, 然后讀取blueprint里面的值, 否則blueprint里面的值會被覆蓋才對;
總感覺這個變量沒什么用, 去掉應該也可以… …? test … ….
該類內部專屬editor代碼,這部分代碼關聯著C++與blueprint之間交互的機制:https://docs.unrealengine.com/latest/CHN/Programming/Introduction/index.html
#if WITH_EDITOR void AStrategyMiniMapCapture::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) {Super::PostEditChangeProperty(PropertyChangedEvent);UProperty* PropertyThatChanged = PropertyChangedEvent.Property;FName PropertyName = PropertyThatChanged != nullptr ? PropertyThatChanged->GetFName() : NAME_None;if (PropertyName==FName(TEXT("RelativeRotation"))){FRotator ChangedRotation = RootComponent->GetComponentRotation();RootComponent->SetWorldRotation(FRotator(-90,0,ChangedRotation.Roll));} }void AStrategyMiniMapCapture::EditorApplyRotation(const FRotator& DeltaRotation, bool bAltDown, bool bShiftDown, bool bCtrlDown) {FRotator FiltredRotation(0, DeltaRotation.Yaw, 0);Super::EditorApplyRotation(FiltredRotation, bAltDown, bShiftDown, bCtrlDown); }#endif該類內部的GroundLevel定義: capture的camera Z值減去該值即為 [大約]距離地面的高度;
在StrategyPlayerController.Cpp里面發現代碼用于坐標轉換, 射線與平面檢測(比較有用):
const FPlane GroundPlane = FPlane(FVector(0,0,GroundLevel), FVector::UpVector);FViewport* const Viewport = GEngine->GameViewport->ViewportFrame->GetViewport(); FVector2D const ScreenRes = Viewport->GetSizeXY(); FVector RayOrigin, RayDirection; FVector2D const ScreenCenterPoint = ScreenRes * 0.5f;//獲取屏幕中心點 FStrategyHelpers::DeprojectScreenToWorld(ScreenCenterPoint, MyPlayer, RayOrigin, RayDirection);//屏幕中心點坐標轉換到世界空間,傳出世界空間中的射線始點與方向,其內部: FSceneViewProjectionData ProjectionData;if (Player->GetProjectionData(Player->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData)){const FMatrix ViewMatrix = FTranslationMatrix(-ProjectionData.ViewOrigin) * ProjectionData.ViewRotationMatrix;const FMatrix InvViewMatrix = ViewMatrix.InverseFast();const FMatrix InvProjectionMatrix = ProjectionData.ProjectionMatrix.InverseFast();FSceneView::DeprojectScreenToWorld(ScreenPosition, ProjectionData.GetConstrainedViewRect(), InvViewMatrix, InvProjectionMatrix, /*out*/ RayOrigin, /*out*/ RayDirection);return true;}FVector const WorldPoint = FStrategyHelpers::IntersectRayWithPlane(RayOrigin, RayDirection, GroundPlane);//在世界空間進行射線與平面檢測至于這個類及其render target與UI渲染的交互, 繪制到畫面上: 是在class AStrategyHUD : public AHUD的函數DrawMiniMap()里面,該類重載了很多AHUD的函數,如DrawHUD; 默認的HUD可以在gamemode里面進行定義;關于小地圖位置的設定也是在繪制的時候寫死的,繪制:
FCanvasTileItem MapTileItem( FVector2D( 0.0f, 0.0f), FVector2D( 0.0f, 0.0f ), FLinearColor::White );MapTileItem.Texture = MiniMapTexture->Resource;MapTileItem.Size = FVector2D( MapWidth, MapHeight );MapTileItem.BlendMode = SE_BLEND_Opaque;Canvas->DrawItem( MapTileItem, FVector2D( MiniMapMargin * UIScale, Canvas->ClipY - MapHeight - MiniMapMargin * UIScale ) )?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HUD部分:
HUD和菜單本來算是同一種實現方式,但是在本demo中是不一樣的;
?
像上段落提到的,class AStrategyHUD : public AHUD;作為本demo默認的HUD類;
?
如何使用UE4的UMG進行UI設計? 這種方法可以純粹使用blueprint,而不必使用代碼,只是在按鈕按下之類的事件的響應函數中可以利用代碼實現(為了能夠在blueprint里面在響應時候被調用,這種專門后臺處理UI的類應該暴露給blueprint,而這個類可以作為一個成員變量存儲到主角類里面,這樣blueprint里面通過主角來獲得該類,進而調用函數);如果一個C++類具有兩個blueprint實例,那么這兩個實例之間應該是沒關系的,所以對于這種專門后臺處理UI相應事件的邏輯(供blueprint調用)的類, 不必具有blueprint實例; 主要使用的資源類型是”Widget Blueprint”, 在這種資源里面進行UI設計,添加相應事件,調用響應函數; 游戲整體與這個資源進行交互的機制是, 一般可以在level blueprint(比如menu是個專門的level, 就具有專門的level blueprint)里面使用”Create Wedget”節點,使用這個資源(比如做彈出菜單的時候可以常用這種):
然后將這個”PauseMenuReference”作為節點”Add to Viewport”的target, 將菜單添加到Viewport中;
關于UI相關的資源在UE4中有四種:
1. Font: 字體;
本demo中沒有新建字體;
2. Slate Brush: An asset describing how a texture can exist in slate’s DPI-aware environment and how this texture responds resizing, eg. Scale9-stretching? Tiling?
在blueprint里面可以使用節點“MakeSlateBrush”來創建一個SlateBrush;
一個brush可以包一個UTexture或者UMaterialInstance,一個brush應該可以理解為,設定一個貼圖怎樣繪制(大小,邊緣怎樣處理,對其方式);
在Widget Blueprint資源里面,如果拖進去一個image的控件,會發現屬性里面有Brush一欄,里面的設定和一個Slate Brush的設定是一樣的;
在本demo中,創建了一個Slate Brush資源,使用的方法是(看起來是作為備份的默認圖片):
const FSlateBrush* SStrategyButtonWidget::GetButtonImage() const {if (ButtonImage.IsValid()){return ButtonImage.Get();} else {return FStrategyStyle::Get().GetBrush("DefaultActionImageBrush");} }可以看到這種slate brush資源“DefaultActionImageBrush”是做后備萬一的;
3.?Slate Widget Style: Just a wrapper for the struct with real data in it;
4.?Widget Blueprint: The widget blueprint enables extending UUserWidget the user extensible UWidget; 其實可以完全不使用代碼,僅僅通過這種資源來進行所有UI的設定,然后在blueprint里面通過節點“”“Create Widget”引用這個資源,然后“Add to Viewport”即可;只是本demo全是C++代碼寫的,類似于早期的純代碼寫windows app的UI,而不是利用MFC拖控件的方式;
?
?
- 自定義Widget:
Widget可以理解為在UE4中即指代UI控件,本demo沒有使用Widget Blueprint資源來涉及UI,所有都是代碼寫的,創建了很多繼承自ScompoundWidget的類,作為一種新的自定義的Widget來作為UI的最小單元控件(后被用于組合成一個style);
如:class SStrategyButtonWidget : public SCompoundWidget : public SWidget
自定義Widget內部具體內容(如點擊時的事件回調函數,button上的text等)需要以SLATE_BEGIN_ARGS(SStrategyButtonWidget)形式開始,以SLATE_END_ARGS()形式結束,內容例如:
SLATE_BEGIN_ARGS(SStrategyButtonWidget){}/* Owning HUD for getting Game World */SLATE_ARGUMENT(TWeakObjectPtr<AStrategyHUD>, OwnerHUD)SLATE_DEFAULT_SLOT(FArguments, Content)/** called when the button is clicked */SLATE_EVENT(FOnClicked, OnClicked)//這個是自定義的新事件并自動被delegate,其實在SWidget里面有OnMouseButtonDown這樣的虛函數已經托管好了,重載即可,這里拿來理解怎樣新增自己事件以及綁定好回調函數/** text on the button */SLATE_ATTRIBUTE(FText, ButtonText)SLATE_END_ARGS()又如:class SStrategyMiniMapWidget : public SCompoundWidget : public SWidget
這個是自定義小地圖使用的widget,這部分和小地圖的那個rendertarget渲染部分和事件響應(鼠標在小地圖上點擊,移動)是有關系的,該類還重載了OnPaint(…)函數, 這個函數內部只是繪制小地圖上的白線的,通過FSlateDrawElement::MakeLines(…)函數;
至于小地圖上的那個rendertarget的繪制,小地圖部分已經提及;
?
一點比較有用的代碼:
AStrategyPlayerController* const PC = Cast<AStrategyPlayerController>(GEngine->GetFirstLocalPlayerController(OwnerHUD.Get()->GetWorld()));AStrategyGameState const* const MyGameState = PC && PC->GetWorld() ? PC->GetWorld()->GetGameState<AStrategyGameState>() : NULL;AStrategyHUD* const HUD = PC ? Cast<AStrategyHUD>(PC->MyHUD) : NULL;注意這里自定義的幾種新的Widget,但是對于editor而言是沒用的,也沒有對應的資源之類的東西,只是邏輯代碼上的東西;這些自定義的widget應該被用于后面實現class SStrategySlateHUDWidget : public SCompoundWidget;
自定義的widget在其構造函數中會初始化該widget的屬性,如:(https://docs.unrealengine.com/latest/CHN/Programming/Slate/Overview/index.html )
?
void SStrategyButtonWidget::Construct(const FArguments& InArgs) {OwnerHUD = InArgs._OwnerHUD;ButtonText = InArgs._ButtonText;CenterText = InArgs._CenterText;CornerText = InArgs._CornerText;OnClicked = InArgs._OnClicked;OnClickedDisabled = InArgs._OnClickedDisabled;CoinIconVisible = InArgs._CoinIconVisible;TextHAlign = InArgs._TextHAlign; TextVAlign = InArgs._TextVAlign; TextMargin = InArgs._TextMargin;TextFont = InArgs._TextFont;Opacity = InArgs._Opacity;bIsUserActionRequired = false;bIsMouseButtonDown = false;bIsActiveAction = false;bIsActionAllowed = true;OnMouseEnterDel = InArgs._OnMouseEnterDel;OnMouseLeaveDel = InArgs._OnMouseLeaveDel;OpacityCurve = WidgetAnimation.AddCurve(0.0f, 0.2f, ECurveEaseFunction::QuadInOut);bMouseCursorVisible = true;ChildSlot.VAlign(VAlign_Fill).HAlign(HAlign_Fill)[SNew(SOverlay)+SOverlay::Slot().HAlign(HAlign_Center).VAlign(VAlign_Center)[SNew(SImage).Image(this, &SStrategyButtonWidget::GetButtonImage).ColorAndOpacity(this,&SStrategyButtonWidget::GetImageColor)]+SOverlay::Slot().HAlign(HAlign_Center).VAlign(VAlign_Center)[SNew(SImage).Image(this, &SStrategyButtonWidget::GetButtonImage).ColorAndOpacity(this,&SStrategyButtonWidget::GetTintColor)]+SOverlay::Slot().HAlign(TextHAlign.Get().IsSet() ? TextHAlign.Get().GetValue() : EHorizontalAlignment::HAlign_Center).VAlign(TextVAlign.Get().IsSet() ? TextVAlign.Get().GetValue() : EVerticalAlignment::VAlign_Bottom).Padding(TAttribute<FMargin>(this, &SStrategyButtonWidget::GetTextMargin))[SNew(STextBlock).ShadowColorAndOpacity(this,&SStrategyButtonWidget::GetTextShadowColor).ColorAndOpacity(this,&SStrategyButtonWidget::GetTextColor).ShadowOffset(FIntPoint(-1,1)).Font(this, &SStrategyButtonWidget::GetTextFont).Text(ButtonText)]+SOverlay::Slot().HAlign(EHorizontalAlignment::HAlign_Center).VAlign(EVerticalAlignment::VAlign_Center)[SNew(STextBlock).ShadowColorAndOpacity(this,&SStrategyButtonWidget::GetTextShadowColor).ColorAndOpacity(this,&SStrategyButtonWidget::GetTextColor).ShadowOffset(FIntPoint(-1,1)).Font(this, &SStrategyButtonWidget::GetTextFont).Text(CenterText)]+SOverlay::Slot().HAlign(EHorizontalAlignment::HAlign_Right).VAlign(EVerticalAlignment::VAlign_Top)[SNew(STextBlock).ShadowColorAndOpacity(this,&SStrategyButtonWidget::GetTextShadowColor).ColorAndOpacity(this,&SStrategyButtonWidget::GetTextColor).ShadowOffset(FIntPoint(-1,1)).Text(CornerText)]+SOverlay::Slot()[InArgs._Content.Widget]]; } View Code在本demo實現class SStrategySlateHUDWidget : public SCompoundWidget的時候, 其內部構造函數就用到了之前實現的幾個別的Widget,也就是說,像class SStrategyMiniMapWidget,class SStrategyButtonWidget這樣的widget都是為class SStrategySlateHUDWidget服務的,雖然他們都是Widget,并且具有相同的父子繼承關系;
所以在void SStrategySlateHUDWidget::Construct(const FArguments& InArgs)里面有:
SNew(SCanvas) SNew(SBorder) SAssignNew(ActionButtonsWidget,SStrategyActionGrid) SAssignNew(MiniMapWidget,SStrategyMiniMapWidget) SNew(SImage) SAssignNew(PauseMenuButtons[ButtonIndex++], SStrategyButtonWidget)即是說,SStrategySlateHUDWidget 類把之前的小地圖自定義widget,button自定義widget等widget組合了起來(指針成為其成員變量),它使用了它們;
另外在它重載的Tick函數里面有兩行代碼比較好用:
UConsole* ViewportConsole = (GEngine !=NULL && GEngine->GameViewport != NULL) ? GEngine->GameViewport->ViewportConsole : NULL;if (ViewportConsole != NULL && (ViewportConsole->ConsoleState == "Typing" || ViewportConsole->ConsoleState == "Open")){FSlateApplication::Get().SetAllUserFocusToGameViewport();FSlateApplication::Get().SetKeyboardFocus(SharedThis(this));}然后組合了各種自定義的Widget的這個SStrategySlateHUDWidget又被class AStrategyHUD : public AHUD使用,在其BuildMenuWidgets(…)函數里面被SAssignNew出來:
void AStrategyHUD::BuildMenuWidgets() {if (!GEngine || !GEngine->GameViewport){return;}if (!MyHUDMenuWidget.IsValid()){const AStrategyPlayerController* PCOwner = Cast<AStrategyPlayerController>(PlayerOwner);if (PCOwner){SAssignNew(MyHUDMenuWidget, SStrategySlateHUDWidget)//AstrategyHUD具有成員變量TSharedPtr<class SStrategySlateHUDWidget> MyHUDMenuWidget;.OwnerHUD(this);if (MyHUDMenuWidget.IsValid()){GEngine->GameViewport->AddViewportWidgetContent(SNew(SWeakWidget).PossiblyNullContent(MyHUDMenuWidget.ToSharedRef()));MyHUDMenuWidget->ActionButtonsWidget->SetVisibility(EVisibility::Visible);MyHUDMenuWidget->ActionWidgetPosition.BindUObject(this,&AStrategyHUD::GetActionsWidgetPos);if (ActionPauseTexture != NULL){MyHUDMenuWidget->PauseButton->SetImage(ActionPauseTexture);MyHUDMenuWidget->PauseButton->DeferredShow();}if (MenuButtonTexture != NULL){for (uint8 i = 0; i < MyHUDMenuWidget->PauseMenuButtons.Num(); i++){MyHUDMenuWidget->PauseMenuButtons[i]->SetImage(MenuButtonTexture);}}}}} } View Code【這塊HUD的分析思路有誤,應該從默認HUD類class AStrategyHUD : public AHUD分析起,會發現其使用了自定義widget SStrategySlateHUDWidget, 然后進入到SStrategySlateHUDWidget的構造函數里面會發現其又組合里其他自定義widget;這樣自頂向下的分析會更有效率更清晰; 以上的分析是自下至上的,容易糊涂;不過自頂而下的分析需要知道頂在哪里;】
?
- 自定義Style:
“Styles can be created and applied to the various parts of a widget. This makes it easy to iterate on the look of the components in the UI, as well as share and reuse styles.”;
本demo中,WidgetStyle類似是個容器類,它組合了之前的自定義widget,
這里style的基類:struct SLATECORE_API FSlateWidgetStyle,僅僅是個struct;
struct FStrategyHUDStyle : public FSlateWidgetStyle, 該類作為容器內部包含了FSlateBrush(可看出帶配置的texture),FSlateColor等,然后定義一個class UStrategyHUDWidgetStyle : public USlateWidgetStyleContainerBase,是正規的類,包含一個FStrategyHUDStyle類型,這樣blueprint就可以對其賦值了;
然后editor中創建這個類對應的style資源,然后再在某個構造函數(void SStrategyMenuWidget::Construct(const FArguments& InArgs))中查找使用它:
MenuStyle = &FStrategyStyle::Get().GetWidgetStyle<FStrategyMenuStyle>("DefaultStrategyMenuStyle");//注意這里是按照名字查找相關style的,也就是說,所有的widget style都應該放到一個map中,查找代碼發現(SlateStyle.h,class SLATECORE_API FSlateStyleSet : public ISlateStyle): FString CoreContentRootDir; /** This dir is Engine/Slate folder to share the items **/TMap< FName, TSharedRef< struct FSlateWidgetStyle > > WidgetStyleValues;會把所有的slate widget style收集起來; #define IMAGE_BRUSH( RelativePath, ... ) FSlateImageBrush( FPaths::GameContentDir() / "Slate"/ RelativePath + TEXT(".png"), __VA_ARGS__ ) #define BOX_BRUSH( RelativePath, ... ) FSlateBoxBrush( FPaths::GameContentDir() / "Slate"/ RelativePath + TEXT(".png"), __VA_ARGS__ ) #define BORDER_BRUSH( RelativePath, ... ) FSlateBorderBrush( FPaths::GameContentDir() / "Slate"/ RelativePath + TEXT(".png"), __VA_ARGS__ ) #define TTF_FONT( RelativePath, ... ) FSlateFontInfo( FPaths::GameContentDir() / "Slate"/ RelativePath + TEXT(".ttf"), __VA_ARGS__ ) #define OTF_FONT( RelativePath, ... ) FSlateFontInfo( FPaths::GameContentDir() / "Slate"/ RelativePath + TEXT(".otf"), __VA_ARGS__ )?
- 自定義Menu:
除了class AStrategyHUD : public AHUD還有一個hud:class AStrategyMenuHUD : public AHUD,按照之前的分析,前者主要是小地圖,右上角的三個代表生命個數的木桶,后者主要是菜單hud;菜單HUD;editor中定義的是前者AStrategyHUD;
菜單除了具有單獨的HUD,菜單還有單獨的game mode和controller:
AStrategyMenuGameMode::AStrategyMenuGameMode(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {//setup our custom PC and HUDPlayerControllerClass = AStrategyMenuPlayerController::StaticClass();HUDClass = AStrategyMenuHUD::StaticClass();SpectatorClass = AStrategySpectatorPawn::StaticClass(); }這個menu gamemode好像沒有顯式使用的地方,也沒有對應的資源;這個Menu HUD的一個指針引用保存在了class SStrategyMenuWidget : public SCompoundWidget里面;
?
這個menu部分,好像一直是獨立出來的一塊似的;它的HUD,game mode,widget, PlayerController似乎都是自己和自己在用,沒找到別的地方有使用它的;?????這是個沒有完成的demo???
這個demo中的兩個地圖,兩個game mode,他們是怎樣工作的?難道是一個地圖就可以有一個game mode?
發現,當游戲啟動時候,會兩個game mode的構造函數都會逐一進入,先進入的是AStrategyGameMode然后是AStrategyMenuGameMode的;
在DefaultEngine.ini里面:
GameDefaultMap=/Game/Maps/StrategyMenu ServerDefaultMap=/Game/Maps/StrategyMenu EditorStartupMap=/Game/Maps/TowerDefenseMap GlobalDefaultGameMode="/Script/StrategyGame.StrategyGameMode"這樣的設定應該決定了游戲剛起來的時候默認地圖是StrategyMenu,所以在游戲開時候回有LoadMap(…)調用來加載地圖StrategyMenu,然后在LoadMap函數內部調用SetGameMode(…):
WorldContext.World()->SetGameMode(URL);//看來似乎一個地圖一個world就有一個game mode設定 bool UWorld::SetGameMode(const FURL& InURL) {if( IsServer() && !AuthorityGameMode ){AuthorityGameMode = GetGameInstance()->CreateGameModeForURL(InURL);//這個URL里面包含了地圖名StrategyMenu,此時正在加載這個地圖,按這個函數名來看,似乎會依賴loadmap的地圖信息弄出來個game modeif( AuthorityGameMode != NULL ){return true;}else{UE_LOG(LogWorld, Error, TEXT("Failed to spawn GameMode actor."));return false;}}return false; }打開地圖可以發現,對于主要的游戲地圖TowerDefenseMap,它的World Setting里面使用的GameModeOverride是“StrategyGameMode”,而StrategyMenu地圖里面的GameOverride是“StrategyMenuGameMenu”,所以知道了:整個游戲可以有自己的game mode,各個地圖可以override自己的game mode,當然也就會有自己獨特專屬的Controller,HUD等等:
AStrategyMenuGameMode::AStrategyMenuGameMode(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {//setup our custom PC and HUDPlayerControllerClass = AStrategyMenuPlayerController::StaticClass();HUDClass = AStrategyMenuHUD::StaticClass();SpectatorClass = AStrategySpectatorPawn::StaticClass(); }所以在地圖StrategyMenu里面看到什么都沒有,它的作用只是在它的world setting里面設定override的專屬game mode,然后這個專屬的game mode AStrategyMenuGameMode的構造函數里面會有設定專屬的Controller和HUD以及SpectatorClass(這個就是主角類,玩家控制的),而這個StrategyMenu在project setting里面里面設定的是作為Startup map,所以游戲啟動后,加載這個菜單地圖,然后其構造函數指定這個菜單地圖的HUD,controller,SpectatorClass;
對應菜單專屬的controller AStrategyMenuPlayerController,沒有做特殊時期,主要是SetupInputComponent()函數的重載實現, 并且沒有特殊的實現,用的是PlayerController.CPP的;
?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Menu部分:
class SStrategyMenuWidget : public SCompoundWidget 這個是menu的widget,其構造函數中找到menu的style:MenuStyle = &FStrategyStyle::Get().GetWidgetStyle<FStrategyMenuStyle> ("DefaultStrategyMenuStyle"); 這style就有點像一個容器內部包含了很多菜單所需要的texture(類型FStrategyMenuStyle包含了SlateBrush,SlateSound等,然后editor中有這個類對應的blueprint資源,這個資源名字就是“DefaultStrategyMenuStyle”),然后在SStrategyMenuWidget的構造函數中通過MenuStyle找到這些texture,然后構建菜單按鈕貼圖等的布局;
在AStrategyMenuHUD的構造函數里面,直接通過代碼方式增加子菜單的四個按鈕“Easy”“Normal”“Hard”“Back”;在SStrategyMenuWidget有各個按鈕的回調函數;這樣菜單就啟動起來了;
?
關于菜單間的切換進入游戲部分:
當點擊easy等游戲難度選擇按鈕后,會調用到回調函數:
void AStrategyMenuHUD::LaunchGame() {FString StartStr = FString::Printf(TEXT("/Game/Maps/TowerDefenseMap?%s=%d"), *AStrategyGameMode::DifficultyOptionName, (uint8) Difficulty);GetWorld()->ServerTravel(StartStr);//這里準備加載游戲主地圖ShowLoadingScreen();//但是主地圖不會馬上加載并顯示進來,所以需要一個loading頁面,按照之前的分析,這個loading screen是在另外一個module里面,另外的module也不一定是同一個線程 }對于函數ShowLoadingScreen():
void AStrategyMenuHUD::ShowLoadingScreen() {IStrategyGameLoadingScreenModule* LoadingScreenModule = FModuleManager::LoadModulePtr<IStrategyGameLoadingScreenModule>("StrategyGameLoadingScreen");//這里就載入了新的module,進入加載游戲的那個頁面if( LoadingScreenModule != nullptr ){LoadingScreenModule->StartInGameLoadingScreen();} }這里看出,UE4在切換的時候,使用GetWorld()->ServerTravel(StartStr);這樣的函數,使得游戲轉入下一個地圖即游戲主地圖“TowerDefenseMap”,同理,在載入地圖的時候會加載override的gamemode “AStrategyGameMode”,其內部構造函數設定使用的controller,state,HUD,controller,DefaultPawnClass,就把各個類結合了起來,隨后比如定義在state(類AStrategyGameState)里面的諸如重載的InitGameState這樣的函數就會開始走,初始化游戲狀態相關內容;
?
點擊建筑,建筑上會出現即時彈出菜單,都是定義在StrategyBuilding文件里,彈出菜單存儲在ASrategyHUD里面,只是顯示隱藏,控件點擊的觸發回調都是一樣的;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AI部分:
NPC行走路線的定義:
NPC自動轉身走動:
CONTINUE ... ...
/*** SensingComponent encapsulates sensory (ie sight and hearing) settings and functionality for an Actor,* allowing the actor to see/hear Pawns in the world. It does *not* enable hearing* and sight sensing by default.*/ UCLASS(config=Game) class UStrategyAISensingComponent : public UPawnSensingComponentStrategyAIController.h: DECLARE_DELEGATE_OneParam(FOnBumpEvent, FHitResult const&); DECLARE_DELEGATE(FOnMovementEvent); 如此這樣聲明一個事件,然后比如” FOnMovementEvent”就成為一種類型,可以聲明變量;FOnMovementEvent MovementDelegate;MovementDelegate.BindUObject(this, &UStrategyAIAction_MoveToBrewery::OnMoveCompleted);MyAIController->RegisterMovementEventDelegate(MovementDelegate);?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Gameplay部分:
在文件DefaultGame.ini里面:
[/Script/StrategyGame.StrategyGameMode] TimeBeforeReturnToMenu=3 [/Script/StrategyGame.StrategyGameState] WarmupTime=3然后找到其對應的代碼變量(StrategyGameMode.h):
/** Time before game returns to menu after finish. */UPROPERTY(config)int32 TimeBeforeReturnToMenu;/** Warm up time before game starts */UPROPERTY(config)int32 WarmupTime;可以知道如果想要讀取這個ini里面的變量,只需要在聲明變量的時候加上config關鍵字即可;
所以在游戲主界面的游戲啟動前(點擊了選擇難度按鈕后)的暖場時間,這個所謂的暖場時間只是主游戲界面左上角的倒計時,跟加載頁面時間沒關系,邏輯是(StrategyGameState.cpp):
void AStrategyGameState::StartGameplayStateMachine() {if (WarmupTime > 0.f){SetGameplayState(EGameplayState::Waiting);GetWorldTimerManager().SetTimer(TimerHandle_OnGameStart, this, &AStrategyGameState::OnGameStart, WarmupTime, false);}else{OnGameStart();} }關于這個啟動游戲前的暖場時間和機制:
點擊easy難度頁面后調用:
void AStrategyMenuHUD::LaunchGame() {FString StartStr = FString::Printf(TEXT("/Game/Maps/TowerDefenseMap?%s=%d"), *AStrategyGameMode::DifficultyOptionName, (uint8) Difficulty);GetWorld()->ServerTravel(StartStr);//@Virtuos[wangsongwei]here we begin to load the main game map TowerDefenseMapShowLoadingScreen();//@Virtuos[wangsongwei]since the main game map will not load and appear quickly, we need one loading screen, and this screen is in another module, but this also is same one thread } ServerTravel函數加載主游戲地圖,初始化state,在初始化state時候有調用: void AStrategyGameState::StartGameplayStateMachine() {if (WarmupTime > 0.f){SetGameplayState(EGameplayState::Waiting);GetWorldTimerManager().SetTimer(TimerHandle_OnGameStart, this, &AStrategyGameState::OnGameStart, WarmupTime, false);//這里設定的不是loading頁面的顯示時間,只是游戲正式開始的倒計時,其實是一個等待時間,時間到了,就調用這里設定的回調函數開始游戲 }else{OnGameStart();} }ShowLoadingScreen函數加載額外的那個screen loading的module,這個module不一定是有單獨線程在走的,這個demo似乎是沒有:
這個加載頁面,它的具體實現是(StrategyGameLoadingScreen.cpp):
virtual void CreateScreen(){FLoadingScreenAttributes LoadingScreen;LoadingScreen.bAutoCompleteWhenLoadingCompletes = true;LoadingScreen.MinimumLoadingScreenDisplayTime = 0.f;//這個值是可以改變等待時間的,單位是秒LoadingScreen.WidgetLoadingScreen = SNew(SStrategyLoadingScreen);GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);}按照這里的設定,是加載完成就自動退出,怎樣才算是加載完成呢?它能自動監視游戲的state嗎?主要邏輯部分代碼在DefaultGameMoviePlayer.cpp里面;
?
?
這里的主視角的camera是class UStrategyCameraComponent : public UCameraComponent作為成員變量存儲在StrategySpectatorPawn里面的,AStrategySpectatorPawn重載了MoveForward之類的函數來控制camera動;引擎怎么知道這個camera是主視角的camera?
發現當我們觸發一個debug camera自由移動場景時候(ToggleDebugCamera命令行輸入),void ADebugCameraController::OnActivate( APlayerController* OriginalPC )會進入,會有:
float const OrigCamFOV = OriginalPC->PlayerCameraManager->GetFOVAngle();可以看出,主視角camera是存儲在playerController里面的PlayerCameraManager里面的:
/** Camera manager associated with this Player Controller. */UPROPERTY(BlueprintReadOnly, Category=PlayerController)class APlayerCameraManager* PlayerCameraManager;/** PlayerCamera class should be set for each game, otherwise Engine.PlayerCameraManager is used */UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=PlayerController)TSubclassOf<class APlayerCameraManager> PlayerCameraManagerClass;對于菜單的那個player controller是這么對其賦值的:
AStrategyMenuPlayerController::AStrategyMenuPlayerController(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {// just use the normal camera...fine for nowPlayerCameraManagerClass = APlayerCameraManager::StaticClass(); }APlayerCameraManager 雖然不包括camera component,但是其具有camera的必要信息;
在加載進入主游戲地圖時候,會先spawn player controller,在spawn player controller的過程中會進行SpawnPlayerCameraManager()會有一些信息存儲到APlayerCameraManager里面;
對于這個游戲demo而言,主視角只有平移操作,就是在平移ASpectatorPawn主角類對象,因為camera是它的子component,所以位置也變了,這樣主視角變化;
而對于縮放整個level場景,實現方法的調用堆棧是:
void APlayerController::UpdateCameraManager(float DeltaSeconds) void APlayerCameraManager::UpdateCamera(float DeltaTime) void APlayerCameraManager::DoUpdateCamera(float DeltaTime) void APlayerCameraManager::UpdateViewTarget(FTViewTarget& OutVT, float DeltaTime) void APlayerCameraManager::UpdateViewTargetInternal(FTViewTarget& OutVT, float DeltaTime) void AActor::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult) void UStrategyCameraComponent::GetCameraView(float DeltaTime, FMinimalViewInfo& OutResult) { APlayerController* Controller = GetPlayerController();if( Controller ) {OutResult.FOV = 30.f;const float CurrentOffset = MinCameraOffset + ZoomAlpha * (MaxCameraOffset - MinCameraOffset);//縮放整個場景主要改變ZoomAlpha這個值實現的FVector Pos2 = Controller->GetFocalLocation();OutResult.Location = Controller->GetFocalLocation() - FixedCameraAngle.Vector() * CurrentOffset;OutResult.Rotation = FixedCameraAngle;} }在PlayerCameraManager.h里面:
/** A ViewTarget is the primary actor the camera is associated with. */ USTRUCT() struct ENGINE_API FTViewTarget { class AActor* Target; }Camera的ViewTarget保存camera注視的是?像這里就是“StrategySpectatorPawn_0”
??
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Input部分:
PlayerController.h:
/** Object that manages player input. */UPROPERTY(transient)class UPlayerInput* PlayerInput;?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sound部分:
類型可以是AmbientSound這種actor;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
作弊cheat部分:?
FReply SStrategySlateHUDWidget::OnCheatAddGold() const {FReply Reply = FReply::Unhandled();APlayerController* PlayerController = Cast<APlayerController>(OwnerHUD->GetWorld()->GetFirstPlayerController());if (PlayerController){UStrategyCheatManager* CheatManager = Cast<UStrategyCheatManager>(PlayerController->CheatManager);if (CheatManager != nullptr){CheatManager->AddGold(10000);Reply = FReply::Handled();}}return Reply; }其中class UStrategyCheatManager : public UCheatManager;
UFUNCTION(exec)// This function is executable from the command line.void AddGold(uint32 NewGold);關于UCheatManager類,有定義free camera的controller:
UCLASS(Blueprintable, Within=PlayerController) class ENGINE_API UCheatManager : public UObject {GENERATED_UCLASS_BODY()/** Debug camera - used to have independent camera without stopping gameplay */UPROPERTY()class ADebugCameraController* DebugCameraControllerRef;/** Debug camera - used to have independent camera without stopping gameplay */UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Debug Camera")TSubclassOf<class ADebugCameraController> DebugCameraControllerClass;… … } /** * Camera controller that allows you to fly around a level mostly unrestricted by normal movement rules. * * To turn it on, please press Alt+C or both (left and right) analogs on XBox pad, * or use the "ToggleDebugCamera" console command. Check the debug camera bindings * in DefaultPawn.cpp for the camera controls. */ UCLASS(config=Game) class ENGINE_API ADebugCameraController: public APlayerController { … … } View CodeUE4還提供了一個專門的類ADebugCameraHUD可供參考,在文件DebugCameraHUD.CPP里面;我們要實現的時候可以繼承他;
PlayerController.h:
/** Object that manages "cheat" commands. Not instantiated in shipping builds. */UPROPERTY(transient, BlueprintReadOnly, Category="Cheat Manager")class UCheatManager* CheatManager;/** Enables cheats within the game */UFUNCTION(exec)virtual void EnableCheats();?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
其他:
- 場景中的燈光都要被包含在一個LightmassImportanceVolume里面;
- 關于碰撞檢測, 對于整個地面的那個static mesh actor:
而對于其他的actor, 則Collision Presets的設定是NoCollision, 比如墻,石頭,木桶,都是可以穿過去的,為了防止此點, 場景中增加了大量的block volume; 利用block volume比物理actor之間檢測碰撞應該更有效率;
- 武器attach到(NPC)角色身上的機制:
這種設定,使得在level blueprint里面設定的一個weapons(暫時先存儲到UStrategyAIDirector : public UActorComponent 類里面, 這種繼承關系應該只是為了tick),然后就被應用到了所有的NPC, 其實并不很好,只是這里的demo適合;
namespace UF {// valid keywords for the UFUNCTION and UDELEGATE macrosenum {/// This function is designed to be overridden by a blueprint. Do not provide a body for this function;/// the autogenerated code will include a thunk that calls ProcessEvent to execute the overridden body. BlueprintImplementableEvent,/// This function is designed to be overridden by a blueprint, but also has a native implementation./// Provide a body named [FunctionName]_Implementation instead of [FunctionName]; the autogenerated/// code will include a thunk that calls the implementation method when necessary. BlueprintNativeEvent,/// This function is sealed and cannot be overridden in subclasses./// It is only a valid keyword for events; declare other methods as static or final to indicate that they are sealed. SealedEvent,/// This function is executable from the command line. Exec,/// This function is replicated, and executed on servers. Provide a body named [FunctionName]_Implementation instead of [FunctionName];/// the autogenerated code will include a thunk that calls the implementation method when necessary. Server,/// This function is replicated, and executed on clients. Provide a body named [FunctionName]_Implementation instead of [FunctionName];/// the autogenerated code will include a thunk that calls the implementation method when necessary. Client,/// This function is both executed locally on the server and replicated to all clients, regardless of the Actor's NetOwner NetMulticast,/// Replication of calls to this function should be done on a reliable channel./// Only valid when used in conjunction with Client or Server Reliable,/// Replication of calls to this function can be done on an unreliable channel./// Only valid when used in conjunction with Client or Server Unreliable,/// This function fulfills a contract of producing no side effects, and additionally implies BlueprintCallable. BlueprintPure,/// This function can be called from blueprint code and should be exposed to the user of blueprint editing tools. BlueprintCallable,/// This function will not execute from blueprint code if running on something without network authority BlueprintAuthorityOnly,/// This function is cosmetic and will not run on dedicated servers BlueprintCosmetic,/// The UnrealHeaderTool code generator will not produce a execFoo thunk for this function; it is up to the user to provide one. CustomThunk,/// Specifies the category of the function when displayed in blueprint editing tools./// Usage: Category=CategoryName or Category="MajorCategory,SubCategory" Category,/// This function must supply a _Validate implementation WithValidation,/// This function is RPC service request ServiceRequest,/// This function is RPC service response ServiceResponse}; } View Code- PlayerController.h:
?
posted on 2016-12-26 17:23?DeanWang 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/DeanWang/p/6222865.html
總結
以上是生活随笔為你收集整理的《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery.nicescroll参数说
- 下一篇: 我为什么会选择计算机专业之 《我的编程人