[{"data":1,"prerenderedAt":127},["ShallowReactive",2],{"\u002F2019-10-31-ue4-singleton-note":3},{"id":4,"title":5,"body":6,"date":104,"description":12,"extension":105,"meta":106,"navigation":109,"path":121,"seo":122,"stem":123,"tags":124,"__hash__":126},"blogs\u002F_legacy\u002F2019\u002F2019-10-31-ue4-singleton-note.md","UE4单例相关",{"type":7,"value":8,"toc":98},"minimark",[9,13,16,20,23,31,37,47,50,53,64,70,73,95],[10,11,12],"p",{},"有很多关于设计模式的书都不建议使用单例，但是实际在很多地方还是会用到。",[10,14,15],{},"因为单例实在是太方便了，而且很“干净”。",[17,18,19],"h2",{"id":19},"编辑器中使用",[10,21,22],{},"在UE4的编辑器中使用单例最大的问题是，默认的调试模式下，所有的PIE共用一个。",[10,24,25,26,30],{},"如果要进行DS的调试的话，很容易出现一些没有预料的问题。这个时候可以选择使用",[27,28,29],"code",{},"GEditorID","来分离。",[10,32,33,34,36],{},"由于",[27,35,29],{},"的特殊性质，可以实现一个奇怪的Trick，能够在不使用RPC的情况下直接在编辑器对服务器上的位置信息进行调试绘制：",[38,39,44],"pre",{"className":40,"code":42,"language":43},[41],"language-text","#if !UE_BUILD_SHIPPING \nif(IS_CLIENT) \n{ \n  UWorld* tp_World = GetWorld(); \n  if (tp_World) \n  { \n    DrawDebugSphere(tp_World, AActor::GetActorLocation(), 30.0f, 7, FColor::Green, false, 4.0f);\n    DrawDebugSphere(tp_World, ReplicatedMovement.Location, 35.0f, 7, FColor::Blue, false, 4.0f); \n  } \n} \n#if WITH_EDITOR \nif (IS_SERVER) \n{ \n    UWorld* tp_World = GEditor->GetWorldContextFromPIEInstance(2)->World(); \n    DrawDebugSphere(tp_World, AActor::GetActorLocation(), 40.0f, 7, FColor::Red, false, 4.0f); \n} \n#endif\n\n#endif\n","text",[27,45,42],{"__ignoreMap":46},"",[10,48,49],{},"如果编译没过的话，注意检查是否引用了UnrealEd这个Module。",[17,51,52],{"id":52},"新版本提供的单例类",[10,54,55,56,63],{},"之前的引擎版本中要使用与引擎相关的单例实现总会有各种各样的麻烦，从UE4.22开始，引擎提供了一组新的类型，包括",[57,58,62],"a",{"href":59,"rel":60},"https:\u002F\u002Fdocs.unrealengine.com\u002Fen-US\u002FAPI\u002FRuntime\u002FEngine\u002FSubsystems\u002FUGameInstanceSubsystem\u002Findex.html",[61],"nofollow","UGameInstanceSubsystem","等。可以方便的在引擎内部实现单例的功能，实际进行测试的话：",[38,65,68],{"className":66,"code":67,"language":43},[41],"void UMyGameInstanceSubsystem::RefreshAndPrint() \n{ \n  int32 td_LastId = MyGameId; \n  MyGameId = GPlayInEditorID; \n  UE_LOG(LogTemp, Log, TEXT(\" UMyGameInstanceSubsystem::RefreshAndPrint: %d->%d || %p\"), td_LastId, MyGameId, this); \n}\n",[27,69,67],{"__ignoreMap":46},[10,71,72],{},"输出的结果如下：",[74,75,76,79,81,84,86,89,91,93],"blockquote",{},[10,77,78],{},"LogTemp: UMyGameInstanceSubsystem::RefreshAndPrint: 2->2 || 00000184F8152AC0",[10,80,78],{},[10,82,83],{},"LogTemp: UMyGameInstanceSubsystem::RefreshAndPrint: 3->3 || 00000184F8152570",[10,85,83],{},[10,87,88],{},"LogTemp: UMyGameInstanceSubsystem::RefreshAndPrint: 1->1 || 00000184F8153FB0",[10,90,88],{},[10,92,78],{},[10,94,78],{},[10,96,97],{},"这样的话就可以不用顾及一些其他的细节问题了。",{"title":46,"searchDepth":99,"depth":100,"links":101},2,3,[102,103],{"id":19,"depth":99,"text":19},{"id":52,"depth":99,"text":52},"2019-10-31","md",{"layout":107,"status":108,"published":109,"author":110,"author_login":112,"author_email":113,"wordpress_id":114,"wordpress_url":115,"date_gmt":116,"excerpt":117},"post","publish",true,{"display_name":111,"login":112,"email":113,"url":46},"风铃","flinkor","flinkor@foxmail.com",2712,"\u002F?p=2712","2019-10-31 07:40:14 +0000",{"type":7,"value":118},[119],[10,120,12],{},"\u002F2019-10-31-ue4-singleton-note",{"title":5,"description":12},"_legacy\u002F2019\u002F2019-10-31-ue4-singleton-note",[125],"UE4","fxXaOz18H1iEySDeB5DX_qBNCeagnDfDL6AfYsCuJ9Q",1788763179061]