|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-10-16 10:58:57
Normally, people want to write the "business logic" of their applications in
Python and save C++ for the speed-critical components. If you can afford to
do what you're asking about, you might consider writing your whole
application in Python and dispensing with C++ altogether. That said...
It isn't well-documented yet, but py_cpp can be used for this purpose. You
can use the py::Callback template to call methods on the objects you get
from Python, e.g. something like this should work:
// caveat: UNTESTED!
#include <py_cpp/pyptr.h>
#include <py_cpp/callback.h>
#include <py_cpp/py.h>
#include <Python.h>
int main()
{
try {
py::Ptr module(PyImport_ImportModule("weapons"));
const int strength = 10;
const char* manufacturer = "Vordon Empire";
py::Ptr a_blaster(py::Callback<py::Ptr>::call_method(
module.get(), "Blaster", strength, manufacturer));
py::Callback<void>::call_method(a_blaster.get(), "Fire");
int old_strength = py::Callback<int>::call_method(a_blaster.get(),
"get_strength");
py::Callback<void>::call_method(a_blaster.get(), "set_strength", 5);
}
catch(...)
{
}
}
I think that CXX (http://cxx.sourceforge.net) may be better at this job than
py_cpp, though I'm not at all sure.
Good luck,
Dave
----- Original Message -----
From: "Ted Milker" <tmilker_at_[hidden]>
> I've been following the C++/python sig mailing list and saw your
> posts about py_cpp. Reading the webpage, it exposes the C++
> classes you write to python, but can you recommend something for
> doing the opposite? I'd like to write a lot of the high level
> objects(NPC AI, items/weapons/etc) in Python and allow C++ to use
> those objects. That way I don't have to recompile the binary to
> modify the objects in the game world.
>
> Ted
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk