{"id":279,"date":"2025-07-13T22:21:30","date_gmt":"2025-07-13T22:21:30","guid":{"rendered":"https:\/\/www.thegames.dev\/?p=279"},"modified":"2025-07-13T22:26:38","modified_gmt":"2025-07-13T22:26:38","slug":"enforce-tobjectptr-and-how-to-avoid-pitfalls","status":"publish","type":"post","link":"https:\/\/www.thegames.dev\/?p=279","title":{"rendered":"Enforce TObjectPtr! (And how to avoid pitfalls)"},"content":{"rendered":"\n<p>As on Unreal Engine 5, TObjectPtr is the new way for classes to hold raw pointers.<br>Pre UE5 you did<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nUPROPERTY(EditAnywhere)\nUMyObject* MyObject;\n<\/pre><\/div>\n\n\n<p>Now with UE5 you should do:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nUPROPERTY(EditAnywhere)\nTObjectPtr&lt;UMyObject&gt; MyObject;\n<\/pre><\/div>\n\n\n<p>To enforce these, i highly recommend placing the following in your Target.cs file for your project (we normally just do it in the MyGameEditor.Target.cs<br><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\n\t\tif (!bBuildAllModules)\n\t\t{\n\t\t\tNativePointerMemberBehaviorOverride = PointerMemberBehavior.Disallow;\n\t\t}\n<\/pre><\/div>\n\n\n<p>This will enforce the use of TObjectPtr!<\/p>\n\n\n\n<p>But there are some issues you can encounter using these. As these should ONLY be for class members, not function parameters, you may run into some issues. But fear not, Epic gave us some templates for these:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ObjectPtrDecay<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">Whenever const access to the underlying storage of a TObjectPtr&lt;...&gt; (or a container of them) we should use this. Example is here:<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nUPROPERTY(Transient)\nTArray&lt;TObjectPtr&lt;AActor&gt;&gt; Actors;\n\nvoid MyFunc()\n{\n    TArray&lt;AActor*&gt; SelectedActors = ObjectPtrDecay(Actors);\n}\n\nAnother example is:\n\nUPROPERTY(Transient)\nTArray&lt;TObjectPtr&lt;UObject&gt;&gt; Objects;\n\nvoid MainFunc()\n{\n   SomeFunc(ObjectPtrDecay(Objects);\n}\n\nvoid SomeFunc(TArray&lt;UObject*&gt; InObjects)\n{\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">ObjectPtrWrap<\/h4>\n\n\n\n<p>This is the inverse of the above, basically makes a TArray&lt;UObject*&gt; to a TArray&lt;TObjectPtr&lt;UObject&gt;&gt;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">MutableView<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">MutableView: safely obtain temporary mutable access to a TObjectPtr's<br>                    underlying storage.<br>This allows you to pass a mutable view to the TObjectPtrs to functions, etc that requires it.<br><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvoid GetAllComponents(TArray&lt;UActorComponent*&gt;&amp; Components)\n{\n  Components.Add(Blah);\n}\nTArray&lt;TObjectPtr&lt;UActorComponent&gt;&gt; Components;\nGetAllComponents(MutableView(Components));\n<\/pre><\/div>\n\n\n<p>Hopefully this helps, just a small random bit of information!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As on Unreal Engine 5, TObjectPtr is the new way for classes to hold raw pointers.Pre UE5 you did Now with UE5 you should do: To enforce these, i highly recommend placing the following in your Target.cs file for your project (we normally just do it in the MyGameEditor.Target.cs This will enforce the use of <a href=\"https:\/\/www.thegames.dev\/?p=279\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Enforce TObjectPtr! (And how to avoid pitfalls)<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-279","post","type-post","status-publish","format-standard","hentry","category-random-bits"],"rise-blocks_total_comments":0,"rise-blocks_categories":[{"term_id":20,"name":"Random Bits","slug":"random-bits","term_group":0,"term_taxonomy_id":20,"taxonomy":"category","description":"Random bits of useful stuff.","parent":0,"count":2,"filter":"raw","cat_ID":20,"category_count":2,"category_description":"Random bits of useful stuff.","cat_name":"Random Bits","category_nicename":"random-bits","category_parent":0}],"rise-blocks_excerpt":"As on Unreal Engine 5, TObjectPtr is the new way for classes to hold raw pointers.Pre UE5 you did Now with UE5 you should do: To enforce these, i highly recommend placing the following in your Target.cs file for your project (we normally just do it in the MyGameEditor.Target.cs This will enforce the use of TObjectPtr! But there are some..","_links":{"self":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":10,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}