MEC - SynapseSL/Synapse GitHub Wiki

MEC

MEC is the threading system used in SP:SL. It's commonly used to create delays in code execution to for example wait 2 - 3 seconds.

If you dont use the NuGet package, you will need to add the refrence UnityEngine.CoreModule.dll and Assembly-CSharp-firstpass.dll to your project dependencies. Both of these can be found in the main Server Directory

Timing.CallDelayed(float duration,Action action);

A method which does 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

Coroutines are a very useful tool if you need to repeat code. You can execute code every x seconds, with timed delays.

To create a a Coroutine create a method that returns an IEnumerator<float>. To start a Coroutine you can use the method: Timing.RunCoroutine(Coroutine method) Note: This Method will start the Coroutine and also give you an object CoroutineHandle For a delay inside the Code, you need to use yield return Timing.WaitForSeconds(float duration);

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 every 5 seconds. If you want to stop the Coroutine, you can use yield break; or store the Coroutine as List<CoroutineHandle>. Then you can kill the Coroutine with this call: 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 may need to 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");
            }
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️