CodeNewWorlds / CoroBehaviour

Coroutine like Unity using C++ and boost coroutine2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CoroBehaviour

This is the coroutine class using C++ and boost coroutine2.
Usage of CoroBehaviour is very similar with MonoBehavior Unity.
You may try to introduce this to the project of Unreal Engine.

Requirements

Boost Coroutine2

CoroBehaviour vs. MonoBehaviour

MonoBehaviour in Unity:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(coroutineA());
    }

    IEnumerator coroutineA()
    {
        // wait for 1 second
        Debug.Log("coroutineA created");
        yield return new WaitForSeconds(1.0f);
        yield return StartCoroutine(coroutineB());
        Debug.Log("coroutineA running again");
    }

    IEnumerator coroutineB()
    {
        Debug.Log("coroutineB created");
        yield return new WaitForSeconds(2.5f);
        Debug.Log("coroutineB enables coroutineA to run");
    }
}

CoroBehaviour in C++:

#include "CoroBehaviour.h"
#include <iostream>

class ExampleClass : public CoroBehaviour
{
    void Start()
    {
        StartCoroutine(coroutineA());
    }

    CoroEnumerator coroutineA()
    {
        return [=](CoroPush& YieldReturn)
        {
            // wait for 1 second
            std::cout << "coroutineA created";
            YieldReturn(new WaitForSeconds(1.0f));
            YieldReturn(StartCoroutine(coroutineB()));
            std::cout << "coroutineA running again";
        };
    }

    CoroEnumerator coroutineB()
    {
        return [=](CoroPush& YieldReturn)
        {
            std::cout << "coroutineB created";
            YieldReturn(new WaitForSeconds(2.5f));
            std::cout << "coroutineB enables coroutineA to run";
        };
    }
}

Disclaimer

I didn't consider about performace, stability and security.
Please use this at your own risk.

And I have no plan to make this move forward.
Please do it yourself if you need more features.

Contact

exawon@gmail.com

About

Coroutine like Unity using C++ and boost coroutine2

License:MIT License


Languages

Language:C++ 100.0%