# How do you make an async task that returns a TArray?

**URL:** <https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 23, 2020, 3:31pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760 "2020-02-23T15:31:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wnbaum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wnbaum](https://forums.unrealengine.com/u/wnbaum)\
**Post date:** [February 23, 2020, 3:31pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/1 "2020-02-23T15:31:13Z")

</div>

Hi,

I am fairly new to c++ programming, and I have a function that generates triangles to be used with a procedural mesh component, but I want it to be run as an async task. I am aware that you cannot edit values, or create new mesh sections in an async task, so I just want to call the async task, return the tris, and generate the mesh on the main thread.

How would I implement returning values from an async task that is executed from “begin play”?

Thanks,  
Will

Edit: I’ve googled a lot and read the documentation, something about using a delegate as an async task parameter? I am very confused please help!

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [February 23, 2020, 4:34pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/2 "2020-02-23T16:34:11Z")

</div>

Should be something like this.

```auto

            TFuture<TArray<MyTriangles>> TriangleFuture;
            TriangleFuture = Async(EAsyncExecution::TaskGraph, MyDelegate);

            // Later...
            if(TriangleFuture.IsReady())
            {
                // Process your triangles.
            }
            else
            {
                // Try again next frame or wait.
            }

```

---

<div class="post-metadata">

**Author:** ![wnbaum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wnbaum](https://forums.unrealengine.com/u/wnbaum)\
**Post date:** [February 23, 2020, 7:25pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/3 "2020-02-23T19:25:35Z")

</div>

So I’m assuming MyDelegate is the delegate that calculates the triangles, but what does TFuture do?

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [February 23, 2020, 10:06pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/4 "2020-02-23T22:06:30Z")

</div>

> [@not yote](#):
>
> So I’m assuming MyDelegate is the delegate that calculates the triangles, but what does TFuture do?

Yes, or you can use a lambda.

A TFuture is a thin wrapper that let’s you see if the Async task is done yet or not and what the returned value (in this case, a TArray) is.

---

<div class="post-metadata">

**Author:** ![wnbaum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wnbaum](https://forums.unrealengine.com/u/wnbaum)\
**Post date:** [February 23, 2020, 10:58pm UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/5 "2020-02-23T22:58:44Z")

</div>

```auto

    chunkDelegate chunkDel;
    chunkDel.BindUFunction(this, FName("InitChunk"));

    TFuture<TArray<FXYZ>> TriangleFuture;
    TriangleFuture = Async(EAsyncExecution::TaskGraph, chunkDel);

```

This gives me an error on the Async(EAsyncExecution::TaskGraph, chunkDel) part, I don’t know why.  
no instance of function template “Async” matches the argument list

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [February 24, 2020, 1:33am UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/6 "2020-02-24T01:33:04Z")

</div>

You can look at Async.h for examples, looks like you need to wrap functions in TUniqueFunction:

```auto

 * // using global function
 * int TestFunc()
 * {
 * return 123;
 * }
 *
 * TUniqueFunction<int()> Task = TestFunc();
 * auto Result = Async(EAsyncExecution::Thread, Task);
 *
 * // using lambda
 * TUniqueFunction<int()> Task = ]()
 * {
 * return 123;
 * }
 *
 * auto Result = Async(EAsyncExecution::Thread, Task);
 *
 *
 * // using inline lambda
 * auto Result = Async(EAsyncExecution::Thread, ]() {
 * return 123;
 * }

```

---

<div class="post-metadata">

**Author:** ![wnbaum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wnbaum](https://forums.unrealengine.com/u/wnbaum)\
**Post date:** [February 25, 2020, 3:55am UTC](https://forums.unrealengine.com/t/how-do-you-make-an-async-task-that-returns-a-tarray/138760/7 "2020-02-25T03:55:41Z")

</div>

Still getting an error on build

TUniqueFunction\<TArray\<FXYZ,FDefaultAllocator\> (void)\>::TUniqueFunction(const TUniqueFunction\<TArray\<FXYZ,FDefaultAllocator\> (void)\> &)’: attempting to reference a deleted function

Code:

```auto

TUniqueFunction<TArray<FXYZ>()> Task = [this, x, y, z, scalarField]()
    {
        TArray<FXYZ> triArray = InitChunk(x, y, z, 0.5, scalarField, scale);
        return triArray;
    };

    Result = Async(EAsyncExecution::Thread, Task);

```

also, Result is defined in the header file
