{"id":1074,"date":"2021-11-03T15:47:54","date_gmt":"2021-11-03T06:47:54","guid":{"rendered":"https:\/\/colory-games.net\/site\/?p=1074"},"modified":"2023-11-04T20:54:11","modified_gmt":"2023-11-04T11:54:11","slug":"receive-input-in-actor-en","status":"publish","type":"post","link":"https:\/\/colory-games.net\/site\/en\/receive-input-in-actor-en\/","title":{"rendered":"[UE4] Receive input in Actor"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Actors can not receive the player input.<br>\nIt is common to use Pawn to receive the player input.<br>\nHowever, you may want to receive the input in Actor as well.<\/p>\n<p>This article explains how to receive input from the player in Actor, in both Blueprint and C++.<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#Case_Blueprint\">Case: Blueprint<\/a><ul><li><a href=\"#Enable_Input\">Enable Input<\/a><\/li><li><a href=\"#Definition_of_Input_Processing\">Definition of Input Processing<\/a><\/li><li><a href=\"#Case_C\">Case: C++<\/a><\/li><li><a href=\"#Enable_Input-2\">Enable Input<\/a><\/li><li><a href=\"#Definition_of_Input_Processing-2\">Definition of Input Processing<\/a><\/li><\/ul><\/li><li><a href=\"#Summary\">Summary<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Case_Blueprint\">Case: Blueprint<\/span><\/h1>\n<h2><span id=\"Enable_Input\">Enable Input<\/span><\/h2>\n<p>In the case of Blueprint, open the Blueprint editor of Actor and set <strong>Auto Receive Input<\/strong> located in Input on the Details tab to <strong>Player 0<\/strong>.\nThen, the Player Controller will be able to receive the player&#8217;s input.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2021\/11\/20211103\/bp_enable_input.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2021\/11\/20211103\/bp_enable_input.png?ssl=1\" alt=\"Enabling Input (Blueprint)\" data-recalc-dims=\"1\"><\/a><\/p>\n<h2><span id=\"Definition_of_Input_Processing\">Definition of Input Processing<\/span><\/h2>\n<p>You can process the player&#8217;s input by defining the process of events while the player&#8217;s input is enabled.<\/p>\n<p>We will show an example of defining the process for an input, which assumes that &quot;AxisA&quot; is set for Axis and &quot;ActionB&quot; for Action.<br>\nFor &quot;AxisA&quot; input, place an &quot;InputAxis AxisA&quot; node and define the process you want to execute.<br>\nSimilarly, for &quot;ActionB&quot;, place an &quot;InputAxis ActionB&quot; node and define the process.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2021\/11\/20211103\/define_input_process.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2021\/11\/20211103\/define_input_process.png?ssl=1\" alt=\"Defining the process for the input (Blueprint)\" data-recalc-dims=\"1\"><\/a><\/p>\n<h2><span id=\"Case_C\">Case: C++<\/span><\/h2>\n<h2><span id=\"Enable_Input-2\">Enable Input<\/span><\/h2>\n<p>In the case of C++, you need to override the member function <code>BeginPlay<\/code> and call the member function <code>EnableInput<\/code> defined in the <code>AActor<\/code> class to receive input from the player.<br>\nThe <code>PlayerController<\/code> must be passed as an argument.<br>\nIn this example, the first <code>PlayerController<\/code> is passed.<\/p>\n<pre><code class=\"language-cpp\">void AInputEnabledActor::BeginPlay()\n{\n    Super::BeginPlay();\n    \/\/ Enable Input.\n    EnableInput(GetWorld()->GetFirstPlayerController());\n    \/\/ ...\n}\n<\/code><\/pre>\n<h2><span id=\"Definition_of_Input_Processing-2\">Definition of Input Processing<\/span><\/h2>\n<p>When an input is enabled, the member variable <code>InputComponent<\/code> of the <code>UInputComponent<\/code> defined in the <code>AActor<\/code> class will be set a value (If <code>EnableInput<\/code> is not called, <code>InputComponent<\/code> becomes <code>nullptr<\/code>).<br>\nOnce the input is enabled, only you need to do is defining the process for the input same as Pawn.<\/p>\n<p>We will define the process for the input immediately after the input is enabled with <code>BeginPlay<\/code>.<\/p>\n<p>It is also possible to set the behaviour for the defined input same as Pawn.<br>\nIn this example, we define <code>bConsumeInput<\/code>, which determines whether the input is propagated to other objects.<br>\nAnd we define <code>bExecuteWhenPaused<\/code>, which determines whether the input can be received while paused.<\/p>\n<pre><code class=\"language-cpp\">void AInputEnabledActor::AxisAProcess(float AxisValue)\n{\n    FVector NewLocation = GetActorLocation();\n    NewLocation += FVector(0.0, AxisValue, 0.0);\n    SetActorLocation(NewLocation);\n}\nvoid AInputEnabledActor::ActionBPressedProcess()\n{\n    UKismetSystemLibrary::PrintString(NULL, TEXT(&quot;ActionB Pressed&quot;));\n}\nvoid AInputEnabledActor::BeginPlay()\n{\n    Super::BeginPlay();\n    \/\/ Enable Input.\n    EnableInput(GetWorld()->GetFirstPlayerController());\n    if (InputComponent)\n    {\n        \/\/ Register the process of input &quot;AxisA&quot;.\n        FInputAxisBinding&amp; AxisBinding = InputComponent->BindAxis(&quot;AxisA&quot;, this, &amp;AInputEnabledActor::AxisAProcess);\n        AxisBinding.bConsumeInput = false;\n        AxisBinding.bExecuteWhenPaused = true;\n        \/\/ Register the process of input &quot;ActionB&quot;.\n        FInputActionBinding&amp; ActionBinding = InputComponent->BindAction(&quot;ActionB&quot;, IE_Pressed, this, &amp;AInputEnabledActor::ActionBPressedProcess);\n        ActionBinding.bConsumeInput = false;\n        ActionBinding.bExecuteWhenPaused = true;\n    }\n}\n<\/code><\/pre>\n<h1><span id=\"Summary\">Summary<\/span><\/h1>\n<p>This article explained how to receive player input in Actor.<\/p>\n<p>We have seen that both Blueprint and C++ require the Player Controller need to be set up properly in order to receive input.<br>\nOnce the Player Controller has been set up, the rest procedures is the same as with Pawn.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Actors can not receive the player input.<br \/>\nIt is common to use Pawn to receive the player input.<br \/>\nHowever, you may want to receive the input in Actor as well.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"templates\/single-home-techblog.php","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[160,179,94],"tags":[185],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1074"}],"collection":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/comments?post=1074"}],"version-history":[{"count":7,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1074\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1074\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media?parent=1074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/categories?post=1074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/tags?post=1074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}