1
\$\begingroup\$

I am developing a game in Python using Pygame. I am wondering whether there is a way to make changes to a currently running game by modifying the script (like Notch does in this clip).

For example, suppose I am doing a bit of testing and I want to increase my player jump height variable. I would like to simply go into the script while the game is running, change the value of the variable, and have the new value reflected in the current run. This would simplify the process of fine-tuning all my physics variables.

My IDE is Sublime Text (I know, it's actually just a glorified text editor, but you can run code from it).

Is there any way to do this? For example, with a particular IDE?

\$\endgroup\$
  • 1
    \$\begingroup\$ After taking a look at the video, I think what you're looking for is something like "debug, edit and continue". There is apparently something like this in Eclipse/Java and I think MS Visual Studio supports it at least for C++ (I would not be surprised that it works with c# too). That being said, according to this question on SO there is something like that for PyDev/Eclipse. I have not tested it myself, but that could help you get the ball rolling. \$\endgroup\$ – Alexandre Vaillancourt Sep 23 '17 at 23:59
2
\$\begingroup\$

This answer will be short because I'm on my phone.


I haven't watched the video from your link, but a way you could accomplish something like this this for your code's parameters is:

  1. isolate all constant values of your game and somehow centralize them in a dictionary;
  2. put all the values in a separate text file, and make sure you can retrieve them with a text key (this can be a JSON file, an XML file or simply something that looks like an INI file, or whatever you fancy);
  3. implement a load/reload shorcut key in your game which will order it to re-populate the centralized dictionary based on the values in the text file.

This way you can start your game, play a bit, tweak a value, hit the reload key and you have your tweaked behaviour without the need to restart the game.

Going one step further: your game could detect that the file has changed (based on the OS' information of the last modification date/time of the file) and reload it for you.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you for the answer Alexandre! It pointed me in the right direction. I discovered that Python has a built-in function, importlib.reload() which can be used to reload modules without leaving the Python interpreter. Given that all my physics variables were set in my settings.py module, I simply mapped a press of the R key to a call on importlib.reload(settings). So now simply pressing the R key will refresh my current game session to reflect the new variables. I will accept your answer, but you may want to edit my solution into it. Thanks again! \$\endgroup\$ – GoldenGremlin Sep 24 '17 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.