Construction Script

One of the panels in the Blueprint Editor is called Construction Script, and is shown in the following screenshot. Construction Script is a special Function that all Actor Blueprints perform when the Blueprint is first added to the Level, when a change is made to its properties in the Level Editor, or when an instance is spawned at runtime:

Construction Script is very useful for creating flexible Blueprints that allow the Level Designer to configure some features of an instance of these Blueprints within the Level Editor.

As an example, let's create a Blueprint with an Instance Editable Static Mesh so we can choose a different Static Mesh for each instance of the Blueprint that is on the Level:

  1. Create or use an existing project that has the starter content.
  2. Click the Add New button in Content Browser and choose the Blueprint Class option.
  3. On the next screen, choose Actor as the parent class.
  4. Name the Blueprint ConstructionBP and double-click it to open the Blueprint Editor.
  5. Click the Add Component button in the Components panel and choose the Static Mesh Component. Rename the Component StaticMeshComp, as shown in the following screenshot:

  1. In the My Blueprint panel, create a new variable named SM_Mesh. In the Details panel, click the Variable Type drop-down menu and search for Static Mesh. Hover over Static Mesh to display a submenu and then choose Object Reference. Check the Instance Editable attribute, as shown in the following screenshot:

  1. Press the Compile button on the toolbar. Let's define an initial Static Mesh for the SM_Mesh variable at the bottom of the Details panel. Click the drop-down menu of the Default value attribute and choose the SM_TableRound Static Mesh.
  2. Click the Construction Script panel. Drag the StaticMeshComp Component from the Components panel and drop it into the Construction Script graph to create a node.
  3. Click on the blue pin of the StaticMeshComp node, then drag and drop in the graph to open the Context Menu. Search for set static mesh and choose the Function with this name, as shown in the following screenshot:

  1. Drag the SM_Mesh variable from the My Blueprint panel, drop it into the Construction Script graph, and choose the Get SM_Mesh option in the menu that appears. Connect the SM_Mesh node pin to the New Mesh pin of the Set Static Mesh Function. Construction Script should look similar to the following screenshot. When Construction Script executes the Set Static Mech Function, it gets the Static Mesh from the SM_Mesh variable and sets it on the Static Mesh Comp Component:

  1. Compile the Blueprint. In the Level Editor, drag ConstructionBP from Content Browser and drop it into the Level to create an instance. Drag and drop the ConstructionBP again to create one more instance. Select one of the instances on the Level and, in the Details panel of the Level Editor, check that the SM_Mesh variable is visible and editable, as shown in the following screenshot:

  1. Click the drop-down menu of the SM_Mesh variable and choose another Static Mesh, such as the SM_Couch. The Construction Script will immediately execute and change the Static Mesh of the instance that was selected. The following screenshot shows two instances of the ConstructionBP class. The instance on the left of the screenshot is using the default Static Mesh, but the instance on the right had its Static Mesh modified to SM_Couch:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset