{"id":1087,"date":"2022-04-24T11:34:01","date_gmt":"2022-04-24T02:34:01","guid":{"rendered":"https:\/\/colory-games.net\/site\/?p=1087"},"modified":"2023-04-20T20:43:18","modified_gmt":"2023-04-20T11:43:18","slug":"define-console-commands-using-cheatmanager-en","status":"publish","type":"post","link":"https:\/\/colory-games.net\/site\/en\/define-console-commands-using-cheatmanager-en\/","title":{"rendered":"[UE4] Define Console Commands Using CheatManager"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>The Unreal Engine allows you to use console commands during gameplay to change the behavior of the game during the gameplay.<br>\nYou can execute console commands at any time during gameplay.<br>\nThis is useful for debugging purposes.<br>\nSeveral useful console commands have already defined by default, and you can define your own console commands.<\/p>\n<p>This article describes how to define console commands using CheatManager.<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#What_are_console_commands\">What are console commands?<\/a><\/li><li><a href=\"#Define_Console_Commands_using_CheatManager\">Define Console Commands using CheatManager<\/a><ul><li><a href=\"#Example\">Example<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"What_are_console_commands\">What are console commands?<\/span><\/h1>\n<p>Console commands are commands that can be executed on the level editor viewport.<br>\nYou can display the console by pressing the key &#8220;&#8220; on the level editor viewport.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/console_command.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/console_command.png?ssl=1\" alt=\"Console Command\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>However, &#8220;` cannot be entered from some keyboard layouts.<br>\nYou need to add the appropriate keys from [Project Settings] &gt; [Input] &gt; [Console] &gt; [Console Keys].<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/set_console_keys.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/set_console_keys.png?ssl=1\" alt=\"Console Key Settings\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>Several console commands are provided by default.<br>\nFor example, the Stat command is a console command that can display CPU and GPU execution times for each process.<br>\nThis is a very useful console command when you look for the processes that are performance bottlenecks in your game.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/console_command_stat_gpu.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/console_command_stat_gpu.png?ssl=1\" alt=\"Console Command (Display GPU Execution Time Display)\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>You may want to enable this function to evaluate performance and display only when you need.<br>\nThe console command, which can be enabled\/disabled at any time during gameplay, matches this purpose.<\/p>\n<h1><span id=\"Define_Console_Commands_using_CheatManager\">Define Console Commands using CheatManager<\/span><\/h1>\n<p>To define a console command, you need to add <code>exec<\/code> to the argument of the <code>UFUNCTION<\/code> macro.<\/p>\n<pre><code class=\"language-cpp\">UFUNCTION(exec)\nvoid SAMPLE_Command();\n<\/code><\/pre>\n<p>However, you can add an argument <code>exec<\/code> to the <code>UFUNCTION<\/code> macro only to following classes.<\/p>\n<ul>\n<li>Pawn<\/li>\n<li>PlayerController<\/li>\n<li>CheatManager<\/li>\n<li>GameMode<\/li>\n<li>PlayerInput<\/li>\n<li>HUD<\/li>\n<\/ul>\n<p>We will define console commands in CheatManager.<br>\nCheatManager is a class with various debugging features, which automatically disabled when you packages the game with the release configuration.<br>\nFor this reason, CheatManager is the suitable target for implementing console commands for debugging purposes.<\/p>\n<p>To learn more about CheatManager, please refer to the official documentation.<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/BlueprintAPI\/CheatManager\/\">Cheat Manager (Blueprint API)<\/a><\/li>\n<li><a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/API\/Runtime\/Engine\/GameFramework\/UCheatManager\/\">UCheatManager (C++ API)<\/a><\/li>\n<\/ul>\n<h2><span id=\"Example\">Example<\/span><\/h2>\n<p>Let&#8217;s create our own class inherited from CheatManager and define a console command to it.<br>\nThis time, we will define a console command <code>SAMPLE_OutputLog<\/code> that receives a string as a command argument and outputs the received string to a log.<\/p>\n<h3>1. Define a Console Command<\/h3>\n<p>CheatManager is defined as <code>UCheatManager<\/code> class in <code>GameFramework\/CheatManager.h<\/code>.<br>\nDefine <code>UOutputLogConsoleCommandManager<\/code> class derived from <code>UCheatManager<\/code> class.<\/p>\n<pre><code class=\"language-cpp\">#pragma once\n\n#include &quot;CoreMinimal.h&quot;\n#include &quot;GameFramework\/CheatManager.h&quot;\n\n#include &quot;OutputLogConsoleCommand.generated.h&quot;\n\n\nUCLASS()\nclass REFERENCEPROEJCT_API UOutputLogConsoleCommandManager : public UCheatManager\n{\n    GENERATED_BODY()\n\npublic:\n\n    \/\/ Define a console command &quot;SAMPLE_OutputLog&quot;.\n    UFUNCTION(exec)\n    void SAMPLE_OutputLog(FString Message);\n};\n<\/code><\/pre>\n<p>You can define a console command by adding the macro <code>UFUNCTION(exec)<\/code> to the member function <code>SAMPLE_OutputLog<\/code> defined in <code>UOutputLogConsoleCommandManager<\/code>.<br>\nNote that <strong>the name of the member function becomes the console command name<\/strong>.<br>\nThe <code>SAMPLE_OutputLog<\/code> console command accepts a string as a command argument, so the argument <code>Message<\/code> with type <code>FString<\/code> is added to the member function <code>SAMPLE_OutputLog<\/code>.<br>\n<strong>The argument of the member function is the command argument of the console command<\/strong>.<\/p>\n<p>Next, we will implement the member function <code>SAMPLE_OutputLog<\/code>.<br>\nThe <code>UE_LOG<\/code> macro is used to output the string received as the command argument to the log.<\/p>\n<pre><code class=\"language-cpp\">#include &quot;OutputLogConsoleCommand.h&quot;\n\n\nvoid UOutputLogConsoleCommandManager::SAMPLE_OutputLog(FString Message)\n{\n    UE_LOG(LogTemp, Log, TEXT(&quot;Message: %s&quot;), *Message);\n}\n<\/code><\/pre>\n<h3>2. Enable CheatManager<\/h3>\n<p>The <code>UOutputLogConsoleCommandManager<\/code> class must be enabled before you can use it.<br>\nTo enable <code>UOutputLogConsoleCommandManager<\/code> class, you must create your own PlayerController and specify <code>UOutputLogConsoleCommandManager<\/code> class in the member variable <code>CheatClass<\/code>.<\/p>\n<p>To implement this in Bluepirnt, inherit PlayerController and set <code>OutputLogConsoleCommandManager<\/code> to the Cheat Class in the Details tab.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/enable_cheat_manager.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/enable_cheat_manager.png?ssl=1\" alt=\"Enable CheatManager\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>To implement in C++, add <code>UOutputLogConsoleCommandManager::StaticClass()<\/code> to the member variable <code>CheatClass<\/code> in the constructor of the class derived from PlayerController.<br>\nBelow code shows the implementation of the <code>AOutputLogConsoleCommandPlayerController<\/code> class derived from PlayerController.<\/p>\n<pre><code class=\"language-cpp\">AOutputLogConsoleCommandPlayerController::AOutputLogConsoleCommandPlayerController(const FObjectInitializer&amp; ObjectInitializer)\n{\n    CheatClass = UOutputLogConsoleCommandManager::StaticClass();\n}\n<\/code><\/pre>\n<h3>3. Create GameMode<\/h3>\n<p>To use the created PlayerController during gameplay, create a new GameMode (in this case <code>BP_OutputLogConsoleCommandGameMode<\/code>) in Blueprint and set the PlayerController to <code>OutputLogConsoleCommandPlayerController<\/code>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/create_game_mode.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/create_game_mode.png?ssl=1\" alt=\"Create a GameMode\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>Set the newly created GameMode to the level.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/set_game_mode.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/set_game_mode.png?ssl=1\" alt=\"Set GameMode\" data-recalc-dims=\"1\"><\/a><\/p>\n<h3>4. Check<\/h3>\n<p>Display the console while gameplay and type <code>SAMPLE_OutputLog Hoge<\/code>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/enter_sample_output_log.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/enter_sample_output_log.png?ssl=1\" alt=\"Enter Command to Console\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>Press Enter to execute the console command, and you will see <code>LogTemp: Message: Hoge<\/code> in the [Output Log].<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/output_log.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/nutti\/2022\/04\/20220424\/output_log.png?ssl=1\" alt=\"Output Log\" data-recalc-dims=\"1\"><\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Unreal Engine allows you to use console commands during gameplay to change the behavior of the game during the gameplay.<br \/>\nYou can execute console commands at any time during gameplay.<br \/>\nThis is useful for debugging purposes.<br \/>\nSeveral useful console commands have already defined by default, and you can define your own console commands.<\/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":[179,94],"tags":[158],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1087"}],"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=1087"}],"version-history":[{"count":3,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":1156,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1087\/revisions\/1156"}],"wp:attachment":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}