I have classes defined in C++ code, and added to the python dictionary like this:
try
{
Py_Initialize();
}
catch (bp::error_already_set &)
{
parse_python_exception();
}
main_module = bp::import("__main__");
main_namespace = main_module.attr("__dict__");
bp::class_<Gamefunctions> gamefunctions_binding = bp::class_<Gamefunctions>("Gamefunctions");
m_localDictionary["Gamefunctions"] = gamefunctions_binding;
m_localDictionary["gamefunctions"] = bp::ptr(&gamefunctions);
void (Gamefunctions::*addlog_comp)(string, string, string, bool) = &Gamefunctions::addlog;
void (Gamefunctions::*addlog_simp)(string, bool) = &Gamefunctions::addlog;
gamefunctions_binding.def("addlog", addlog_comp);
gamefunctions_binding.def("addlog", addlog_simp);
Py_Initialize();
bp::object execute;
string testcode = "def calllog():\n";
testcode += " gamefunctions.addlog(\"logmessage\", True)\n";
testcode += "gamefunctions.addlog(\"logmessage\", True)\n";
testcode += "calllog()";
try
{
execute = exec((testcode).c_str(), main_namespace, m_localDictionary);
}
catch (bp::error_already_set &)
{
parse_python_exception();
}
And I tried to call it from a function in a python file like this:
def calllog():
gamefunctions.addlog("logmessage",True)//not found
gamefunctions.addlog("logmessage",True)//works fine
calllog()
The call from outside the "def" works perfectly, but when I call it from the "def" It gives me this error message:
<type 'exceptions.NameError'> global name 'gamefunctions' is not defined
How can I "define" the 'gamefunction' class in the 'calllog()' function?
Sorry for my bad english and thank you for any advice you give.