Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
GrafDimenzio edited this page Jul 5, 2020 · 6 revisions

MEC

MEC is a really helpfully tool for developing because its allow you easily to do delays without Thread.Sleep() which pause the entire Server(dont use it)

In order to use it you must add UnityEngine.CoreModule.dll and Assembly-CSharp-firstpass.dll to your project dependencies,which you can find both in your server files inside the same directory than the Assembly-CSharp.dll and add using MEC to your references.

Timing.CallDelayed(float,Action);

A Method which do an Action delayed.

Example:

//Set the Player health after 5 seconds to 200
Timing.CallDelayed(5f,() => player.Health = 200);

//or if you want to do more like an check
Timing.CallDelayed(5f, () =>
            {
                if (player.Health > 200)
                    player.Health = 200;
            }); //Set the Player Health after 5 Seconds to 200 when he has over 200 Health

Coroutines

They are really useful to do loops which should loop every x seconds or just do many actions in specified order with a delay between them.

To create a Method which can be a Coroutine it must return IEnumerator<float>.To start a Coroutine you can use Timing.RunCoroutine(Coroutine methode) Note this Method will start the method and also give you the object CoroutineHandle and to do a delay you must use yield return Timing.WaitForSeconds(float);

Example to Start a Coroutine:

using Synapse;
using MEC;
using System.Collections.Generic;

namespace Example
{
    public class Example : Plugin
    {
        public override string GetName => "Example";

        public override void OnEnable()
        {
            Timing.RunCoroutine(ExampleCoroutine());
        }

        private IEnumerator<float> ExampleCoroutine()
        {
            Log.Info("Hello in 5 Seconds i will say Hello again!");

            yield return Timing.WaitForSeconds(5f);

            Log.Info("as i sayed Hello again! Now i will start a loop to say Hello every 5 Second!");

            for (; ; )
            {
                yield return Timing.WaitForSeconds(5f);
                Log.Info("Hello");
            }
        }
    }
}

As you see this Log.Info("Hello"); will now be called each 5 seconds.If we want to stop that we can use yield break; or store the Coroutine in List<CoroutineHandle> when we start it and Kill it later with Timing.KillCoroutines(List<CoroutineHandle>);

Example for yield break;:

using Synapse;
using MEC;
using System.Collections.Generic;

namespace Example
{
    public class Example : Plugin
    {
        public override string GetName => "Example";

        public override void OnEnable()
        {
            Timing.RunCoroutine(ExampleCoroutine());
        }

        private IEnumerator<float> ExampleCoroutine()
        {
            Log.Info("Hello in 5 Seconds i will say Hello again!");

            yield return Timing.WaitForSeconds(5f);

            Log.Info("as i sayed Hello again! Now i will start a loop to say Hello every 5 Second! if you dont use the port 7777");

            if (ServerStatic.ServerPort == 7777) yield break;

            for (; ; )
            {
                yield return Timing.WaitForSeconds(5f);
                Log.Info("Hello");
            }
        }
    }
}

Example for Timing.KillCoroutines(List<CoroutineHandle>); :

using Synapse;
using MEC;
using System.Collections.Generic;

namespace Example
{
    public class Example : Plugin
    {
        private List<CoroutineHandle> CoroutineHandles = new List<CoroutineHandle>();

        public override string GetName => "Example";

        public override void OnEnable()
        {
            CoroutineHandles.Add(Timing.RunCoroutine(ExampleCoroutine()));

            //kills the Coroutine(just a basic Example you can connect this to an Event like Roundrestart)
            Timing.CallDelayed(30f,() => Timing.KillCoroutines(CoroutineHandles));
        }

        private IEnumerator<float> ExampleCoroutine()
        {
            Log.Info("Hello in 5 Seconds i will say Hello again!");

            yield return Timing.WaitForSeconds(5f);

            Log.Info("as i sayed Hello again! Now i will start a loop to say Hello every 5 Second!");

            for (; ; )
            {
                yield return Timing.WaitForSeconds(5f);
                Log.Info("Hello");
            }
        }
    }
}
Clone this wiki locally