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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kanedatakeshi@gmx.de

    #1

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

    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_CompileStri ng()' followed by
    3) 'PyEval_EvalCod e()'

    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_SimpleFi le()', 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_AddMo dule') 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?

    Cheers!

    PiLLe

Working...