1.3 - Spawned Actors

An overview of the Level Scripting example level, example 1.3: Spawned actors with event dispatchers

Windows
MacOS
Linux

levelscripting_1_3.png

In this example, when the player walks into a box trigger, 8 coin pickups are spawned, each of which contains a dispatch function for when it is collected by the player. In order to open the next set of doors, the player must collect all 8 coins.

Level Script

Click for full size or right-click and Save As.

In this case, because the coin Blueprints are spawned during gameplay, they cannot be referenced in level script in the same way the button Blueprint was. Furthermore, the script needs to know when every coin has been collected, which could get messy with a lot of events. Instead, each coin that spawns has its dispatch function bound to a single custom event. This way, when the coin Blueprints are collected, they call the dispatch function and trigger the custom event instead. The script counts how many times the event has fired off until it matches the number of coins that were spawned.

Binding multiple functions to a custom event is useful for a lot of typical gameplay scenarios, such as spawning a group of enemies and then triggering something when all enemies have been killed. In that example, you might bind each spawned enemy to a single 'OnEndPlay' event, which would fire every time one of the enemies was destroyed.

BP_Double_Doors Event Graph

Click for full size or right-click and Save As.

The BP_Double_Doors Blueprint Actor contains a setup for a somewhat sophisticated door. You can see how the Timeline is used to drive several things, including the location of each door, the rotation of the bolt mechanism, and the Material applied to the bolt. The network is fired whenever the Blueprint receives the Open Doors or Close Doors events, which are custom events created for this Blueprint.

Coins Setup

The coins in this example are also used in Example 2.5 of the Blueprints_Advanced level .

Parent Setup

Click for full size or right-click and Save As.

The parent Blueprint, BP_Pickup_Parent, contains script for basic pickup behavior, such as a timeline animation to make the pickup spin and float, and a sequence for collecting the pickup. The child Blueprints inherit all of this script, but have additional script for when they are collected, so that each pickup does something different while sharing the same basic features. This is useful because any updates made to the parent Blueprint's script will also apply to all its child Blueprints.

The OnPickedUp event dispatcher is called when the player's Pawn overlaps the Pickup Trigger SphereComponent. Each child Blueprint binds a different event to OnPickedUp, so that a different string prints when the player collects each pickup and OnPickedUp is called.

Click for full size or right-click and Save As.

As a rule, Blueprint graphs cannot contain duplicates of the same event, so if both the parent script and the child script each contain a ReceiveBeginPlay event, only the event in the parent script will fire. You can get around this in the child script by right-clicking an event and selecting Add call to parent function. This will create a function that will trigger the corresponding event in the parent script when it is called.

Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback