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

Ger van Diepen diepen at astron.nl
Tue Apr 28 15:07:06 CEST 2015


I believe we have done something similar to what you intend to do. 

We have an astronomical query language (implemented in C++) where
arbitrary user defined functions (named libname.func) can be loaded
dynamically from a shared library. It has the possibility of user
defined functions implemented in Python. In the query language such
functions are named py.module.func. Each such function has to reside in
a class 'func', subclassed from a UDFBase class (basically implemented
in C++ and wrapped using Boost-Python). It makes it possible for the
Python code to call back to the C++ code (e.g. to get the value of the
function arguments). 
Basically the 'func' class must have a function 'setup' (to check the
argument data types, etc.) and a function 'get' to obtain query results.
These functions are called from C++ using the boost::object::attr
function. 
Clearly, such functions will perform far from optimal, but it is very
convenient for special ad-hoc query functions. 

If you like to, I can send you more details about the implementation. 

Cheers, 
Ger 


>>> Oam <m11hut at gmail.com> 4/28/2015 3:30 AM >>>
(Original Message Posted At :
http://www.gamedev.net/topic/667952-boostpython-as-engines-scripting-language/
<http://www.gamedev.net/topic/667952-boostpython-as-engines-scripting-language/>
)

Am I thinking about this the right way?

I'm currently working with some Boost and Boost.Python in my C++
project, in
attempts to get my project's library to utilize Python as it's game
scripting language. Similarly I suppose to how Unity utilizes C# as one
of
it's game scripting languages.

So let's say I have an abstract class that contains the virtual methods
for
overriding the specific behaviors of each particular event, such as
Unity's
MonoBehavior, but BaseBehavior in my case.

This is what I'm trying to do and what I'm thinking the right way to do
this
is :

    1) I have a project that is dedicated to just the library portion
of the
framework (Contains the BaseBehavior abstract class)

        class BaseBehavior()
        {
          public:
            virtual void OnUpdate() = 0; 
        };

    2) Export this library portion classes / functions / etc using
Boost.Python

        class BaseBehaviorWrap : public BaseBehavior, public
boost::python::wrapper<BaseBehavior>
        {
          public:
            void OnUpdate()
            {
              this->get_override("OnUpdate")();
            }
        };

        BOOST_PYTHON_MODULE(ModuleName)
        {
          boost::python::class_<BaseBehaviorWrap,
boost::noncopyable>("BaseBehavior")
            .def("OnUpdate",
boost::python::pure_virtual(&BaseBehavior::OnUpdate));
        }

    3) Within Python, in for example: ObjectBehavior.py I import my
library
module inheriting from a new class from the BaseBehavior and utilizing
the
particular override behaviors, such as OnUpdate()

        import ModuleName

        class NewObjectBehavior(BaseBehavior):
          def OnUpdate(self):
            # Game Behavior
            # ...

    4) Back within the C++ side I'll have a list/vector of all the
objects
that contain scriptable behavior and their associated attached scripts
that
contain this BaseBehavior script
    5) I'll collect each script per object and call the [ object
exec_file(str filename, object globals = object(), object locals =
object())
] function within the Boost.Python on my C++ engine side
    6) From here I will utilize the boost::python::extract object to
collect
my particular overridden functions and call them accordingly per
object

I'm concerned with my fascination to go back to the C++ side to execute
each
file and then extract each function I need per object that contains
behavior. Is this the right way to do this, or should everything be
done on
the Python side? I'm really trying to keep the ideal that the GameApp
is
utilized within C++, such as the window creation, event handling,
frame
calculations, etc, just the engine specific pieces. While the Python
side is
dedicated specifically to just Game Behaviors.



--
View this message in context:
http://boost.2283326.n4.nabble.com/Boost-Python-As-Engine-s-Scripting-Language-tp4674850.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20150428/e842c8c2/attachment-0001.html>


More information about the Cplusplus-sig mailing list