0

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.

Bady
  • 56
  • 8
  • It would help if you provided a [mcve]. Maybe, you would even find the error yourself extracting that from your code. – Ulrich Eckhardt Jan 30 '21 at 18:47
  • It's really just the definiton and the calling what is missing from here, but if it's more understandable like this, I won't be lazy. – Bady Jan 30 '21 at 21:18

1 Answers1

0

After a long searching, I found this page here: Boost Python - Global and Local dictionary

I already knew, there is something with the local and global scopes, but I approached the probmem from the wrong direction. I tought, I need to link the inside classes somehow in the python function/class itself, but actually I need to add the C++ classes to the global dictionary too like this:

bp::class_<Gamefunctions> gamefunctions_binding = bp::class_<Gamefunctions>("Gamefunctions");
m_localDictionary["Gamefunctions"] = gamefunctions_binding;
m_localDictionary["gamefunctions"] = bp::ptr(&gamefunctions);

main_namespace["Gamefunctions"] = gamefunctions_binding;
main_namespace["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);
Bady
  • 56
  • 8