
Hi, I have a game that currently integrates the python interpreter using the Python C API. I would like to expose various C++ interfaces & functions to python, but I want to do so without having to create a separate DLL, as state needs to be modified in the actual core engine of the game as the user calls C++ functions from Python. For example, say I have a C++ function called CompleteQuest(). In Python, we would script a quest for my game and at some point call CompleteQuest() from python. This should map directly to a function in the executable (game). Is there a way to do this? Thanks.

on Thu Feb 07 2008, "Robert Dailey"
Hi,
I have a game that currently integrates the python interpreter using the Python C API. I would like to expose various C++ interfaces & functions to python, but I want to do so without having to create a separate DLL, as state needs to be modified in the actual core engine of the game as the user calls C++ functions from Python.
That really doesn't have much bearing on whether your extension module is in a DLL or not. You can always link your DLL extension module directly to your application as well as letting Python load it.
For example, say I have a C++ function called CompleteQuest(). In Python, we would script a quest for my game and at some point call CompleteQuest() from python. This should map directly to a function in the executable (game).
Is there a way to do this? Thanks.
Yes, it's called "embedding Python;" see http://www.boost.org/libs/python/doc/building.html HTH, -- Dave Abrahams Boost Consulting http://boost-consulting.com

On Feb 8, 2008 9:06 AM, David Abrahams
You can always link your DLL extension module directly to your application as well as letting Python load it.
Explain please. Also, I don't want to use a DLL, as I already mentioned. I want there to be my python scripts and my game executable, that's it. I don't want a DLL and a game executable. I want this to remain simple. As I originally asked, is there a way to do this? Do I load my exe as a pyd module or what?

Robert Dailey wrote:
I don't want to use a DLL, as I already mentioned. I want there to be my python scripts and my game executable, that's it. I don't want a DLL and a game executable. I want this to remain simple. As I originally asked, is there a way to do this? Do I load my exe as a pyd module or what?
I recommend you look at py2exe: http://www.py2exe.org/ We built one product with Python scripts at the top level. All our custom C++ logic (notably the main loop) is in a set of .pyd Python extensions called by those scripts. But all, including the Python interpreter, is very neatly packaged by py2exe so that none of those details are visible to the user: the install footprint is just a big .exe and our data files.

Robert Dailey wrote:
On Feb 8, 2008 9:06 AM, David Abrahams
mailto:dave@boost-consulting.com> wrote: You can always link your DLL extension module directly to your application as well as letting Python load it.
Explain please. Also, I don't want to use a DLL, as I already mentioned. I want there to be my python scripts and my game executable, that's it. I don't want a DLL and a game executable. I want this to remain simple. As I originally asked, is there a way to do this? Do I load my exe as a pyd module or what?
You can try the example "embedding" in Boost.Python: http://svn.boost.org/svn/boost/trunk/libs/python/example/quickstart/embeddin... if you compile (and link to) boost as a static library, you will get only an EXE and a python script, no extra DLL.

on Sat Feb 09 2008, "Robert Dailey"
On Feb 8, 2008 9:06 AM, David Abrahams
wrote: You can always link your DLL extension module directly to your application as well as letting Python load it.
Explain please. Also, I don't want to use a DLL, as I already mentioned.
It doesn't seem like an explanation would be worth much if your desire to avoid a DLL isn't based on your state-sharing needs (as you implied it was in your first posting).
I want there to be my python scripts and my game executable, that's it. I don't want a DLL and a game executable. I want this to remain simple. As I originally asked, is there a way to do this? Do I load my exe as a pyd module or what?
In my original reply I referred you to http://www.boost.org/libs/python/doc/building.html where "embedding" is described. If that doesn't tell you what you need to know, please ask a more detailed question so I know how to help you. -- Dave Abrahams Boost Consulting http://boost-consulting.com

On Feb 11, 2008 10:15 AM, David Abrahams
In my original reply I referred you to http://www.boost.org/libs/python/doc/building.html where "embedding" is described. If that doesn't tell you what you need to know, please ask a more detailed question so I know how to help you.
Yes, their description of embedding is exactly what I'm trying to do. Sorry for the confusion. However, I didn't find any code examples on that page. Do I just use Boost.Python to expose my C++ functions and call them from the script without importing any packages? Thanks for your response.

On Feb 11, 2008 10:31 AM, Robert Dailey
Yes, their description of embedding is exactly what I'm trying to do. Sorry for the confusion. However, I didn't find any code examples on that page. Do I just use Boost.Python to expose my C++ functions and call them from the script without importing any packages?
Thanks for your response.
Never mind, I actually found embedded.cpp inside of the libs/python/example/quickstart folder. It's a bit hard to follow, but hopefully after an hour or two I'll have an idea of how this process works. Thanks again guys.

Wouldn't this work instead of calling PyImport_AppendInittab(): static void DoFoo() { } using namespace boost::python; BOOST_PYTHON_MODULE(pythontest) { def( "DoFoo", DoFoo ); } void SetupPython() { object import( "pythontest" ); } Notice how I'm importing the "pythontest" module. I want to use Boost.Pythonwhere ever I can, and in the embedding.cpp example they use the Python API directly to import the embedded module. Thanks.

Slight correction. I meant:
object mainmod = import( "pythontest" );
I find now that this does not work and actually causes an access violation
in my application.
On Feb 11, 2008 12:54 PM, Robert Dailey
Wouldn't this work instead of calling PyImport_AppendInittab():
static void DoFoo() { }
using namespace boost::python;
BOOST_PYTHON_MODULE(pythontest) { def( "DoFoo", DoFoo ); }
void SetupPython() { object import( "pythontest" ); }
Notice how I'm importing the "pythontest" module. I want to use Boost.Python where ever I can, and in the embedding.cpp example they use the Python API directly to import the embedded module.
Thanks.
participants (4)
-
David Abrahams
-
gchen
-
Nat Goodspeed
-
Robert Dailey