Recommended Posts

I'm investigating using Boost.Python for scripting in a game. After reading about all the documentation I can find on embedding Python using Boost.Python, I think a good strategy to pursue would to be have base classes defined in C++, and be able to create derived classes in Python scripts, and then call the base class methods on objects from my game, which would defer to the virtual methods defined in a Python script to execute some logic.

Given the following code, the next step I would like to try to take would be to have the exec call be replaced with the exec_file call, but apparently there is alot I don't understand yet.

I am guessing this is happening because my base class, Object is not able to be imported into the main namespace, being that it is defined on the C++ side? Or not, I really have no clue and am just stabbing in the dark here.

The code, as posted, works just fine, but isn't all that useful to me since it's executing a Python string defined in C++. I could read the .py files into a string, and use the exec call to execute them that way, but the exec_file seems much more elegant. Unfortunately, all the examples end at this point, and leave incorporating reading actual Python scripts as an exercise for the reader.

Can anyone point me in the right direction, and at least verify that my approach is valid?

Edit: There is definitely something wrong with the source tags, putting lang="cpp" in there caused it to seem to choke on the double less than symbol. At least all of the code is there now. If a mod wants to fix it or enlighten me on the subtleties of the new forum software, I'd appreciate it.

Share this post

Link to post

Share on other sites

I have solved the problem, if you look at the error I posted, the line where I try to call [color=#282828]

Core.Object.__init__self(id) should read

[color=#282828]

Core.Object.__init__(self,id)

[color=#282828]

With that, I'm moving on for now, but still wondering if my approach here is valid, or if there is a better way. I'm thinking a possible strategy for controlling entities would be to have base classes defined in C++, for example a Controller class that receives events from an event system, and provides virtual methods for handling those events. I could then have classes defined in Python scripts that would inherit from this Controller class, and implement logic for different controllers in the Python scripts. It may also be possible to change a Controller's behavior at runtime by associating a different Python script with it to change it's behavior, or change an entity's controller altogether.

[color=#282828]

I'm still in the investigation stages of incorporating scripting, so some of that may be way off, but I do know it gets old incrementing a value by 10, recompiling and relaunching my program only to see it not have any effect, then rinsing and repeating a hundred times, so a scripting console is definitely a feature I'm after here as well.