
Originally Posted by
ManuTOO
This :
"boost:: python::globals["gameState"] = &gameState;"
is not C++ ...

Maybe it's C# ..?
This is actually perfectly running code from Tony's and my C++ sources...
After a quick look into boost:: python docs one can come up with a solution like this (you would pack this into modules and stuff for production code):
Code:
#include <iostream>
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
struct World
{
std::string msg;
void greet()
{
std::cout << msg << std::endl;
}
};
int main (int argc, char * const argv[])
{
try
{
// Initialize context
Py_Initialize();
// Retrieve the main module.
object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
// Retrieve the main module's namespace
object main_namespace(main_module.attr("__dict__"));
// Expose World class
main_namespace["World"] = class_<World>("World")
.def_readwrite("msg", &World::msg)
.def("greet", &World::greet);
// Expose World instance
World world;
main_namespace["world"] = ptr(&world);
// Run script
handle<> ignored(( PyRun_String("world.msg = 'Hello World!' \n"
"world.greet() \n",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ) ));
// Finalize context
Py_Finalize();
}
catch( error_already_set )
{
PyErr_Print();
}
return 0;
}
Edit: I just saw that you want to pass the structure into python. For that you could do the following (replace from "// Expose World instance"):
Code:
// Define python function
handle<> ignored(( PyRun_String("def use_world(world): \n"
" world.msg = 'Hello World!' \n"
" world.greet() \n",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ) ));
// Create World instance
World world;
// Get python function...
object use_world = main_namespace["use_world"];
// ...and call it
use_world(world);
cheers,
Timo