c层创建的对象,python获取为空
这里为什么c层使用Init创建 agentMgr 对象,python层使用CreateAgent时,agentMgr 为NULL
class CAgent
{
public:
CAgent() {};
~CAgent() {};
};
class CAgentManager
{
public:
CAgentManager() {};
~CAgentManager() {};
CAgent* CreateAgent();
};
static CAgentManager* agentMgr = nullptr;
static void Init()
{
agentMgr = new CAgentManager();
cout << "111111111111111 Init " << agentMgr<<endl;
}
CAgent* CreateAgent()
{
cout << "222222222222222 CreateAgent " << agentMgr;
CAgent* agent = agentMgr->CreateAgent();
return agent;
}
// 采用pybind11导出pyd文件进行调用
PYBIND11_MODULE(C_monster, m)
{
m.def("Init", &Init);
m.def("CreateAgent", &CreateAgent, py::return_value_policy::copy);
}
int main()
{
Py_Initialize();
Init();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('D:/Work/projects/C++/Test/test1/Scripts/')");
PyObject* pModuleInit = PyImport_ImportModule("main");
PyObject* pFuncInitGame = PyObject_GetAttrString(pModuleInit, "InitGame");
PyObject_CallFunction(pFuncInitGame, NULL);
Py_DECREF(pModuleInit);
Py_DECREF(pFuncInitGame);
Py_Finalize();
system("pause");
return 0;
}
import C_monster
def InitGame():
print("InitGame")
C_monster.CreateAgent()
111111111111111 Init 0000020D0F577EF0
InitGame
222222222222222 CreateAgent 0000000000000000
C_monster 是啥?
不太严格按照python扩展编写规范来写,官方有实例的。