Language:
Page Info
Tags:
Skill Level:
Engine Version:
Related Pages

Creating Functions

Functions are node graphs belonging to a particular Blueprint that can be executed, or called, from another graph within the Blueprint. Functions have a single entry point designated by a node with the name of the Function containing a single exec output pin. When the Function is called from another graph, the output exec pin is activated causing the connected network to execute.

The steps below will guide you through creating a Function which will print text to the screen when a button is pressed.

  1. Inside the Content Browser, click the New Button then select Blueprint Class.

    NewBlueprint.png

  2. In Pick Parent Class window, select Actor.

    ActorBlueprint.png

  3. Name the Blueprint, then Double-click on it to open it up in the Blueprint Editor.

    FunctionBlueprint.png

  4. Right-click in the graph and search for and add the Event Begin Play Event.

    EventBeginPlay.png

    This node will execute once when the game is launched, along with any script that follows it.

  5. Right-click in the graph and search for and add the Get Player Controller node.

    GetPlayerController.png

    This will get the currently assigned player controller and allow us to Enable Input for this Blueprint.

  6. Right-click in the graph and search for and add the Enable Input node.

    EnableInput.png

    This is the node which will allow input to be received for this Blueprint.

  7. Connect the nodes as shown below.

    ConnectNodes.png

    When the game is launched, we get the player controller and enable input from it in this Blueprint.

  8. In the MyBlueprint window, click the Add New Function button.

    AddFunctionButton.png

  9. Select the new fuction in the My Blueprint window and press F2 to rename it.

    RenameFunction.png

    Give the Function a name such as "Print Text".

  10. In the Function's graph, drag off the Print Text pin and search for and add a Print String node.

    PrintStringFunction.png

  11. In the In String box, you can leave or change the text to the text you want to display in-game.

    EnterText.png

  12. Click the Event Graph tab to return to the Event Graph.

    EventGraphTab.png

  13. Right-click in the graph and search for and add an F Key Event.

    FKeyEvent.png

  14. Drag off the Pressed pin and search for and add your Print Text function.

    CallFunction.png

    When F is pressed, it will call the Print Text function which is set to print text to the screen using a Print String.

    If you do not see your function, try clicking the Compile button from the Toolbar then try your search again.

  15. Your network should look similar to below.

    FinishedGraph.png

  16. Click the Compile button then close the Blueprint.

    CompileButton.png

  17. Drag the Blueprint into the level, then click the Play button to play in the Editor.

    DragIntoLevel.png

  18. Press F and the Function will be called and print your text to the screen.

    WorkingFunction.png

While this example Function only prints text to the screen when a key is pressed, you could add more script to the Function which would be executed by the assigned key press.

For example, your Function could be used to cast a spell when the key is pressed and the script containing the spawning of the spell and its location, the effects associated with the spell and how it affects the world, or if it damages other Actors could all be contained within the function leaving your Event Graph free from all that script which would be contained neatly within the Function.

There are other ways Functions can be used and accessed, for more on Functions please see the Related Topics section below.

Tags