Original Post
Hi everyone! First i just want to warn you that this is a rather lengthy post, so please bear with me :) Ok,i've been able to export a Singleton class from my CPP program to python as an extension module. The class is just a test class i wrote to experiment with some things i want to do with my game. The class just has a static pointer to a CObject ( which a simple test class with just X and Y members ). The singleton is called CObjectManager which will be responsible for creating/destroying objects and things of that sort. Here is the code for this class: ( And yes, i know this isnt a "real" singleton, but that isnt the point ;) ) Now then, the CPP program will be responsible for this class for initialization and all of that, and the Python script should be able to access this class and use the FetchActiveObject() method to get the object stored. So the CPP program will create a CObject and store it in CObjectManager so that the Python script can get it and do things with it. Follow me so far ? Ok, all of the above works just fine for me, the python script can access CObjectManager and access the methods, HOWEVER , when python calls the method FetchActiveObject() it gets the value None! And ofcourse, since the extension module exposed to python is in a dll library, that is what i would expect, since the CObjectManager that python loads in the DLL wont have any value for its pointer to the stored CObject, since it has its own "instance" for that class. So my question is this: How can i share the CObject pointer that the CPP program creates with Python ? I understand if the above seems confusing to the reader of this post, but just let me know what im missing or is unclear about. NOTE: In the code listing above you might notice the extern int gMyInt; variable, i just put that in there to see if i could set that value in CPP, and if it had the same value in Python, but ofcourse it didnt, as i should have known ;) There, i hope you got enough information to help me on this ( and that its readable ;) ) Thanks for your time! PS: I should note that something i wanted to do before i even begain with exporting modules to Python, i wanted to create my CObject in the CPP program, and pass it as an argument to a function in python using this:
#ifndef __COBJECTMANAGER_H_INCLUDED__
#define __COBJECTMANAGER_H_INCLUDED__
#include "CObject.h"
extern int gMyInt;
class CObjectManager
{
private:
static CObject* m_pActiveObject;
CObjectManager() { }
~CObjectManager() { }
public:
static CObject* FetchActiveObject();
static void SetActiveObject(CObject* objPtr);
};
#endif __COBJECTMANAGER_H_INCLUDED__
// ... Assumes that pArgs tuple have be set with values
PyObject_CallObject( pFunc, pArgs );
However i couldnt find anyway to add a custom object to the argument list, using regular datatypes like int and such was easy with the python functions PyInt_FromLong() etc, but since all python functions like this use the PyObject, i relized that i had to somehow cast or convert my CObject into a PyObject, but i have no idea how to do that, and ive looked through all docs i could find but without any results, so if anyone knows how that should be done please let me know :)