{"id":248,"date":"2022-10-20T00:30:04","date_gmt":"2022-10-20T00:30:04","guid":{"rendered":"https:\/\/merserver.com\/?p=248"},"modified":"2022-10-20T00:30:04","modified_gmt":"2022-10-20T00:30:04","slug":"in-unreal-engine-4-how-do-i-get-the-components-game-object-via-c","status":"publish","type":"post","link":"http:\/\/merserver.com\/index.php\/2022\/10\/20\/in-unreal-engine-4-how-do-i-get-the-components-game-object-via-c\/","title":{"rendered":"In Unreal Engine 4, how do I get the component&#8217;s &#8220;game object&#8221; via C++?"},"content":{"rendered":"\n<p>1<\/p>\n\n\n\n<p><a href=\"https:\/\/gamedev.stackexchange.com\/posts\/174445\/timeline\"><\/a><\/p>\n\n\n\n<p>To do this in Unreal and C++, you&#8217;ll need to set a pointer to the class and then cast to it.<\/p>\n\n\n\n<p>Casting is expensive in terms of overhead so if it&#8217;s something you&#8217;ll be doing often, it&#8217;s best to get it sorted in Begin Play and call the variable in Tick or whenever you want to call it.<\/p>\n\n\n\n<p>In the .h file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ACharacter;\n<\/code><\/pre>\n\n\n\n<p>In whatever section (public, private or protected) is best:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ACharacter* ComponentOwner = nullptr;\n<\/code><\/pre>\n\n\n\n<p>Over in your .cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"ACharacter.h\"\n<\/code><\/pre>\n\n\n\n<p>Then in your Begin Play<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ComponentOwner = Cast&lt;ACharacter&gt;(GetOwner());\n<\/code><\/pre>\n\n\n\n<p>It is good practise to protect your pointers to avoid crashes. While getting the program working, I&#8217;d check here at Begin Play as well:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (!ComponentOwner)\n{    \n  UE_LOG(LogTemp, Warning, TEXT(\"No ComponentOwner found!\");\n}\n<\/code><\/pre>\n\n\n\n<p>and then in Tick or whatever function is calling the character:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (!ComponentOwner)\n{ return; }\n\n\/\/ Put your code here<\/code><\/pre>\n\n\n\n<p>OR<\/p>\n\n\n\n<p>Use\u00a0<code>GetOwner()<\/code>\u00a0in the component<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 To do this in Unreal and C++, you&#8217;ll need to set a pointer to the class and then cast to it. Casting is expensive in terms of overhead so if it&#8217;s something you&#8217;ll be doing often, it&#8217;s best to get it sorted in Begin Play and call the variable in Tick or whenever you&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"http:\/\/merserver.com\/index.php\/2022\/10\/20\/in-unreal-engine-4-how-do-i-get-the-components-game-object-via-c\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;In Unreal Engine 4, how do I get the component&#8217;s &#8220;game object&#8221; via C++?&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-gamedev","category-unreal-engine"],"_links":{"self":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":1,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":249,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions\/249"}],"wp:attachment":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}