{"id":1091,"date":"2022-06-03T21:01:16","date_gmt":"2022-06-03T12:01:16","guid":{"rendered":"https:\/\/colory-games.net\/site\/?p=1091"},"modified":"2023-04-20T20:42:21","modified_gmt":"2023-04-20T11:42:21","slug":"execute-construction-script-intentionally-en","status":"publish","type":"post","link":"https:\/\/colory-games.net\/site\/en\/execute-construction-script-intentionally-en\/","title":{"rendered":"[UE5] Execute Construction Script Intentionally"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p><strong>Construction Scripts<\/strong> are automatically executed when you change the value of an Actor variable or move an Actor placed on a Level.<br>\nThis is a very useful function for work on the Level Editor, such as changing multiple variables simultaneously.<\/p>\n<p>Although the Construction Script is executed at the specific time, do you want to intentionally (forcefully) invoke it?<br>\nFor example, when one Actor is executed, another Actor should be executed.<\/p>\n<p>This article explains <strong>how to intentionally execute a Construction Script<\/strong>.<br>\nNote that this method requires implementation in C++.<br>\nWe will also explain how to execution in BP (Blueprints) for general use.<\/p>\n<ul>\n<li>This method can be executed during gameplay, but it may not be safe.<\/li>\n<\/ul>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#When_is_Construction_Script_Automatically_Executed\">When is Construction Script Automatically Executed<\/a><\/li><li><a href=\"#How_to_Intentionally_Execute_a_Construction_Script\">How to Intentionally Execute a Construction Script<\/a><ul><li><a href=\"#Sample_Specifications\">Sample Specifications<\/a><\/li><li><a href=\"#How_to_Execute_Construction_Script\">How to Execute Construction Script<\/a><\/li><li><a href=\"#Sample_Actor_Implementation\">Sample: Actor Implementation<\/a><\/li><li><a href=\"#Run_the_sample\">Run the sample<\/a><\/li><\/ul><\/li><li><a href=\"#How_to_Intentionally_Execute_Construction_Script_in_BP\">How to Intentionally Execute Construction Script in BP<\/a><\/li><li><a href=\"#Summary\">Summary<\/a><\/li><\/ul><\/div>\n<h1><span id=\"When_is_Construction_Script_Automatically_Executed\">When is Construction Script Automatically Executed<\/span><\/h1>\n<p>Construction Script is usually executed automatically by the following operations on Actors (Example).<\/p>\n<ul>\n<li>Change the value of a member variable<\/li>\n<li>Move, rotate, or expand the Actor<\/li>\n<li>Compile on the BP editor<\/li>\n<\/ul>\n<p>The process executed by Construction Script needs to be implemented in <code>Construction Script<\/code> on Actor&#8217;s BP editor.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/edit_construction_script.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/edit_construction_script.png?ssl=1\" alt=\"Construction Script\" data-recalc-dims=\"1\"><\/a><\/p>\n<h1><span id=\"How_to_Intentionally_Execute_a_Construction_Script\">How to Intentionally Execute a Construction Script<\/span><\/h1>\n<p>Now, we will explain how to intentionally execute a Construction Script.<br>\nTo summarize, it can be achieved by <strong>calling Actor&#8217;s member function RerunConstructionScripts<\/strong>.<\/p>\n<h2><span id=\"Sample_Specifications\">Sample Specifications<\/span><\/h2>\n<p>We will create the following sample.<\/p>\n<ul>\n<li>Create a class <code>ActorA<\/code> derived from <code>Actor<\/code> in C++. And, create a class <code>ActorB<\/code> derived from <code>Actor<\/code> in BP. Then, place them on Level.<\/li>\n<li>The <code>ActorA<\/code> class has a member variable <code>TargetActor<\/code> whose type is <code>TObjectPtr<AActor><\/code> (<code>AActor*<\/code> in UE4). The variable <code>TargetActor<\/code> is set to <code>ActorB<\/code> placed on the Level.<\/li>\n<li>The <code>ActorA<\/code> class displays &quot;A called&quot; on the screen when the construction script is executed. And executes the Construction Script of the Actor set in the variable <code>TargetActor<\/code>.<\/li>\n<li>The <code>ActorB<\/code> class displays &quot;B called&quot; on the screen when the construction script is executed.<\/li>\n<\/ul>\n<h2><span id=\"How_to_Execute_Construction_Script\">How to Execute Construction Script<\/span><\/h2>\n<p>The class <code>Actor<\/code> has a member function <strong>RerunConstructionScripts<\/strong> to execute Construction Scripts.<\/p>\n<pre><code class=\"language-cpp\">RerunConstructionScripts()\n<\/code><\/pre>\n<p>This function cannot be called in BP, only in C++.<br>\nWe will explain how to call it in BP later.<\/p>\n<p>By calling this function, the Construction Script can be executed at any time you want.<br>\nBy specifying the Actor, it is also possible to execute Construction Scripts of the other Actors.<\/p>\n<h2><span id=\"Sample_Actor_Implementation\">Sample: Actor Implementation<\/span><\/h2>\n<p>We will show <strong>C++ source code for the ActorA class<\/strong>.<br>\nThe project name is <code>MyProject<\/code>.<br>\nIn C++, the Construction Script need to be implemented in the <code>OnConstruction<\/code> function.<br>\nIn the <code>OnConstruction<\/code> function, call the <code>RerunConstructionScripts<\/code> function.<\/p>\n<p>Header file.<\/p>\n<pre><code class=\"language-cpp\">#pragma once\n\n#include &quot;GameFramework\/Actor.h&quot;\n#include &quot;ActorA.generated.h&quot;\n    \nUCLASS()\nclass MYPROJECT_API AActorA : public AActor\n{\n    GENERATED_BODY()\n    \npublic:\n    virtual void OnConstruction(const FTransform& Transform) override;\n    \n    UPROPERTY(EditAnywhere)\n    TObjectPtr<AActor> TargetActor;    \/\/ TObjectPtr<AActor> is AActor* in UE4\n};\n<\/code><\/pre>\n<p>Source file.<\/p>\n<pre><code class=\"language-cpp\">#include &quot;ActorA.h&quot;\n\nvoid AActorA::OnConstruction(const FTransform& Transform)\n{\n\tSuper::OnConstruction(Transform);\n\n    GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, &quot;A called&quot;);\n    if (TargetActor) {\n        TargetActor->RerunConstructionScripts();\n    }\n}\n<\/code><\/pre>\n<p>Next, we will show the <strong>BP of the ActorB class<\/strong>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/actorb_bp.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/actorb_bp.png?ssl=1\" alt=\"BP of ActorB class\" data-recalc-dims=\"1\"><\/a><\/p>\n<h2><span id=\"Run_the_sample\">Run the sample<\/span><\/h2>\n<p>Place the created <code>ActorA<\/code> and <code>ActorB<\/code> on the Level.<br>\nIf <code>ActorA<\/code> created by C++ is placed on the level, it cannot be moved.<br>\nSo, create a derived class of <code>ActorA<\/code> by BP and place it.<br>\nSet the placed <code>ActorB<\/code> to the variable <code>TargetActor<\/code> of the placed <code>ActorA<\/code>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/set_actors.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/set_actors.png?ssl=1\" alt=\"Actor Placement and Setup\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>Run <code>ActorA<\/code>&#8216;s Construction Script by moving <code>ActorA<\/code> or changing some variables.<br>\nYou will see &quot;A called&quot; and &quot;B called&quot; on the screen, and you can confirm that <strong>ActorB&#8217;s Construction Script is executed at the same time as ActorA<\/strong>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/move_actora.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/move_actora.png?ssl=1\" alt=\"Simultaneous Execution of Construction Script\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>As described above, Construction Scripts can be intentionally executed by calling the <code>RerunConstructionScripts<\/code> function.<br>\nIn this example, we showed the process of executing the Construction Script from another Actor.\nBut, the <code>RerunConstructionScripts<\/code> function can be called at any time,<\/p>\n<h1><span id=\"How_to_Intentionally_Execute_Construction_Script_in_BP\">How to Intentionally Execute Construction Script in BP<\/span><\/h1>\n<p>You think it is inconvenient that Construction Scripts can only be executed in C++.<br>\nYou can also call it in BP by <strong>creating a BP function that calls the RerunConstructionScripts function in the BP function library<\/strong>.<\/p>\n<p>Since we can call the <code>RerunConstructionScripts<\/code> only in C++, create a BP function library in C++.<br>\nWe will show an example of C++ source code.<br>\nThe library function name is <code>MyFunctionLibrary<\/code>.<br>\nIt takes an <code>Actor*<\/code> as input and executes the <code>RerunConstructionScripts<\/code> function of it.<\/p>\n<p>Header file.<\/p>\n<pre><code class=\"language-cpp\">#pragma once\n\n#include &quot;Kismet\/BlueprintFunctionLibrary.h&quot;\n#include &quot;MyFunctionLibrary.generated.h&quot;\n\nUCLASS()\nclass MYPROJECT_API UMyFunctionLibrary : public UBlueprintFunctionLibrary\n{\n\tGENERATED_BODY()\n\t\npublic:\n\tUFUNCTION(BlueprintCallable)\n\tstatic void RerunConstructionScripts(AActor* Target);\n};\n<\/code><\/pre>\n<p>Source file.<\/p>\n<pre><code class=\"language-cpp\">#include &quot;MyFunctionLibrary.h&quot;\n\nvoid UMyFunctionLibrary::RerunConstructionScripts(AActor* Target)\n{\n    if (Target) {\n        Target->RerunConstructionScripts();\n    }\n}\n<\/code><\/pre>\n<p>This library function can be used to create the <code>ActorA<\/code> class.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/actora_bp.png?ssl=1\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/colory-games.net\/site\/wp-content\/uploads\/tech-blog\/beisaku\/2022\/06\/20220603\/actora_bp.png?ssl=1\" alt=\"BP for ActorA class\" data-recalc-dims=\"1\"><\/a><\/p>\n<p>Thus, it is possible to intentionally execute Construction Script in BP as well as C++.<\/p>\n<h1><span id=\"Summary\">Summary<\/span><\/h1>\n<p>The important points of this article are as follows.<\/p>\n<ul>\n<li>Member function <code>RerunConstructionScripts<\/code> of <code>Actor<\/code> class makes it possible to intentionally execute Construction Script.<\/li>\n<li>BP can also intentionally execute Construction Scripts by creating BP function that call the <code>RerunConstructionScripts<\/code> function.<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>**Construction Scripts** are automatically executed when you change the value of an Actor variable or move an Actor placed on a Level.<br \/>\nThis is a very useful function for work on the Level Editor, such as changing multiple variables simultaneously.<\/p>\n","protected":false},"author":3,"featured_media":513,"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,162,154,179,94,71],"tags":[185],"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\/06\/20220603\/eye_catch.png?fit=260%2C128&ssl=1","_links":{"self":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1091"}],"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=1091"}],"version-history":[{"count":2,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"predecessor-version":[{"id":1154,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/posts\/1091\/revisions\/1154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media\/513"}],"wp:attachment":[{"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colory-games.net\/site\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}