Jump to content

  • Log In with Google      Sign In   
  • Create Account

Editing a Python Script during Runtime?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 blueskies9041   Members   -  Reputation: 138

Like
0Likes
Like

Posted 18 March 2014 - 04:18 PM

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? 

 

 



Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6325

Like
1Likes
Like

Posted 18 March 2014 - 04:27 PM

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


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!

#3 blueskies9041   Members   -  Reputation: 138

Like
0Likes
Like

Posted 18 March 2014 - 04:41 PM

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.



#4 SiCrane   Moderators   -  Reputation: 9676

Like
1Likes
Like

Posted 18 March 2014 - 08:45 PM

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



#5 ColinDuquesnoy   Members   -  Reputation: 1151

Like
1Likes
Like

Posted 19 March 2014 - 09:19 AM

 

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

 

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS