3

In the game I'm making I'd like to be able to override specific C++ functions with a python functions, as seamlessly as possible. The idea would be to allow quick coding of some functions in an interpreted language for prototyping / modding etc

Ideally I'd like to have a structure behaving like this:

In my C++ source code:

class WorldActions
{
public:

    //example of a method with a default implementation:

    virtual Coordinates respawnLocation(int event) const
    {
        if(event == 0)
            return Coordinates(0,0);
        else
            return Coordinates(1,0);
    }
}

Within a python file:

def respawnLocationOverride(worldActionInstance, event):
    coords = Coordinates()
    coords.x = 1
    return coords

At some point in the C++ engine I dynamically load the python method "respawnLocationOverride" which will effectively replace WorldActions::respawnLocation, this could be done through a special derived class like this :

class WorldActionsFromPy : public WorldActions
{
public:
    virtual Coordinates respawnLocation(int event) const
    {
        if(_something == NULL)
        {
         return WorldActions::respawnLocation(event);
        }
        else
        {
        return _something->callPythonMethod("respawnLocationOverride", this, event);
        }
    }
}

What kind of architecture would you recommend for me to be able to do that as simply as possible? I know I can use swig to generate all the python classes, but I have no idea how I would be able to re-import the python code within a .py file into C++ code

thanks!

lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • 2
    For something as extensive as this, I would look into Boost for embedded Python: http://stackoverflow.com/a/982670/4131059 You should be able to load and then reload Python modules as necessary using `importlib`. – Alex Huszagh Dec 21 '15 at 15:44
  • Does this mean that both the export of thé c++ code and thé import of the python code are required to both be done via boost? Or could I just use it for half ? – lezebulon Dec 21 '15 at 19:40
  • Nah, you can actually embed Python inside C++. Or you can export C++ to Python. Boost is quite variable in what it can do, and so if you would like to merely call dynamic Python code from C++, you can do so. Or, you can expose C++ classes to Python, and then call them from Python using importlib to re-load a given module. – Alex Huszagh Dec 21 '15 at 19:42
  • In your case, you obviously would like to override a given function with an embedded piece of Python code for quick prototyping, so probably the best tutorial would be here: http://members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html If this seems productive, I can write a longer answer below. – Alex Huszagh Dec 21 '15 at 19:47
  • hi ! thanks for the help I've managed to use swig to create a .pyd module that wraps my existing class in Python, so it works well from python, and I can write custom python functions. Now how can I "import" these functions in python and for instance transform my "Coordinate" C++ instance into a "Coordinate" python object ? thanks! – lezebulon Dec 21 '15 at 22:19

0 Answers0