Using Custom Events and Structs

Choose your OS:

The Event Track can be used to call Custom Events , which fire off Blueprint Scripts inside of Level Blueprints . When the Custom Event is called, any script attached to it will also be executed and can be used for any number of scenarios based on your needs. When using the Event Track and calling a Custom Event, you can also pass your own Custom Struct , which can be used to update the variables contained therein. This can be useful in situations where you want to update other variables during the playback of a level sequence.

In this example, we will use the Event Track to call a Custom Event with a Custom Struct that is used to move an object in the level when the sequence plays.

EndResultImage.png

In the following how-to guide, when we press a key, the level sequence plays, and our event to move the cube upwards is executed each time.

Steps

For this how-to guide, we are using the Blueprint Third Person Template project with Starter Content enabled.

  1. From the Content Browser under the Content/StarterContent/Shapes folder, drag any mesh into the level.

    EventTrack1.png

    Position the mesh anywhere inside of the level.

  2. In the Details panel for your mesh, set the mesh to Movable.

    EventTrack1b.png

    This will enable us to move the mesh during gameplay.

  3. Click the Add New button. Then, under Blueprints, select the Structure option.

    EventTrack2.png

  4. Enter the name MyStruct, then open the asset and add a Vector variable called MoveCube.

    EventTrack3.png

    The naming of your variables is important here because these names will need to be used when you want to reference them later.

  5. Add a new Level Sequence from the Cinematics button called Master to the level, then inside Sequencer, add an Event Track.

    EventTrack4.png

  6. Move the timeline to frame 5 and add a key, then right-click the key and name the event LiftCube. Now, assign MyStruct and set the Z value to 100.

    EventTrack5.png

    When we get to this keyframe in our sequence, we are going to move our mesh up by 100 units along the Z axis.

  7. Move the timeline to frame 10 and press the ] key to end the sequence at frame 10.

    EventTrack6.png

  8. Select the mesh that you added (along with the Level Sequence in the level), and then click the Blueprints button from the toolbar, ultimately selecting Open Level Blueprint.

    EventTrack7.png

  9. In the Level Blueprint, right-click and search for Reference, then select Create References to 2 selected Actors.

    EventTrack8.png

  10. Right-click in the graph and add a 1 Keyboard Event, then drag off your Master Level Sequence, searching for and adding the Set Playback Position node.

    EventTrack9.png

    This will be used to reset your sequence whenever you start to play it before issuing a play function call.

  11. Drag off the Sequence Player node being added, and use the Play node, connecting as shown below.

    EventTrack10.png

  12. Right-click in the graph and create a Custom Event called LiftCube.

    EventTrack11.png

    Your Custom Event name must be the same name that is specified under Event Name for your key in Sequencer (or it will not get called).

  13. In the Details panel for the Custom Event, click the + button to add an input, naming it MoveCube with the variable type of Vector.

    EventTrack12.png

    The event will not be called if the following conditions aren't met:

    • The Input names must be the same names that are used in your Struct.

    • The number of inputs must match the number of variables defined in your custom struct.

    For example, if your struct has 2 variables in it, your Custom Event must have 2 inputs with the correct variable names and types.

  14. Drag off your mesh reference, search for and use the AddActorLocalOffset node.

    EventTrack13.png

  15. Connect the MoveCube vector pin to the Delta Location pin, and the LiftCube executable pin to the AddActorLocalOffset node as shown below.

    EventTrack14.png

  16. Click the Play button from the Main Toolbar to enter Play in Editor (PIE) mode.

End Result

When playing your game in the editor, each time you press the 1 key, the cube will move along the Z axis.

In this example, we are not assigning a camera to view our cinematic from but instead are using Sequencer and Blueprint to perform scripted sequences. In your projects, an Event Track with a Custom Event and Custom Struct can be used to update player information during a cutscene (such as the player receiving a health increase, new weapon, or other gameplay related effects of your choosing).

Tags