playdate.gameWillTerminate is called on launch

In SDK 2.0.1 on Mac, trying to implement save/load state, I've added a save call in gameWillTerminate, but in the Simulator this seems to be getting called when the game is launched but before my mainGameSetup function is called.

Running the below file in the simulator shows the following in the console:
gameWillTerminate
setup

If you then exit the app from the menu, you will then see another gameWillTerminate message

Not sure if its me doing something odd.
And for info, running from Visual Studio Code.

-- Common CoreLibs imports.
import "CoreLibs/graphics"


-- Use common shorthands for playdate code
local gfx <const> = playdate.graphics
local display <const> = playdate.display

gfx.setBackgroundColor(gfx.kColorWhite)


function myGameSetUp()
    print( "setup")
end

myGameSetUp()

function playdate.gameWillTerminate()
    print( "gameWillTerminate")
end

function playdate.gameWillSleep()
    print( "gameWillSleep")
    save()
end

function playdate.update()
end

Are you sure it's not being called on the prior instance of your game? E.G. You load your game in the Simulator, do some debugging, rebuild and relaunch your game and the old instance of your game is quit while the new instance is loaded so the gameWillTerminate handler is called but on the older instance of the game.

2 Likes

I don't think so.

I've quit the app (menu home), get a "gameWillTerminate" log message, then restarted the debugger and then get the "gameWillTerminate" followed by "setup" log messges.

I've also quit the app, and closed the simulator, then restarted the debugging session and again get the gameWillTerminate followed by "setup" log messages.

Am I quitting in the right way?

Are you using the VSCode debugging extension? I can reproduce this, but only when debugging a game, not if I normally run it. EDIT: looks like you are.

Yes indeed I am - and indeed it doesn't seem to happen on when running on device but does make debugging my save state a little tricky :wink:

I cannot repro this in Nova on Mac, wither whilst debugging or not.

I see the following:

Launching game from Nova:

  1. init

Relaunching from Nova

  • old game: gameWillTerminate
  • new game: init (only line visible in log for new game)

Pressing Cmd+R in Simulator:

  1. gameWillTerminate
  2. init

Here's the code I used to help reproduce:

print("init")

function playdate.gameWillTerminate()
	print("gameWillTerminate")
end

function playdate.update()
end

That is interesting. It makes sense since the Mac debugger was developed interacting with Nova, not VSCode, and I've found a couple differences in how they behave. That said, I believe I have a solution for this issue with VSCode and plan on getting it fixed today.

1 Like

Thats excellent news - thanks very much!