UE4 C++源碼 在Visual studio 2015中使用過程發現添加 try catch 之后打包通不過。追溯錯誤提示是C++的 try catch 代碼。網上查找之后發現
https://blog.csdn.net/SC32024826/article/details/78710672
public class XXXXXX: TargetRules
{
public XXXXX(TargetInfo Target) : base(Target)
{
……
Target.bForceEnableExceptions = true;
}
然后沒有報錯能生成文件。后續再觀察。using UnrealBuildTool; using System.Collections.Generic; public class MyModule : ModuleRules { public MyModule(ReadOnlyTargetRules Target) : base(Target) { // Settings go here } }
這樣的結構可以直接使用
Target.bEnableExceptions = true;
但是我使用的代碼層次是
using UnrealBuildTool; using System.Collections.Generic; public class XXXTarget : TargetRules { public XXXTarget(TargetInfo Target) : base(Target) { Type = TargetType.Game; ExtraModuleNames.Add("szmUEDesign"); } }
並沒有ReadOnlyTargetRules。(搜索到這邊其實應該解決問題了,可是自己忽略重點繼續花時間搜索)
后面直接在源代碼搜索TargetInfo 。 發現TargetInfo 的成員變量都跟異常不相關。但是,在它的成員變量在的文件"UeBulidTarget.cs"能搜索到bEnableExceptions 。發現 bEnableExceptions出現在以下環境
GlobalCompileEnvironment.bEnableExceptions = Rules.bForceEnableExceptions || Rules.bBuildEditor;
此處的Rules定義如下
public ReadOnlyTargetRules Rules;
追述ReadOnlyTargetRules 到TargetRulers.cs
public partial class ReadOnlyTargetRules { …… /// <summary> /// The writeable TargetRules instance /// </summary> TargetRules Inner; …… public bool bForceEnableExceptions { get { return Inner.bForceEnableExceptions; } } …… } TargetRules定義 public abstract class TargetRules { …… /// <summary> /// Enable exceptions for all modules. /// </summary> [RequiresUniqueBuildEnvironment] public bool bForceEnableExceptions = false; }
到這邊就明白調用規則了。 直接在自己的bulid文件增加以下代碼,啟用異常檢測機制。
using UnrealBuildTool; using System.Collections.Generic; public XXXTarget : TargetRules { public XXXTarget(TargetInfo Target) : base(Target) { Type = TargetType.Game; ExtraModuleNames.Add("szmUEDesign"); this.bForceEnableExceptions = true; } }
4.18(不含)之前在 bulid.cs文件添加 UEBuildConfiguration.bForceEnableExceptions = true;或者 UEBuildConfiguration.bEnableExceptions = true; 。 UE4.18之后使用 this.bForceEnableExceptions = true;解決異常
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2019-04-02
問題:代碼添加 this.bForceEnableExceptions = true; UE4.19編輯器環境可以運行,但是打包失敗。
解答:該問題是因為使用ue4官方下載生成好的環境。 當調用 this.bForceEnableExceptions 時,因為和編輯好的環境沖突,所以失敗。 解決的辦法是下載UE4官方源代碼,然后建立sln文件,本地重新生成。 然后項目引用生成后的ue4引擎。這時候打包和運行都正常了。