Editing a Python Script during Runtime?

Started by
3 comments, last by renega_666 10 years, 1 month ago

So I was tasked at my college to embed Python into their framework - which provides all the necessary things to make a basic 2D game (sprites, window context, etc...).

So I successfully "embedded" Python 2.7 into their framework, and I can now call Python functions from C++ (using Visual Studio 2010, if that matters) and C++ Functions from Python. The python interpreter is setup properly, and I have decent Python debugging outputting to my console.

The last bit of the assignment says that they want me to figure out how to reload Python Scripts during application run-time, currently I can edit a script and see the changes if I close the app and open it up again (don't need to recompile).

How do I make C++/VS2010 detect the changes to my script during run time?

Advertisement

If you only need to reload scripts during runtime you could just toss in a button that reloads the script for you. (or opens a file dialog where you can select new scripts to load), or throw in a console where you can enter your own python code and have it execute immediatly.

If you need to detect when a file changes you can look here: http://stackoverflow.com/questions/931093/how-do-i-make-my-program-watch-for-file-modification-in-c

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

What does the C++ code for " Reloading " a python script look like? I thought I had it figured out, in my games update loop I just re-assigned the PyObject * that was storing the handle to the script the same values ( handle contains the filepath of the script, and the name of the function im trying to call from it), I don't have to recompile to see the changes - I don't see why just opening the script & saving it isn't enough.

How did you load it in the first place? If you imported it as a module then you can reload it with PyImport_ReloadModule().

What does the C++ code for " Reloading " a python script look like?

The python C api has a function just for that: PyImport_ReloadModule

This topic is closed to new replies.

Advertisement