[C++-sig] Boost.Python As Engine's Scripting Language

Oam m11hut at gmail.com
Tue Apr 28 08:35:33 CEST 2015


After further tinkering I'm feeling like I have a good starting location. I
have the BaseBehavior inheritable class exported to the Python side for
utilization, and I have my cpp side calling the particular test script that
contains a class inheriting from the BaseBehavior and executing it.

However, now I am trying to figure out how to collect / extract that
particular class after the script has been executed on the cpp side. This is
what I'm trying to do as a simple example, but I'm not sure how to specify
what the result to look for. All I know is that it inherits from the
BaseBehavior class.

This is my test.py script:

> import Engine
> 
> print("Hello?")
> 
> class TestPiece(MagicEngine.BaseBehavior):
>   def OnUpdate(self):
>     print("Hello From TestPiece?")
>     
> test = TestPiece()
> test.OnUpdate()

This is my cpp side locating the "test" object from the main_namespace
BP::dict, which works.

> try
>   {
>     PyImport_AppendInittab("Engine", &PyInit_Engine);
>     Py_Initialize();
> 
>     // '__main__' is the name of the scope in which top-level code
> executes
>     BP::object main_module = BP::import("__main__");
> 
>     // we create a dictionary object for the __main__ module's namespace
>     // m.x = 1 is equivalent to m.__dict__["x"] = 1
>     // Special read-only attribute: __dict__ is the modules namespace as a
> dictionary object
>     BP::dict main_namespace = BP::extract
> <BP::dict>
> (main_module.attr("__dict__"));
> 
>     auto result = BP::exec_file("../Debug/test.py", main_namespace,
> main_namespace);
>     ME::BaseBehavior& base = BP::extract
> <
> ME::BaseBehavior&
> >
> (main_namespace["test"]);
> 
>     base.OnUpdate();
>   }
>   catch (...)
>   {
>     PyObject *ptype, *pvalue, *ptraceback;
>     PyErr_Fetch(&ptype, &pvalue, &ptraceback);
> 
>     if (pvalue != nullptr)
>     {
>       std::string error = BP::extract
> <std::string>
> (pvalue);
>       std::cout << error << std::endl;
>     }
>   }

How would I make it so it retrieves the TestPiece class for execution
without having to manually create an object on the python side. What I
really want is for the object to exist on the cpp side, but the python side
is what is manipulating it's behavior.



--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-Python-As-Engine-s-Scripting-Language-tp4674850p4674859.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.


More information about the Cplusplus-sig mailing list