《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
整體模塊module分析:
打開(kāi)StrategyGame.uproject來(lái)看:
{"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", //這個(gè)是額外增加的module"Type": "Runtime","LoadingPhase": "PreLoadingScreen" //從代碼中找到解釋, 這個(gè)module必須設(shè)置成這種類(lèi)型, 否則不會(huì) “hook in time”, 從字面意思來(lái)看,是預(yù)加載一直存在于內(nèi)存中的; }],"EpicSampleNameHash": "0","TargetPlatforms": ["Android","IOS","MacNoEditor","WindowsNoEditor"] }可以看到這里面定義了兩個(gè)模塊, 名字,類(lèi)型,還有一個(gè)”LoadingPhase”的屬性;
這個(gè)文件里面的內(nèi)容是自己定義的, 還是自動(dòng)生成的 ?
CONTINUE ... ...
?
這個(gè)demo從整體模塊來(lái)講可以看成有兩個(gè), 一個(gè)模塊這里指的是生成dll的個(gè)數(shù), 除了主模塊StrategyGame之外,還有一個(gè)StrategyGameLoadingScreen:
- StrategyGameLoadingScreen: 作為子模塊, 會(huì)生成一個(gè)dll, UE4Editor-StrategyGameLoadingScreen.dll(只是在editor里面編譯運(yùn)行過(guò)游戲); 具體實(shí)現(xiàn)的時(shí)候添加的內(nèi)容有:
文件StrategyGameLoadingScreen.h/cpp
FStrategyGameLoadingScreenModule : IStrategyGameLoadingScreenModule : public IModuleInterface
利用這樣的繼承關(guān)系實(shí)現(xiàn)一個(gè)新的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"});} }? ??可以看到其內(nèi)部定義了該模塊的額外include path, 以及使用那些引擎的原生模塊; 認(rèn)為, 可能是: public為屬性的模塊可以和其他模塊進(jìn)行接口之間調(diào)用的交互,而private的只能是自己當(dāng)前模塊使用的(參考RulesCompiler.cs);
??? 這個(gè)模塊的主要功能是實(shí)現(xiàn)開(kāi)始菜單的部分,并且一些UI元素如背景圖等是代碼寫(xiě)出來(lái)的(class SStrategyLoadingScreen 的Construct 函數(shù) ), 在此發(fā)現(xiàn)SNew和SAssignNew是UI 進(jìn)行new專(zhuān)用的;
? ? 但是對(duì)于這個(gè)demo的UI而言,這個(gè)module里面的內(nèi)容還不夠, 猜測(cè)主模塊StrategyGame里面對(duì)于UI的處理是結(jié)合這個(gè)模塊的;
- 主模塊StrategyGame:
這個(gè)模塊對(duì)應(yīng)了一個(gè)UE4Editor-StrategyGame.dll; 其內(nèi)部使用了上面那個(gè)StrategyGameLoadingScreen模塊:
?
在StrategyGame.Build.cs文件里面除了增加其他module,還有:
?
?
PrivateDependencyModuleNames.AddRange(new string[] {"StrategyGameLoadingScreen"});- 主模塊StrategyGame和StrategyGameLoadingScreen模塊的交互:
?這個(gè)demo中,主要是主模塊調(diào)用子模塊函數(shù)然后讓其顯示菜單背景圖等, 需要包含子模塊的頭文件, 顯式加載子模塊, 調(diào)用子模塊的具體實(shí)現(xiàn):
void AStrategyMenuHUD::ShowLoadingScreen() {IStrategyGameLoadingScreenModule* LoadingScreenModule = FModuleManager::LoadModulePtr<IStrategyGameLoadingScreenModule>("StrategyGameLoadingScreen");if( LoadingScreenModule != nullptr ){LoadingScreenModule->StartInGameLoadingScreen();} }這個(gè)StrategyGameLoadingScreen模塊作為具有PreLoadingScreen屬性的模塊,在游戲啟動(dòng)時(shí)候會(huì)專(zhuān)門(mén)先加載(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; }關(guān)于模塊加載函數(shù)LoadModulesForProject其參數(shù)是:
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屬性會(huì)在加載的時(shí)候具有不同的時(shí)機(jī);
看后面可以發(fā)現(xiàn)會(huì)逐一加載后面幾個(gè)屬性的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里面都定義當(dāng)前的角色, 那么當(dāng)前使用的角色到底是哪個(gè)?
想來(lái),如果在代碼的BeginPlay里面定義一些事情,再在blueprint里面的beginPlay節(jié)點(diǎn)后面連接定義一些事情,那么估計(jì)應(yīng)該是先走代碼里面的,再走blueprint里面的邏輯;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GameMode部分:
本demo中g(shù)amemode C++類(lèi)沒(méi)有創(chuàng)建對(duì)應(yīng)的blueprint資源, 認(rèn)為,對(duì)于一些不需要美術(shù),或者不經(jīng)常改動(dòng)的變量,可以不暴露給editor,這樣就不需要額外的blueprint, 純C++類(lèi)即可, 特別對(duì)于一些單件類(lèi)可能更是如此;
GameMode主要是包含一些游戲性相關(guān)的接口,比如AllowCheats, InitGame, InitGameState, GetDefaultPawnClassForController, StartPlay, SetPause, ResetLevel, StartToLeaveMap, PreLogin, ???
CanSpectate(這個(gè)好像是是否freecamera)等, 在本demo中, 只是重新實(shí)現(xiàn)了InitGameState, RestartPlayer函數(shù), 新增一些如ModifyDamage, ReturnToMenu, FinishGam, ExitGme這樣的函數(shù), 新增的函數(shù)如果允許blueprint來(lái)調(diào)用可以加上屬性”BlueprintCallable”;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GameState部分:
GameStateBase is a class that manages the game's global state, and is spawned by GameModeBase.
這個(gè)demo里面這部分沒(méi)有對(duì)這個(gè)類(lèi)進(jìn)行特定的blueprint資源;
有個(gè)類(lèi)APlayerState:public AInfo 于文件PlayerState.h, 屬于引擎原生文件, 如playername, playerID, starttime 等;
Demo中這個(gè)類(lèi)添加了一些如敵人(這是個(gè)塔防游戲)個(gè)數(shù)(數(shù)組存儲(chǔ)),OnCharPawn(供AI部分代碼調(diào)用spawn出新的敵人); SetGamePaused供blueprint調(diào)用;
小地圖指針也存儲(chǔ)于此類(lèi): TWeakObjectPtr<AStrategyMiniMapCapture> MiniMapCamera;
?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MiniMap部分:
左下角的小地圖有類(lèi): class AStrategyMiniMapCapture : public ASceneCapture2D[該父類(lèi)自帶一個(gè)DrawFrustum的component, 是個(gè)camera]? 增加一些小地圖寬高,AudioListener的FrontDir,RightDir[但在這里沒(méi)用,應(yīng)該只是存儲(chǔ),然后更新立體聲時(shí)候從這里拿]等,以及輔助capture的變量; 在其BeginPlay函數(shù)里面存儲(chǔ)this到GameState中; 根據(jù)tick函數(shù)來(lái)進(jìn)行capture的更新; ASceneCapture2D自帶GetCaptureComponent2D()->UpdateContent()[內(nèi)部實(shí)現(xiàn):{ CaptureSceneDeferred(); }];用于更新自身rendertarget的內(nèi)容;
該類(lèi)具有blueprint實(shí)例, 實(shí)例里面定義了當(dāng)前使用哪個(gè)RenderTarget資源;
至于這個(gè)類(lèi)里面的render target,在這個(gè)demo里面是自定義的成員變量:
在BeginPlay()里面將其給了父類(lèi)中rendertarget:
MiniMapView = NewObject<UTextureRenderTarget2D>();MiniMapView->InitAutoFormat(MiniMapWidth,MiniMapHeight);GetCaptureComponent2D()->TextureTarget = MiniMapView;注意這里雖然是New出來(lái)的,但是沒(méi)有顯式析構(gòu); 其定義是?? UPROPERTY() UTextureRenderTarget2D* MiniMapView; 應(yīng)該是這樣加上屬性令UE4管理其析構(gòu)的;
這里有點(diǎn)懷疑, 不使用額外的自定義的rendertarget應(yīng)該也可以, 而在blueprint里面賦值的也只是這個(gè)TextureTarget, 而不是類(lèi)中新增加的MiniMapView;
如此懷疑: ?blueprint與C++代碼的交互是: 代碼的BeginPlay()先走, 給TextureTarget賦值, 然后讀取blueprint里面的值, 否則blueprint里面的值會(huì)被覆蓋才對(duì);
總感覺(jué)這個(gè)變量沒(méi)什么用, 去掉應(yīng)該也可以… …? test … ….
該類(lèi)內(nèi)部專(zhuān)屬editor代碼,這部分代碼關(guān)聯(lián)著C++與blueprint之間交互的機(jī)制: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該類(lèi)內(nèi)部的GroundLevel定義: capture的camera Z值減去該值即為 [大約]距離地面的高度;
在StrategyPlayerController.Cpp里面發(fā)現(xiàn)代碼用于坐標(biāo)轉(zhuǎn)換, 射線與平面檢測(cè)(比較有用):
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;//獲取屏幕中心點(diǎn) FStrategyHelpers::DeprojectScreenToWorld(ScreenCenterPoint, MyPlayer, RayOrigin, RayDirection);//屏幕中心點(diǎn)坐標(biāo)轉(zhuǎn)換到世界空間,傳出世界空間中的射線始點(diǎn)與方向,其內(nèi)部: 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);//在世界空間進(jìn)行射線與平面檢測(cè)至于這個(gè)類(lèi)及其render target與UI渲染的交互, 繪制到畫(huà)面上: 是在class AStrategyHUD : public AHUD的函數(shù)DrawMiniMap()里面,該類(lèi)重載了很多AHUD的函數(shù),如DrawHUD; 默認(rèn)的HUD可以在gamemode里面進(jìn)行定義;關(guān)于小地圖位置的設(shè)定也是在繪制的時(shí)候?qū)懰赖?#xff0c;繪制:
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和菜單本來(lái)算是同一種實(shí)現(xiàn)方式,但是在本demo中是不一樣的;
?
像上段落提到的,class AStrategyHUD : public AHUD;作為本demo默認(rèn)的HUD類(lèi);
?
如何使用UE4的UMG進(jìn)行UI設(shè)計(jì)? 這種方法可以純粹使用blueprint,而不必使用代碼,只是在按鈕按下之類(lèi)的事件的響應(yīng)函數(shù)中可以利用代碼實(shí)現(xiàn)(為了能夠在blueprint里面在響應(yīng)時(shí)候被調(diào)用,這種專(zhuān)門(mén)后臺(tái)處理UI的類(lèi)應(yīng)該暴露給blueprint,而這個(gè)類(lèi)可以作為一個(gè)成員變量存儲(chǔ)到主角類(lèi)里面,這樣blueprint里面通過(guò)主角來(lái)獲得該類(lèi),進(jìn)而調(diào)用函數(shù));如果一個(gè)C++類(lèi)具有兩個(gè)blueprint實(shí)例,那么這兩個(gè)實(shí)例之間應(yīng)該是沒(méi)關(guān)系的,所以對(duì)于這種專(zhuān)門(mén)后臺(tái)處理UI相應(yīng)事件的邏輯(供blueprint調(diào)用)的類(lèi), 不必具有blueprint實(shí)例; 主要使用的資源類(lèi)型是”Widget Blueprint”, 在這種資源里面進(jìn)行UI設(shè)計(jì),添加相應(yīng)事件,調(diào)用響應(yīng)函數(shù); 游戲整體與這個(gè)資源進(jìn)行交互的機(jī)制是, 一般可以在level blueprint(比如menu是個(gè)專(zhuān)門(mén)的level, 就具有專(zhuān)門(mén)的level blueprint)里面使用”Create Wedget”節(jié)點(diǎn),使用這個(gè)資源(比如做彈出菜單的時(shí)候可以常用這種):
然后將這個(gè)”P(pán)auseMenuReference”作為節(jié)點(diǎn)”Add to Viewport”的target, 將菜單添加到Viewport中;
關(guān)于UI相關(guān)的資源在UE4中有四種:
1. Font: 字體;
本demo中沒(méi)有新建字體;
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里面可以使用節(jié)點(diǎn)“MakeSlateBrush”來(lái)創(chuàng)建一個(gè)SlateBrush;
一個(gè)brush可以包一個(gè)UTexture或者UMaterialInstance,一個(gè)brush應(yīng)該可以理解為,設(shè)定一個(gè)貼圖怎樣繪制(大小,邊緣怎樣處理,對(duì)其方式);
在Widget Blueprint資源里面,如果拖進(jìn)去一個(gè)image的控件,會(huì)發(fā)現(xiàn)屬性里面有Brush一欄,里面的設(shè)定和一個(gè)Slate Brush的設(shè)定是一樣的;
在本demo中,創(chuàng)建了一個(gè)Slate Brush資源,使用的方法是(看起來(lái)是作為備份的默認(rèn)圖片):
const FSlateBrush* SStrategyButtonWidget::GetButtonImage() const {if (ButtonImage.IsValid()){return ButtonImage.Get();} else {return FStrategyStyle::Get().GetBrush("DefaultActionImageBrush");} }可以看到這種slate brush資源“DefaultActionImageBrush”是做后備萬(wàn)一的;
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; 其實(shí)可以完全不使用代碼,僅僅通過(guò)這種資源來(lái)進(jìn)行所有UI的設(shè)定,然后在blueprint里面通過(guò)節(jié)點(diǎn)“”“Create Widget”引用這個(gè)資源,然后“Add to Viewport”即可;只是本demo全是C++代碼寫(xiě)的,類(lèi)似于早期的純代碼寫(xiě)windows app的UI,而不是利用MFC拖控件的方式;
?
?
- 自定義Widget:
Widget可以理解為在UE4中即指代UI控件,本demo沒(méi)有使用Widget Blueprint資源來(lái)涉及UI,所有都是代碼寫(xiě)的,創(chuàng)建了很多繼承自ScompoundWidget的類(lèi),作為一種新的自定義的Widget來(lái)作為UI的最小單元控件(后被用于組合成一個(gè)style);
如:class SStrategyButtonWidget : public SCompoundWidget : public SWidget
自定義Widget內(nèi)部具體內(nèi)容(如點(diǎn)擊時(shí)的事件回調(diào)函數(shù),button上的text等)需要以SLATE_BEGIN_ARGS(SStrategyButtonWidget)形式開(kāi)始,以SLATE_END_ARGS()形式結(jié)束,內(nèi)容例如:
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)//這個(gè)是自定義的新事件并自動(dòng)被delegate,其實(shí)在SWidget里面有OnMouseButtonDown這樣的虛函數(shù)已經(jīng)托管好了,重載即可,這里拿來(lái)理解怎樣新增自己事件以及綁定好回調(diào)函數(shù)/** text on the button */SLATE_ATTRIBUTE(FText, ButtonText)SLATE_END_ARGS()又如:class SStrategyMiniMapWidget : public SCompoundWidget : public SWidget
這個(gè)是自定義小地圖使用的widget,這部分和小地圖的那個(gè)rendertarget渲染部分和事件響應(yīng)(鼠標(biāo)在小地圖上點(diǎn)擊,移動(dòng))是有關(guān)系的,該類(lèi)還重載了OnPaint(…)函數(shù), 這個(gè)函數(shù)內(nèi)部只是繪制小地圖上的白線的,通過(guò)FSlateDrawElement::MakeLines(…)函數(shù);
至于小地圖上的那個(gè)rendertarget的繪制,小地圖部分已經(jīng)提及;
?
一點(diǎn)比較有用的代碼:
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,但是對(duì)于editor而言是沒(méi)用的,也沒(méi)有對(duì)應(yīng)的資源之類(lèi)的東西,只是邏輯代碼上的東西;這些自定義的widget應(yīng)該被用于后面實(shí)現(xiàn)class SStrategySlateHUDWidget : public SCompoundWidget;
自定義的widget在其構(gòu)造函數(shù)中會(huì)初始化該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實(shí)現(xiàn)class SStrategySlateHUDWidget : public SCompoundWidget的時(shí)候, 其內(nèi)部構(gòu)造函數(shù)就用到了之前實(shí)現(xiàn)的幾個(gè)別的Widget,也就是說(shuō),像class SStrategyMiniMapWidget,class SStrategyButtonWidget這樣的widget都是為class SStrategySlateHUDWidget服務(wù)的,雖然他們都是Widget,并且具有相同的父子繼承關(guān)系;
所以在void SStrategySlateHUDWidget::Construct(const FArguments& InArgs)里面有:
SNew(SCanvas) SNew(SBorder) SAssignNew(ActionButtonsWidget,SStrategyActionGrid) SAssignNew(MiniMapWidget,SStrategyMiniMapWidget) SNew(SImage) SAssignNew(PauseMenuButtons[ButtonIndex++], SStrategyButtonWidget)即是說(shuō),SStrategySlateHUDWidget 類(lèi)把之前的小地圖自定義widget,button自定義widget等widget組合了起來(lái)(指針成為其成員變量),它使用了它們;
另外在它重載的Tick函數(shù)里面有兩行代碼比較好用:
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的這個(gè)SStrategySlateHUDWidget又被class AStrategyHUD : public AHUD使用,在其BuildMenuWidgets(…)函數(shù)里面被SAssignNew出來(lái):
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的分析思路有誤,應(yīng)該從默認(rèn)HUD類(lèi)class AStrategyHUD : public AHUD分析起,會(huì)發(fā)現(xiàn)其使用了自定義widget SStrategySlateHUDWidget, 然后進(jìn)入到SStrategySlateHUDWidget的構(gòu)造函數(shù)里面會(huì)發(fā)現(xiàn)其又組合里其他自定義widget;這樣自頂向下的分析會(huì)更有效率更清晰; 以上的分析是自下至上的,容易糊涂;不過(guò)自頂而下的分析需要知道頂在哪里;】
?
- 自定義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類(lèi)似是個(gè)容器類(lèi),它組合了之前的自定義widget,
這里style的基類(lèi):struct SLATECORE_API FSlateWidgetStyle,僅僅是個(gè)struct;
struct FStrategyHUDStyle : public FSlateWidgetStyle, 該類(lèi)作為容器內(nèi)部包含了FSlateBrush(可看出帶配置的texture),FSlateColor等,然后定義一個(gè)class UStrategyHUDWidgetStyle : public USlateWidgetStyleContainerBase,是正規(guī)的類(lèi),包含一個(gè)FStrategyHUDStyle類(lèi)型,這樣blueprint就可以對(duì)其賦值了;
然后editor中創(chuàng)建這個(gè)類(lèi)對(duì)應(yīng)的style資源,然后再在某個(gè)構(gòu)造函數(shù)(void SStrategyMenuWidget::Construct(const FArguments& InArgs))中查找使用它:
MenuStyle = &FStrategyStyle::Get().GetWidgetStyle<FStrategyMenuStyle>("DefaultStrategyMenuStyle");//注意這里是按照名字查找相關(guān)style的,也就是說(shuō),所有的widget style都應(yīng)該放到一個(gè)map中,查找代碼發(fā)現(xiàn)(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;會(huì)把所有的slate widget style收集起來(lái); #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還有一個(gè)hud:class AStrategyMenuHUD : public AHUD,按照之前的分析,前者主要是小地圖,右上角的三個(gè)代表生命個(gè)數(shù)的木桶,后者主要是菜單hud;菜單HUD;editor中定義的是前者AStrategyHUD;
菜單除了具有單獨(dú)的HUD,菜單還有單獨(dú)的game mode和controller:
AStrategyMenuGameMode::AStrategyMenuGameMode(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {//setup our custom PC and HUDPlayerControllerClass = AStrategyMenuPlayerController::StaticClass();HUDClass = AStrategyMenuHUD::StaticClass();SpectatorClass = AStrategySpectatorPawn::StaticClass(); }這個(gè)menu gamemode好像沒(méi)有顯式使用的地方,也沒(méi)有對(duì)應(yīng)的資源;這個(gè)Menu HUD的一個(gè)指針引用保存在了class SStrategyMenuWidget : public SCompoundWidget里面;
?
這個(gè)menu部分,好像一直是獨(dú)立出來(lái)的一塊似的;它的HUD,game mode,widget, PlayerController似乎都是自己和自己在用,沒(méi)找到別的地方有使用它的;?????這是個(gè)沒(méi)有完成的demo???
這個(gè)demo中的兩個(gè)地圖,兩個(gè)game mode,他們是怎樣工作的?難道是一個(gè)地圖就可以有一個(gè)game mode?
發(fā)現(xiàn),當(dāng)游戲啟動(dòng)時(shí)候,會(huì)兩個(gè)game mode的構(gòu)造函數(shù)都會(huì)逐一進(jìn)入,先進(jìn)入的是AStrategyGameMode然后是AStrategyMenuGameMode的;
在DefaultEngine.ini里面:
GameDefaultMap=/Game/Maps/StrategyMenu ServerDefaultMap=/Game/Maps/StrategyMenu EditorStartupMap=/Game/Maps/TowerDefenseMap GlobalDefaultGameMode="/Script/StrategyGame.StrategyGameMode"這樣的設(shè)定應(yīng)該決定了游戲剛起來(lái)的時(shí)候默認(rèn)地圖是StrategyMenu,所以在游戲開(kāi)時(shí)候回有LoadMap(…)調(diào)用來(lái)加載地圖StrategyMenu,然后在LoadMap函數(shù)內(nèi)部調(diào)用SetGameMode(…):
WorldContext.World()->SetGameMode(URL);//看來(lái)似乎一個(gè)地圖一個(gè)world就有一個(gè)game mode設(shè)定 bool UWorld::SetGameMode(const FURL& InURL) {if( IsServer() && !AuthorityGameMode ){AuthorityGameMode = GetGameInstance()->CreateGameModeForURL(InURL);//這個(gè)URL里面包含了地圖名StrategyMenu,此時(shí)正在加載這個(gè)地圖,按這個(gè)函數(shù)名來(lái)看,似乎會(huì)依賴loadmap的地圖信息弄出來(lái)個(gè)game modeif( AuthorityGameMode != NULL ){return true;}else{UE_LOG(LogWorld, Error, TEXT("Failed to spawn GameMode actor."));return false;}}return false; }打開(kāi)地圖可以發(fā)現(xiàn),對(duì)于主要的游戲地圖TowerDefenseMap,它的World Setting里面使用的GameModeOverride是“StrategyGameMode”,而StrategyMenu地圖里面的GameOverride是“StrategyMenuGameMenu”,所以知道了:整個(gè)游戲可以有自己的game mode,各個(gè)地圖可以override自己的game mode,當(dāng)然也就會(huì)有自己獨(dú)特專(zhuān)屬的Controller,HUD等等:
AStrategyMenuGameMode::AStrategyMenuGameMode(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {//setup our custom PC and HUDPlayerControllerClass = AStrategyMenuPlayerController::StaticClass();HUDClass = AStrategyMenuHUD::StaticClass();SpectatorClass = AStrategySpectatorPawn::StaticClass(); }所以在地圖StrategyMenu里面看到什么都沒(méi)有,它的作用只是在它的world setting里面設(shè)定override的專(zhuān)屬game mode,然后這個(gè)專(zhuān)屬的game mode AStrategyMenuGameMode的構(gòu)造函數(shù)里面會(huì)有設(shè)定專(zhuān)屬的Controller和HUD以及SpectatorClass(這個(gè)就是主角類(lèi),玩家控制的),而這個(gè)StrategyMenu在project setting里面里面設(shè)定的是作為Startup map,所以游戲啟動(dòng)后,加載這個(gè)菜單地圖,然后其構(gòu)造函數(shù)指定這個(gè)菜單地圖的HUD,controller,SpectatorClass;
對(duì)應(yīng)菜單專(zhuān)屬的controller AStrategyMenuPlayerController,沒(méi)有做特殊時(shí)期,主要是SetupInputComponent()函數(shù)的重載實(shí)現(xiàn), 并且沒(méi)有特殊的實(shí)現(xiàn),用的是PlayerController.CPP的;
?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Menu部分:
class SStrategyMenuWidget : public SCompoundWidget 這個(gè)是menu的widget,其構(gòu)造函數(shù)中找到menu的style:MenuStyle = &FStrategyStyle::Get().GetWidgetStyle<FStrategyMenuStyle> ("DefaultStrategyMenuStyle"); 這style就有點(diǎn)像一個(gè)容器內(nèi)部包含了很多菜單所需要的texture(類(lèi)型FStrategyMenuStyle包含了SlateBrush,SlateSound等,然后editor中有這個(gè)類(lèi)對(duì)應(yīng)的blueprint資源,這個(gè)資源名字就是“DefaultStrategyMenuStyle”),然后在SStrategyMenuWidget的構(gòu)造函數(shù)中通過(guò)MenuStyle找到這些texture,然后構(gòu)建菜單按鈕貼圖等的布局;
在AStrategyMenuHUD的構(gòu)造函數(shù)里面,直接通過(guò)代碼方式增加子菜單的四個(gè)按鈕“Easy”“Normal”“Hard”“Back”;在SStrategyMenuWidget有各個(gè)按鈕的回調(diào)函數(shù);這樣菜單就啟動(dòng)起來(lái)了;
?
關(guān)于菜單間的切換進(jìn)入游戲部分:
當(dāng)點(diǎn)擊easy等游戲難度選擇按鈕后,會(huì)調(diào)用到回調(diào)函數(shù):
void AStrategyMenuHUD::LaunchGame() {FString StartStr = FString::Printf(TEXT("/Game/Maps/TowerDefenseMap?%s=%d"), *AStrategyGameMode::DifficultyOptionName, (uint8) Difficulty);GetWorld()->ServerTravel(StartStr);//這里準(zhǔn)備加載游戲主地圖ShowLoadingScreen();//但是主地圖不會(huì)馬上加載并顯示進(jìn)來(lái),所以需要一個(gè)loading頁(yè)面,按照之前的分析,這個(gè)loading screen是在另外一個(gè)module里面,另外的module也不一定是同一個(gè)線程 }對(duì)于函數(shù)ShowLoadingScreen():
void AStrategyMenuHUD::ShowLoadingScreen() {IStrategyGameLoadingScreenModule* LoadingScreenModule = FModuleManager::LoadModulePtr<IStrategyGameLoadingScreenModule>("StrategyGameLoadingScreen");//這里就載入了新的module,進(jìn)入加載游戲的那個(gè)頁(yè)面if( LoadingScreenModule != nullptr ){LoadingScreenModule->StartInGameLoadingScreen();} }這里看出,UE4在切換的時(shí)候,使用GetWorld()->ServerTravel(StartStr);這樣的函數(shù),使得游戲轉(zhuǎn)入下一個(gè)地圖即游戲主地圖“TowerDefenseMap”,同理,在載入地圖的時(shí)候會(huì)加載override的gamemode “AStrategyGameMode”,其內(nèi)部構(gòu)造函數(shù)設(shè)定使用的controller,state,HUD,controller,DefaultPawnClass,就把各個(gè)類(lèi)結(jié)合了起來(lái),隨后比如定義在state(類(lèi)AStrategyGameState)里面的諸如重載的InitGameState這樣的函數(shù)就會(huì)開(kāi)始走,初始化游戲狀態(tài)相關(guān)內(nèi)容;
?
點(diǎn)擊建筑,建筑上會(huì)出現(xiàn)即時(shí)彈出菜單,都是定義在StrategyBuilding文件里,彈出菜單存儲(chǔ)在ASrategyHUD里面,只是顯示隱藏,控件點(diǎn)擊的觸發(fā)回調(diào)都是一樣的;
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AI部分:
NPC行走路線的定義:
NPC自動(dòng)轉(zhuǎn)身走動(dòng):
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); 如此這樣聲明一個(gè)事件,然后比如” FOnMovementEvent”就成為一種類(lèi)型,可以聲明變量;FOnMovementEvent MovementDelegate;MovementDelegate.BindUObject(this, &UStrategyAIAction_MoveToBrewery::OnMoveCompleted);MyAIController->RegisterMovementEventDelegate(MovementDelegate);?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Gameplay部分:
在文件DefaultGame.ini里面:
[/Script/StrategyGame.StrategyGameMode] TimeBeforeReturnToMenu=3 [/Script/StrategyGame.StrategyGameState] WarmupTime=3然后找到其對(duì)應(yīng)的代碼變量(StrategyGameMode.h):
/** Time before game returns to menu after finish. */UPROPERTY(config)int32 TimeBeforeReturnToMenu;/** Warm up time before game starts */UPROPERTY(config)int32 WarmupTime;可以知道如果想要讀取這個(gè)ini里面的變量,只需要在聲明變量的時(shí)候加上config關(guān)鍵字即可;
所以在游戲主界面的游戲啟動(dòng)前(點(diǎn)擊了選擇難度按鈕后)的暖場(chǎng)時(shí)間,這個(gè)所謂的暖場(chǎng)時(shí)間只是主游戲界面左上角的倒計(jì)時(shí),跟加載頁(yè)面時(shí)間沒(méi)關(guān)系,邏輯是(StrategyGameState.cpp):
void AStrategyGameState::StartGameplayStateMachine() {if (WarmupTime > 0.f){SetGameplayState(EGameplayState::Waiting);GetWorldTimerManager().SetTimer(TimerHandle_OnGameStart, this, &AStrategyGameState::OnGameStart, WarmupTime, false);}else{OnGameStart();} }關(guān)于這個(gè)啟動(dòng)游戲前的暖場(chǎng)時(shí)間和機(jī)制:
點(diǎn)擊easy難度頁(yè)面后調(diào)用:
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函數(shù)加載主游戲地圖,初始化state,在初始化state時(shí)候有調(diào)用: void AStrategyGameState::StartGameplayStateMachine() {if (WarmupTime > 0.f){SetGameplayState(EGameplayState::Waiting);GetWorldTimerManager().SetTimer(TimerHandle_OnGameStart, this, &AStrategyGameState::OnGameStart, WarmupTime, false);//這里設(shè)定的不是loading頁(yè)面的顯示時(shí)間,只是游戲正式開(kāi)始的倒計(jì)時(shí),其實(shí)是一個(gè)等待時(shí)間,時(shí)間到了,就調(diào)用這里設(shè)定的回調(diào)函數(shù)開(kāi)始游戲 }else{OnGameStart();} }ShowLoadingScreen函數(shù)加載額外的那個(gè)screen loading的module,這個(gè)module不一定是有單獨(dú)線程在走的,這個(gè)demo似乎是沒(méi)有:
這個(gè)加載頁(yè)面,它的具體實(shí)現(xiàn)是(StrategyGameLoadingScreen.cpp):
virtual void CreateScreen(){FLoadingScreenAttributes LoadingScreen;LoadingScreen.bAutoCompleteWhenLoadingCompletes = true;LoadingScreen.MinimumLoadingScreenDisplayTime = 0.f;//這個(gè)值是可以改變等待時(shí)間的,單位是秒LoadingScreen.WidgetLoadingScreen = SNew(SStrategyLoadingScreen);GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);}按照這里的設(shè)定,是加載完成就自動(dòng)退出,怎樣才算是加載完成呢?它能自動(dòng)監(jiān)視游戲的state嗎?主要邏輯部分代碼在DefaultGameMoviePlayer.cpp里面;
?
?
這里的主視角的camera是class UStrategyCameraComponent : public UCameraComponent作為成員變量存儲(chǔ)在StrategySpectatorPawn里面的,AStrategySpectatorPawn重載了MoveForward之類(lèi)的函數(shù)來(lái)控制camera動(dòng);引擎怎么知道這個(gè)camera是主視角的camera?
發(fā)現(xiàn)當(dāng)我們觸發(fā)一個(gè)debug camera自由移動(dòng)場(chǎng)景時(shí)候(ToggleDebugCamera命令行輸入),void ADebugCameraController::OnActivate( APlayerController* OriginalPC )會(huì)進(jìn)入,會(huì)有:
float const OrigCamFOV = OriginalPC->PlayerCameraManager->GetFOVAngle();可以看出,主視角camera是存儲(chǔ)在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;對(duì)于菜單的那個(gè)player controller是這么對(duì)其賦值的:
AStrategyMenuPlayerController::AStrategyMenuPlayerController(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {// just use the normal camera...fine for nowPlayerCameraManagerClass = APlayerCameraManager::StaticClass(); }APlayerCameraManager 雖然不包括camera component,但是其具有camera的必要信息;
在加載進(jìn)入主游戲地圖時(shí)候,會(huì)先spawn player controller,在spawn player controller的過(guò)程中會(huì)進(jìn)行SpawnPlayerCameraManager()會(huì)有一些信息存儲(chǔ)到APlayerCameraManager里面;
對(duì)于這個(gè)游戲demo而言,主視角只有平移操作,就是在平移ASpectatorPawn主角類(lèi)對(duì)象,因?yàn)閏amera是它的子component,所以位置也變了,這樣主視角變化;
而對(duì)于縮放整個(gè)level場(chǎng)景,實(shí)現(xiàn)方法的調(diào)用堆棧是:
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);//縮放整個(gè)場(chǎng)景主要改變ZoomAlpha這個(gè)值實(shí)現(xiàn)的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部分:
類(lèi)型可以是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);關(guān)于UCheatManager類(lèi),有定義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還提供了一個(gè)專(zhuān)門(mén)的類(lèi)ADebugCameraHUD可供參考,在文件DebugCameraHUD.CPP里面;我們要實(shí)現(xiàn)的時(shí)候可以繼承他;
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();?
?
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
其他:
- 場(chǎng)景中的燈光都要被包含在一個(gè)LightmassImportanceVolume里面;
- 關(guān)于碰撞檢測(cè), 對(duì)于整個(gè)地面的那個(gè)static mesh actor:
而對(duì)于其他的actor, 則Collision Presets的設(shè)定是NoCollision, 比如墻,石頭,木桶,都是可以穿過(guò)去的,為了防止此點(diǎn), 場(chǎng)景中增加了大量的block volume; 利用block volume比物理actor之間檢測(cè)碰撞應(yīng)該更有效率;
- 武器attach到(NPC)角色身上的機(jī)制:
這種設(shè)定,使得在level blueprint里面設(shè)定的一個(gè)weapons(暫時(shí)先存儲(chǔ)到UStrategyAIDirector : public UActorComponent 類(lèi)里面, 這種繼承關(guān)系應(yīng)該只是為了tick),然后就被應(yīng)用到了所有的NPC, 其實(shí)并不很好,只是這里的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 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/DeanWang/p/6222865.html
總結(jié)
以上是生活随笔為你收集整理的《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jquery.nicescroll参数说
- 下一篇: 地磁室内导航定位