Skip to content

Coroutine like Unity using C++ and boost coroutine2

License

Notifications You must be signed in to change notification settings

CodeNewWorlds/CoroBehaviour

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

[email protected]

About

Coroutine like Unity using C++ and boost coroutine2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%