Pause callbacks

User avatar
Wuzzy
Member
 
Posts: 2161
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Pause callbacks

by Wuzzy » Tue Jul 15, 2014 15:14

I have a suggestion for two new Lua API callbacks for when the game gets paused and unpaused.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_pause(func)

This callback shall call func, when the player pauses the game by opening the pause menu. This callback should only be called in singleplayer mode.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_unpause(func)

This callback shall call func, when the player unpauses/continues the game by closing the pause menu. This callback should only be called in singleplayer mode.

For both callbacks: func takes one argument: player. This is the player object of the player who paused the game. Yeah, I know, it’s just one player but I think of it as a convenience feature. The return value of func is discarded.

The idea of these callbacks is (for example) for mods who need to rely on time. Normally, a modder would want to freeze all timers when the game is paused and unfreeze them when the game continues. Weird results can happen if the timers just move on while the game is “officially” paused.
 

User avatar
Krock
Member
 
Posts: 3598
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker

Re: Pause callbacks

by Krock » Tue Jul 15, 2014 15:30

Other suggestion: Freeze all timers and globalsteps when the game is paused in singleplayer. This would make such a callback useless.
Newest Win32 builds - Find a mod - All my mods
ALL YOUR DONATION ARE BELONG TO PARAMAT (Please support him and Minetest)
New DuckDuckGo !bang: !mtmod <keyword here>
 

User avatar
BrandonReese
Member
 
Posts: 836
Joined: Wed Sep 12, 2012 00:44
GitHub: bremaweb
IRC: BrandonReese
In-game: BrandonReese

Re: Pause callbacks

by BrandonReese » Tue Jul 15, 2014 15:44

Krock wrote:Other suggestion: Freeze all timers and globalsteps when the game is paused in singleplayer. This would make such a callback useless.


I believe everything freezes when you bring up the menu in single player
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

Re: Pause callbacks

by prestidigitator » Tue Jul 15, 2014 22:04

I think it would be useful to think of the thing that is being paused as the player, not the game. Essentially, an "AFK" mode. That would be useful for mods to know about outside of singleplayer mode too. So I suggest making the callback universal (the callback could call minetest.is_singleplayer() to filter for interesting cases if desired). Also, any mod that wants to know about a pause is almost certain to want to know about the corresponding unpause, so it might make sense as a single callback with a (e.g. boolean) parameter rather than two callbacks.
 

User avatar
Wuzzy
Member
 
Posts: 2161
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: Pause callbacks

by Wuzzy » Wed Jul 16, 2014 21:18

The problem lies partly in minetest.after.
I don’t know if minetest.after timers freeze when the game pauses, but I do know that all minetest.after timers are gone when the server shuts down.
I worked around this issue in my Player Effects by remembering the start times of the timers and storing everything to file (including shutdown time) when the server shuts down. When the server reboots, the times are loaded again. But! They are not immediately started again, they are started again with minetest.after when the player rejoined.
Kind of hacky, but it works fine so far. The only bug I encountered so far is the thing with the pause. Player Effects is completely unaware of any pause.
I do not think that it is a good idea to change the behaviour of minetest.after in that way. This may break some mods, like Player Effects. Instead, I would suggest to add funcitonality to minetest.after.
Like stopping on pause (default=false) or deletion on shutdown (default=true). I chose the defaults from current behaviour.

I also use os.time() and os.difftime() to compare times. Now this doesn’t work anymore, since the OS time happily continiues in the pause. If Minetest would provide better timers, this may be a much cleaner way to do, I agree.

But still, I think there may be other use cases of such callbacks, for example when you want to back up some data or whatever I can’t think of yet.

prestidigitator wrote:Also, any mod that wants to know about a pause is almost certain to want to know about the corresponding unpause, so it might make sense as a single callback with a (e.g. boolean) parameter rather than two callbacks.

Good idea!
 


Return to Minetest Features

Who is online

Users browsing this forum: Google [Bot] and 8 guests

cron