By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,585 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,585 IT Pros & Developers. It's quick & easy.

Creating Modules/Namespaces from C/C++ for better class/function encapsulation/cleanup

P: n/a
Hi folks!

The problem is the following:

I use Python 2.1 embedded in a C++ game engine. Some of the engines
complexer objects (AI, game logic, etc.) use python classes that act as
an extension of those "parent" C++-classes.

The python classes are currently loaded on demand by their "parent"
C++-instances, say if an enemy gets activated and starts running. I do
that by:
1.) reading the needed python script (dedicated to that enemy class)
into a string and then
2) calling 'Py_CompileString()' followed by
3) 'PyEval_EvalCode()'

Then I have access to all class(es) defined in the python script
implementing all the needed methods for my enemy.
It looks a bit tedious than just calling e.g. 'PyRun_SimpleFile()', but
I thought, that PyEval_EvalCode() could provide me with means to force
a local and global namespace on the interpreted code.

Problem and Question:
To get rid of ALL code/class definitions/etc. defined when I loaded and
interpreted the enemy's python script, I want the python interpreter to
force a namespace or module context on everything associated with my
enemy, so I can be sure I cleaned up everything that is related to the
previous loading operation. I tried to use a new module (e.g. calling
'PyImport_AddModule') as my local namespace, but the script couldn't
find any built-in functions or even other classes defined in the very
same script file anymore when I did that.

Any idea how to do that properly?



Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.