Original Post
Hello! I'm trying to integrate Python into my application using boost.python. But things aren't going well. There are not many (none?) practical tutorials out there covering my problem. There's the boost.python own tutorial to get started but that's it! My problem is that I don't just want to expose c++ classes but also expose instances of these classes created in the c++ world. In many cases I just want to control objects in python, not create them. So I thought I'd get help from you guys here and build some practical examples and eventually gather all information into a tutorial/article here on gamedev. So, basically what I want to do is: 1. Write a class myClass in c++ 2. Make an instance of myClass in c++ 3. Expose myClass to python in a module called Game 4. Make an attribute to Game which holds a reference to the instance of myClass Im familiar with the boost.python expose/embed interface. But Im not sure how to use it properly according to my needs. This is some of my conditions if it can be done: * My game engine is object oriented * I'd like to put all expose-to-python-routines in class methods (like a ScriptManager class) * Avoid global/static functions and variables in my c++ framework * Be able to in runtime add/remove object-references in the Game module Lets 'abstract' the problem to a simple concrete structure:
class Car // to be exposed to python
{
public:
void drive();
};
class ScriptManager
{
public:
ScriptManager()
{
// create a python module called 'Game' here..?
}
void addReferenceToModule(Car* reference)
{
// add reference to the 'Game' module.
// this reference should be named 'myCar' in the 'Game' module's global scope
}
runScript()
{
// run script 'testScript.py' here...?
}
};
int main()
{
SM = new ScriptManager();
car = new Car();
SM->addReferenceToModule(car);
SM->runScript();
return 0;
}
How should I code these empty methods? :)
When testScript.py is run I should be able to do:
import Game
car = Game.myCar #get the instance which is created in the c++ world
car.drive()
Thank you for your time and help! EDIT: found this link http://forums.indiegamer.com/showthread.php?t=14280 helped me [Edited by - mrmclovin on August 18, 2009 10:01:46 AM]