{"id":2411,"date":"2024-07-20T02:19:27","date_gmt":"2024-07-19T18:19:27","guid":{"rendered":"https:\/\/courtship.top\/?p=2411"},"modified":"2024-07-20T02:19:27","modified_gmt":"2024-07-19T18:19:27","slug":"ue%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e6%95%b4%e7%90%86","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2024\/07\/20\/ue%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e6%95%b4%e7%90%86\/","title":{"rendered":"UE\u667a\u80fd\u6307\u9488\u6574\u7406"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0\u539f\u7406<\/h2>\n\n\n\n<p>\u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0\u539f\u7406\u4e00\u4e2a\u662f<strong>\u5f15\u7528\u8ba1\u6570<\/strong>\uff0c\u4e00\u4e2a\u662f<strong>\u5229\u7528\u8d85\u51fa\u4f5c\u7528\u57df\u81ea\u52a8\u6790\u6784\u539f\u7406<\/strong>\u3002<\/p>\n\n\n\n<p>\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0c\u91ca\u653e\u5185\u5b58\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TWeakPtr<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u7ec6\u8282<\/h3>\n\n\n\n<p>UE\u5f88\u5de7\u5999\u7684\u64b8\u4e86\u4e2a WeakObjectPtr \uff0c\u5b9e\u73b0\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5b83\u7684\u6838\u5fc3\u5c31\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/WeakObjectPtr.h\nint32\tObjectIndex;\nint32\tObjectSerialNumber;\n\n\/\/WeakObjectPtr.cpp\nObjectIndex = GUObjectArray.ObjectToIndex((UObjectBase*)Object);\nObjectSerialNumber = GUObjectArray.AllocateSerialNumber(ObjectIndex);<\/code><\/pre>\n\n\n\n<p>ObjectIndex \u5c31\u662f\u5f53\u524dWeakObjectPtr \u6307\u5411\u7684\u5bf9\u8c61\u5728\u6570\u7ec4GUObjectArray\u5185\u7684\u7d22\u5f15\uff0c\u800cObjectSerialNumber \u5219\u662f\u8be5\u5bf9\u8c61\u5f53\u521d\u5728\u521b\u5efa\u7684\u65f6\u5019\u7684\u552f\u4e00\u5e8f\u5217\u7801 SerialNumber \uff08\u8d4b\u503c\u548c\u81ea\u589e\u90fd\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u7ebf\u7a0b\u5b89\u5168\uff09\uff1b\u56e0\u4e3a\u5b83\u4e0d\u76f4\u63a5\u5f15\u7528 Obj \u6307\u9488 \uff0c\u56e0\u6b64\u4e0d\u4f1a\u5f71\u54cd GC\uff0c\u7136\u540e ObjectSerialNumber \u4f1a\u8ddf GUObjectArray \u5185 ObjectIndex \u4f4d\u7f6e\u5bf9\u8c61\u7684 SerialNumber \u5bf9\u6bd4\uff0c\u4e0d\u4e00\u6837\u5c31\u8868\u793a\u6839\u672c\u4e0d\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e94\u7528\u7ec6\u8282<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u907f\u514dcrash<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u4e00\u4e9b\u4f1a\u9694\u5e27\u6267\u884c\u7684\u4ee3\u7801\uff08\u5b9a\u65f6\u5668\u56de\u8c03\u3001 \u59d4\u6258\u7b49\uff09\uff0c\u5982\u679c\u6709\u7528\u5230 UObject \u5bf9\u8c61\uff0c\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528 WeakObjectPtr\uff0c\u80fd\u51cf\u5c11\u5f88\u591a\u9690\u85cf\u7684 Crash\uff0c\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u4e50\u767e\u6c0f( Robust )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5faa\u73af\u5f15\u7528<\/h3>\n\n\n\n<p>\u7531\u4e8eTWeakPtr\u4e0d\u53c2\u4e0e\u5f15\u7528\u8ba1\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u6765\u89e3\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002\u5faa\u73af\u5f15\u7528\u662f\u6307\u5bf9\u8c61A\u4e2d\u5b58\u5728B\u7684\u5f15\u7528\uff0c\u5bf9\u8c61B\u4e2d\u5b58\u5728A\u7684\u5f15\u7528\u3002\u5982\u56fe\u6240\u793a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"334\" src=\"https:\/\/courtship.top\/wp-content\/uploads\/2024\/07\/1721350947-v2-35d5bc66946c4397463f5901f142f237_720w.webp\" alt=\"\" class=\"wp-image-2413\" srcset=\"https:\/\/courtship.top\/wp-content\/uploads\/2024\/07\/1721350947-v2-35d5bc66946c4397463f5901f142f237_720w.webp 844w, https:\/\/courtship.top\/wp-content\/uploads\/2024\/07\/1721350947-v2-35d5bc66946c4397463f5901f142f237_720w-300x119.webp 300w, https:\/\/courtship.top\/wp-content\/uploads\/2024\/07\/1721350947-v2-35d5bc66946c4397463f5901f142f237_720w-768x304.webp 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/figure>\n\n\n\n<p>\u56fe\u4e2d\u5185\u5b58A\u548c\u5185\u5b58B\u5404\u88ab\u5f15\u75282\u6b21\uff0c\u56e0\u6b64\u5f15\u7528\u8ba1\u6570\u4e3a2\u3002\u5f53\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\uff0c\u6307\u9488A\u548c\u6307\u9488B\u6790\u6784\uff0c\u56e0\u6b64\u5f15\u7528\u8ba1\u6570\u5404\u51cf1\uff0c\u6b64\u65f6\u5f15\u7528\u8ba1\u6570\u4e3a1\u4e0d\u4e3a0\uff0c\u56e0\u6b64\u5185\u5b58A\u548c\u5185\u5b58B\u65e0\u6cd5\u91ca\u653e\uff0c\u8fd9\u5c31\u662f\u5faa\u73af\u5f15\u7528\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<strong>\u89e3\u51b3\u65b9\u5f0f\u5c31\u662f\u5bf9\u8c61A\u4e2d\u4f7f\u7528\u5f31\u6307\u9488\u6307\u5411B\uff0c\u5bf9\u8c61B\u4e2d\u4f7f\u7528\u5f31\u6307\u9488\u6307\u5411A\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TSharedPtr<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u6742\u9879<\/h2>\n\n\n\n<p>TWeakObjectPtr\u4e0eTWeakPtr\u7684\u533a\u522b\u662f\uff1aTWeakObjectPtr\u7528\u4e8e\u6307\u5411UObject\u5bf9\u8c61\uff0c\u800cTWeakPtr\u7528\u4e8e\u6307\u5411\u5e38\u89c4C++\u5bf9\u8c61\u3002TWeakObjectPtr\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5df2\u9500\u6bc1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WeakObjectPtr&lt;UActorComponent&gt; ptr = MakeWeakObjectPtr(TestUObj);\/\/\u5f31\u5bf9\u8c61\u6307\u9488\nTestUObj-&gt;DestroyComponent();\/\/\u9500\u6bc1\n\nif (ptr.IsValid())\n{\n    UE_LOG(LogTemp, Log, TEXT(\"Not Destroy!\"));\n}\nelse\n{\n    UE_LOG(LogTemp, Log, TEXT(\"Destroyed!\"));\/\/\u6b63\u5e38\u4e86\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u73b0\u539f\u7406 \u667a\u80fd\u6307\u9488\u7684\u5b9e\u73b0\u539f\u7406\u4e00\u4e2a\u662f\u5f15\u7528\u8ba1\u6570\uff0c\u4e00\u4e2a\u662f\u5229\u7528\u8d85\u51fa\u4f5c\u7528\u57df\u81ea\u52a8\u6790\u6784\u539f\u7406\u3002 \u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0c\u91ca\u653e\u5185\u5b58\u3002 TWeakPtr \u5b9e &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22,12,1],"tags":[],"class_list":["post-2411","post","type-post","status-publish","format-standard","hentry","category-c","category-ue","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2411"}],"version-history":[{"count":6,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2411\/revisions"}],"predecessor-version":[{"id":2418,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2411\/revisions\/2418"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}