Synapse
Search…
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
All the Functions are still the same but this was written for Synapse 1 so some parts of the Code regarding to Synapse does no longer work
If you dont use the NuGet package, you will need to add the reference 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:
1
//Set the Player health after 5 seconds to 200
2
Timing.CallDelayed(5f,() => player.Health = 200);
3
4
//or if you want to do more like an check
5
Timing.CallDelayed(5f, () =>
6
{
7
if (player.Health > 200)
8
player.Health = 200;
9
}); //Set the Player Health after 5 Seconds to 200 when he has over 200 Health
Copied!

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:

1
using Synapse;
2
using MEC;
3
using System.Collections.Generic;
4
5
namespace Example
6
{
7
public class Example : Plugin
8
{
9
public override string GetName => "Example";
10
11
public override void OnEnable()
12
{
13
Timing.RunCoroutine(ExampleCoroutine());
14
}
15
16
private IEnumerator<float> ExampleCoroutine()
17
{
18
Log.Info("Hello in 5 Seconds i will say Hello again!");
19
20
yield return Timing.WaitForSeconds(5f);
21
22
Log.Info("as i sayed Hello again! Now i will start a loop to say Hello every 5 Second!");
23
24
for (; ; )
25
{
26
yield return Timing.WaitForSeconds(5f);
27
Log.Info("Hello");
28
}
29
}
30
}
31
}
Copied!
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;

1
using Synapse;
2
using MEC;
3
using System.Collections.Generic;
4
5
namespace Example
6
{
7
public class Example : Plugin
8
{
9
public override string GetName => "Example";
10
11
public override void OnEnable()
12
{
13
Timing.RunCoroutine(ExampleCoroutine());
14
}
15
16
private IEnumerator<float> ExampleCoroutine()
17
{
18
Log.Info("Hello in 5 Seconds i will say Hello again!");
19
20
yield return Timing.WaitForSeconds(5f);
21
22
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");
23
24
if (ServerStatic.ServerPort == 7777) yield break;
25
26
for (; ; )
27
{
28
yield return Timing.WaitForSeconds(5f);
29
Log.Info("Hello");
30
}
31
}
32
}
33
}
Copied!

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

1
using Synapse;
2
using MEC;
3
using System.Collections.Generic;
4
5
namespace Example
6
{
7
public class Example : Plugin
8
{
9
private List<CoroutineHandle> CoroutineHandles = new List<CoroutineHandle>();
10
11
public override string GetName => "Example";
12
13
public override void OnEnable()
14
{
15
CoroutineHandles.Add(Timing.RunCoroutine(ExampleCoroutine()));
16
17
//Kills the Coroutine (Just a basic example you may need to connect this to an event like RoundRestart)
18
Timing.CallDelayed(30f,() => Timing.KillCoroutines(CoroutineHandles));
19
}
20
21
private IEnumerator<float> ExampleCoroutine()
22
{
23
Log.Info("Hello in 5 Seconds i will say Hello again!");
24
25
yield return Timing.WaitForSeconds(5f);
26
27
Log.Info("as i sayed Hello again! Now i will start a loop to say Hello every 5 Second!");
28
29
for (; ; )
30
{
31
yield return Timing.WaitForSeconds(5f);
32
Log.Info("Hello");
33
}
34
}
35
}
36
}
Copied!