Carpet

Carpet

2M Downloads

[Suggestion] Game freeze when nobody online (for server)

SpiritedAwayCN opened this issue · 11 comments

commented

As mentioned in the title, could you add a new feature, that game will be frozen when every player or bot is offline ,and will run normally when anyone joins the game?

And this feature seems to only work in server env.

Thank you!

commented

Something like (untested, just wrote it here):

__on_player_disconnects(p,r) ->
(
  if (length(player('all')) == 0, run('tick freeze'))
);

__on_player_connects(p) ->
(
  if (length(player('all')) == 1, run('tick freeze'))
);

And of course make it autoload and stay loaded.

commented

A more general solution (where n is the minimum number of players that must be online) would be:

__on_player_disconnects(p,r) ->
(
  if (length(players(*) < n, run('tick freeze'));
);

__on_player_connects(p)
(
  if (length(players(*) >= n, run('tick freeze'));
);
commented

That wouldn't work. That would make it cycle randomly, since tick freeze would get called every time a player leaves as long as there are less than n, so at n-1 it would unfreeze, n-2 freeze, etc (same for join).

Also why would you want to have the game frozen with players online?

(the solution to your case would be when == n-1 and when == n I think)

commented

This would be definitely possible with a Scarpet app instead. Check every time __on_player_disconnects if there is no one left then freeze, check __on_player_connects and if there were no one unfreeze.

commented

@altrisi the player(*) only lists players in the current dimension (so it would also freeze if nobody is in overworld), instead use player('all') (need quotes too)
i also realized that very late that the * is only same dimension

commented

Ok, then close, yeah? also add a __config()->{'stay_loaded'->'true'} so it loads after server restarts.

commented

Ok, then close, yeah? also add a __config()->{'stay_loaded'->'true'} so it loads after server restarts.

Maybe there's another problem: game should be immediately freezed when server starts completely. Otherwise, game is still running and will be freezed when a player joins the game.

commented

For that you can just put run('tick freeze') inside the script, and it will be run when it loads (not inside any event)

commented

Thanks for everyone. This issue can be closed now.

commented

It would appear that run('tick freeze') is not being run inside __on_player_disconnects. I have confirmed this by doing /tick health and comparing the output with when ticking is frozen.

Manually running /tick freeze from the server console, and connecting does unfreeze ticks though, so this is only something that happens when disconnecting. Any ideas?

commented

Ok figured it out, the event gets caller before the number of players get updated, which means the following is needed:

__on_player_disconnects(p,r) ->
(
  if (length(player('all')) == 1, run('tick freeze'))
);