Custom latent – delayed Blueprint function in C++

30 Nov

I wanted to make two functions with delays to assist BP scripting of logic. One for dialog and one for item interaction. The one I’ll cover here handles dialog.

My goal: Make a function that accepts dialog parameters, waits for a user response and returns the player’s selection. (Additionally, make a breakout which flows execution based on the player’s selection.)

I couldn’t find any help online when writing this function. So I crept through engine code. Unreal has a LatentActionManager held by UWorld. You can get its reference from GetWorld->GetLatentActionManager()

Using this I made a c++ class derived from FPendingLatentAction and put our own logic in it.

 

 

 

Here is the BP functions header:

Here is the function’s definition:

What happens above, we grab the LatentActionManager and make sure it currently isn’t running a task for this object and pass it our custom latent task, along with our custom latent tasks construction params.  InteractionInfo is a custom struct with the players choices, and PlayerSelection is an enum of the possible responses the player can have.

Let’s look at my custom FPendingLatentAction derived class:

Constructor:

I create a callback (I send a self reference to the actor who called this function.) Once the player has selected input, I push a response to this delayed action.

One could also poll from the UpdateOperation. The tick/update function notes a value has been set and completes the task.

If you want to break out the response use the ExpandEnumAsExecs UFunction specifier:

 

 

Leave a Reply

Your email address will not be published.