northwood-studios / NwPluginAPI

Official server-side plugin system for SCP: Secret Laboratory game.
GNU Lesser General Public License v3.0
81 stars 46 forks source link

Threading with SetRole causes crash #197

Closed wumtdev closed 1 year ago

wumtdev commented 1 year ago

Example:

[PluginEvent(ServerEventType.PlayerCoinFlip)]`
void OnPlayerCoinFlip(Player player, bool isTails)
{
    Thread thread = new Thread(ExampleTimer);
    player.SendBroadcast("Wait 5 seconds", 3, shouldClearPrevious: true);
    thread.Start(player);
}

public static void ExampleTimer(object obj)
{
    Thread.Sleep(3000);

    Player player = (Player)obj;

    player.SendBroadcast("Wait 2 seconds", 3, shouldClearPrevious: true);

    Thread.Sleep(2000);
    player.SetRole(RoleTypeId.Tutorial); // Crash
}

Thread from System.Threading

Server crashes(restarts) immediately after role setup without any messages about error

Sorry, if something wrong or it's my mistake :P

Misaka-ZeroTwo commented 1 year ago

Not a bug, you are not supposed to use Thread.Sleep, because that just kills the main gain thread. use MEC instead

wumtdev commented 1 year ago

Not a bug, you are not supposed to use Thread.Sleep, because that just kills the main gain thread. use MEC instead

Sorry, but what MEC mean? If you tell me, I'm sure I'll find the guide on the Internet...

moddedmcplayer commented 1 year ago

Not a bug, you are not supposed to use Thread.Sleep, because that just kills the main gain thread. use MEC instead

Sorry, but what MEC mean? If you tell me, I'm sure I'll find the guide on the Internet...

https://exiled-team.github.io/Web/docs/Plugins/MoreEffectiveCoroutines

wumtdev commented 1 year ago

Sorry, but what MEC mean? If you tell me, I'm sure I'll find the guide on the Internet...

https://exiled-team.github.io/Web/docs/Plugins/MoreEffectiveCoroutines

All works, thank you a lot! ❤️