Giter Club home page Giter Club logo

ue5coro's Introduction

UE5Coro

This library implements C++20 coroutine support for Unreal Engine 5. It complements the engine's (as of 5.0) experimental coroutine support with additional features such as easy authoring of BP latent actions.

Installing

Download the release that you wish to use from the Releases page, extract it to your project's Plugins folder and in case you downloaded a source code zip, rename the folder that it contains to just UE5Coro so that you end up with YourProject\Plugins\UE5Coro\UE5Coro.uplugin.

In modules where you wish to use coroutines, add or change this line in the corresponding Build.cs file to enable the required language features:

CppStandard = CppStandardVersion.Cpp20;

Add "UE5Coro" to your dependency module names in the same Build.cs file, enable the plugin in the Unreal editor, and you're ready to go!

Features

Click these links for the detailed description of the main features provided by this plugin, or keep reading for a quick overview.

  • Generators are caller-controlled and co_yield a sequence of objects, also known as iterators in C#.
  • Async coroutines control their own resumption by co_awaiting various awaiter objects. They can be used to implement BP latent actions or as a generic fork in code execution like AsyncTask, but not necessarily involving multithreading.
  • Overview of built-in awaiters that you can use with async coroutines.

Generators

Generators can be used to return an arbitrary number of items from a function without having to pass them through temp arrays, etc. In C# they're known as iterators.

Returning UE5Coro::TGenerator<T> makes a function coroutine enabled, supporting co_yield:

using namespace UE5Coro;

TGenerator<FString> MakeParkingSpaces(int Num)
{
    for (int i = 1; i <= Num; ++i)
        co_yield FString::Printf(TEXT("๐Ÿ…ฟ๏ธ %d"), i);
}

// Elsewhere
for (const FString& Str : MakeParkingSpaces(123))
    Process(Str);

Async coroutines

Return FAsyncCoroutine from a function (regular or UFUNCTION) to make it coroutine enabled and support co_await. There's special handling in place that automatically implements BP latent actions for you but it works for everything:

using namespace UE5Coro;

UFUNCTION(BlueprintCallable, Meta = (Latent, LatentInfo = "LatentInfo"))
FAsyncCoroutine UExampleFunctionLibrary::K2_Foo(int EpicPleaseFixUE22342,
                                                FLatentActionInfo LatentInfo)
{
    // You can freely hop between threads:
    co_await Async::MoveToThread(ENamedThreads::AnyBackgroundThreadNormalTask);
    DoExpensiveThingOnBackgroundThread();
    co_await Async::MoveToGameThread();

    // Delay for 1 more second without blocking the game thread:
    co_await Latent::Seconds(1.0f);

    // The BP node will fire its latent exec pin once control leaves the coroutine.
}

ue5coro's People

Contributors

landelare avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.