UPDATE
I have found a much easier way to do this by simply setting up Visual Studio to use the simulator in debugging. Please see this thread for how to do that. This is actually preferable to simply launching the simulator when building, since it allows you to use debugging features like setting breakpoints in your code.
The Issue
When I was playing around with the SDK and using the C API in Visual Studio 2019, I started getting annoyed that every time I wanted to build the project I had to manually close the simulator if it was already running the project (this would cause an error in the building process) and then manually reopen it to test the changes. I spent the a decent amount of time finding a way to make Visual Studio do these actions for me, and now I want to share them with the rest of you, since this is the kind of thing that really streamlines the development process.
The Solution
Visual Studio has what are known as Build Events. These are categorized as Pre-Build, Pre-Link, and Post-Build. The first and last one of these are the ones that matter for this situation. To view these events, you can right-click the main project (the one that builds the DLL) and view its Properties, from there you can expand the section for Build Events.
The Pre-Build Event
This is the event that's going to make it so that, if the simulator is running, it will be closed before building the project. The reason we have to do this is because if the simulator is currently accessing the .pdx
folder, Visual Studio complains and errors out, since accessing that folder is part of its build process.
The easiest way I found to kill the simulator is with a batch file. Go to the place where you have installed the SDK, and go to the bin
folder. In here, make a new file called kill_simulator.bat
and copy-paste the following inside of it.
tasklist /FI "IMAGENAME eq PlaydateSimulator.exe" 2>NUL | find /I /N "PlaydateSimulator.exe">NUL
if "%ERRORLEVEL%"=="0" taskkill /f /im PlaydateSimulator.exe
Afterward, go to the Pre-Build Event in Visual Studio and paste this in the Command Line field:
cd $(PLAYDATE_SDK_PATH)\bin && kill_simulator.bat
The Post-Build Event
The easiest way I found to get Visual Studio to bring back the simulator after completing the entire build process is to call a powershell-specific command.
When you go to the Command Line field of the Post-Build Event, you're probably going to see a bunch of commands that had been auto-generated by CMake. Don't touch any of them, since this will most likely mess up the build process. Instead, click on the little arrow that lets you expand a drop-down menu and choose "Edit"
A window will open up where you can see all the contents of the Command Line field at the top, and enter some new ones. Find whatever the command is, put a new line under it, and paste this
powershell start-process $(PLAYDATE_SDK_PATH)\bin\PlaydateSimulator.exe
Click OK to save the Command Line and OK again to save the Properties
After this, you should be all good to go. Now whenever you go to build your project, the simulator will auto-close, and come back as soon as it's had a successful build. Because the simulator will default to reopening whatever .pdx
folder it was last running, you now have a really quick way to reload the simulator with your latest changes.
Happy programming!