Coder Social home page Coder Social logo

aurodev / coroutine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ellpeck/coroutine

1.0 0.0 0.0 73 KB

A simple implementation of Unity's Coroutines to be used for any C# project

Home Page: https://www.nuget.org/packages/Coroutine

License: MIT License

C# 100.00%

coroutine's Introduction

The Coroutine logo

Coroutine is a simple implementation of Unity's Coroutines to be used for any C# project

Features

Coroutine adds the ability to run coroutines. Coroutines are methods that run in parallel to the rest of the application through the use of an Enumerator. This allows for the coroutine to pause execution using the yield return statement.

There are two predefined ways to pause a coroutine:

  • Waiting for a certain amount of seconds to have passed
  • Waiting for a certain custom event to occur

Additionally, Coroutine provides the following features:

  • Creation of custom events to wait for
  • No multi-threading, which allows for any kind of process to be executed in a coroutine, including rendering
  • Thread-safety, which allows for coroutines to be started from different threads

How to Use

Setting up the CoroutineHandler

The CoroutineHandler is the place where coroutines get executed. For this to occur, the Tick method needs to be called continuously. The Tick method takes a single parameter which represents the amount of time since the last time it was called. It can either be called in your application's existing update loop or as follows.

var lastTime = DateTime.Now;
while (true) {
    var currTime = DateTime.Now;
    CoroutineHandler.Tick(currTime - lastTime);
    lastTime = currTime;
    Thread.Sleep(1);
}

Creating a Coroutine

To create a coroutine, simply create a method with the return type IEnumerator<Wait>. Then, you can use yield return to cause the coroutine to wait at any point:

private static IEnumerator<Wait> WaitSeconds() {
    Console.WriteLine("First thing " + DateTime.Now);
    yield return new Wait(1);
    Console.WriteLine("After 1 second " + DateTime.Now);
    yield return new Wait(5);
    Console.WriteLine("After 5 seconds " + DateTime.Now);
    yield return new Wait(10);
    Console.WriteLine("After 10 seconds " + DateTime.Now);
}

Starting a Coroutine

To start a coroutine, simply call Start:

CoroutineHandler.Start(WaitSeconds());

Using Events

To use an event, an Event instance first needs to be created. When not overriding any equality operators, only a single instance of each event should be used.

private static readonly Event TestEvent = new Event();

Waiting for an event in a coroutine works as follows:

private static IEnumerator<Wait> WaitForTestEvent() {
    yield return new Wait(TestEvent);
    Console.WriteLine("Test event received");
}

Of course, having time-based waits and event-based waits in the same coroutine is also supported.

To actually cause the event to be raised, causing all currently waiting coroutines to be continued, simply call RaiseEvent:

CoroutineHandler.RaiseEvent(TestEvent);

Additional Examples

For additional examples, take a look at the Example class.

coroutine's People

Contributors

ellpeck avatar zaafar avatar

Stargazers

Roman 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.