{"id":1126,"date":"2022-07-09T22:05:08","date_gmt":"2022-07-09T13:05:08","guid":{"rendered":"https:\/\/colory-games.net\/site\/?p=1126"},"modified":"2023-11-04T20:47:36","modified_gmt":"2023-11-04T11:47:36","slug":"printstring_cpp-en","status":"publish","type":"post","link":"https:\/\/colory-games.net\/site\/en\/printstring_cpp-en\/","title":{"rendered":"[UE5] Use PrintString in C++"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>The PrintString function is very useful for debugging Blueprint (BP).<br>\nIt is also useful in C++ implementation.<br>\nBut for C++ beginner, you may not know how to call it.<br>\nThis article explains <strong>How to call the PrintString function in C++<\/strong>.<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#How_to_Call_the_PrintString_function_in_C\">How to Call the PrintString function in C++<\/a><ul><li><a href=\"#1_Include_the_UKismetSystemLibrary_Class\">1. Include the UKismetSystemLibrary Class<\/a><\/li><li><a href=\"#2_Call_the_PrintString_Function\">2. Call the PrintString Function<\/a><\/li><li><a href=\"#3_Sample\">3. Sample<\/a><\/li><\/ul><\/li><li><a href=\"#How_to_Research_the_Calling_Process_of_Functions\">How to Research the Calling Process of Functions<\/a><\/li><\/ul><\/div>\n<h1><span id=\"How_to_Call_the_PrintString_function_in_C\">How to Call the PrintString function in C++<\/span><\/h1>\n<p>We can call PrintString function as follows.<\/p>\n<ol>\n<li>Include the UKismetSystemLibrary class<\/li>\n<li>Call the PrintString function<\/li>\n<\/ol>\n<h2><span id=\"1_Include_the_UKismetSystemLibrary_Class\">1. Include the UKismetSystemLibrary Class<\/span><\/h2>\n<p>The PrintString function is defined in the UKismetSystemLibrary class.<br>\n<strong>Put the following include statement at the top of the source file<\/strong>.<\/p>\n<pre><code class=\"language-cpp\">#include &quot;Kismet\/KismetSystemLibrary.h&quot;\n<\/code><\/pre>\n<h2><span id=\"2_Call_the_PrintString_Function\">2. Call the PrintString Function<\/span><\/h2>\n<p>We will explain how to output a string directly and how to output the value of a variable.<\/p>\n<h3>Case: Output a String Directory<\/h3>\n<p>We will implment the following process in C++.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/printstring_string.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/printstring_string.png?ssl=1\" alt=\"PrintString Function\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>In C++, <strong>put the following PrintString function<\/strong> where you want to call it.\nThe third and subsequent arguments can be omitted.<\/p>\n<pre><code class=\"language-cpp\">\/\/ If not omitted.\nUKismetSystemLibrary::PrintString(GEngine->GetWorld(), &quot;Hello&quot;, true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);\n\n\/\/ If omitted.\nUKismetSystemLibrary::PrintString(GEngine->GetWorld(), &quot;Hello&quot;);\n<\/code><\/pre>\n<p>The relation between BP and C++ function inputs (arguments) is as follows.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/compare_cpp_bp.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/compare_cpp_bp.png?ssl=1\" alt=\"The Relation between BP and C++\" data-recalc-dims=\"1\"><\/a><\/p>\n<h3>Case: Output the Value of a Variable<\/h3>\n<p>You may want to output the value of the variable.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/printstring_value.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/printstring_value.png?ssl=1\" alt=\"PrintString Function\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>We can implement this by following C++ code.\nIn <strong>FString::SanitizeFloat(Val), the float variable Val is converted into a string variable (FString)<\/strong>.<\/p>\n<pre><code class=\"language-cpp\">UKismetSystemLibrary::PrintString(GEngine->GetWorld(), FString::SanitizeFloat(Val), true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);\n<\/code><\/pre>\n<p>We can convert the other type variable into a string variable as follows (the variable name is assumed to be Val).<\/p>\n<table>\n<thead>\n<tr>\n<th>Source Data Type<\/th>\n<th>How to Convert into a string<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>float<\/td>\n<td>FString::SanitizeFloat(Val)<\/td>\n<\/tr>\n<tr>\n<td>int<\/td>\n<td>FString::FromInt(Val)<\/td>\n<\/tr>\n<tr>\n<td>FVector<\/td>\n<td>Val.ToString()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Conversions from other types are summarized in the official documentation.<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/ProgrammingAndScripting\/ProgrammingWithCPP\/UnrealArchitecture\/StringHandling\/FString\/\">FString _ Unreal Engine 4.27 Documentation<\/a><\/li>\n<\/ul>\n<h2><span id=\"3_Sample\">3. Sample<\/span><\/h2>\n<p>We will show an example of the PrintString function.<br>\nThis example dispalys the result of A+B on the screen.<\/p>\n<pre><code class=\"language-cpp\">#include &quot;Hoge.h&quot;\n#include &quot;Kismet\/KismetSystemLibrary.h&quot;    \/\/ Include a header file that UKismetSystemLibrary class is defined.\n\nvoid UHoge::Func(float A, float B)\n{\n    float Out = A + B;\n\n    \/\/ Call PrintString function.\n    UKismetSystemLibrary::PrintString(GEngine->GetWorld(), FString::SanitizeFloat(Out), true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);\n}\n<\/code><\/pre>\n<h1><span id=\"How_to_Research_the_Calling_Process_of_Functions\">How to Research the Calling Process of Functions<\/span><\/h1>\n<p>(We will publish later in other articles)<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The PrintString function is very useful for debugging Unreal Engine blueprints.<br \/>\nIt is also useful in C++ implementation, but for C++ beginners, they do not know how to call it.<br \/>\nThis article explains how to call the PrintString function in C++.<\/p>\n","protected":false},"author":3,"featured_media":706,"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,154,94,71],"tags":[158],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/07\/20220709\/eyecatch.png?fit=323%2C143&ssl=1","_links":{"self":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1126"}],"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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/comments?post=1126"}],"version-history":[{"count":2,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1126\/revisions"}],"predecessor-version":[{"id":1150,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1126\/revisions\/1150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media\/706"}],"wp:attachment":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media?parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/categories?post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/tags?post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}