So I have this game project which is going very well for now. I implemented OpenGL stuff, animations, interfaces, and a lot of very nice things that I'm glad I've finally gotten around.

So now that the skeleton of my game engine is made and is solid, I'd like to add AIs to make my entities more lively. Because I like the idea of a game being simple to mod, I would really like to write the AIs in Python files, that would then be read and executed from my C++ code.

So my questions are the following :

  • It's a game, so performance is a concern. Is it a good idea to write the AIs in scripting files ?

  • I've heard that there were some JIT compilers such as PyPy, are they easy to use from a C++ program ?

  • Finally, how easy is it to send C++ objects to be treated in the Python code ?

  • \$\begingroup\$ "Is this a good idea" usually boils down to opinion and development style (unless you're doing something truly monstrous), and "how easy is..." doesn't really have a correct answer. (ie. "That's a 3.5 on the easiness scale" doesn't really fly there) If you want to ask about how to do this, I'd recommend editing your question to pull out these more subjective bits and focus on the problem you're solving. Subjective questions often get closed as too broad or opinion-based. \$\endgroup\$
    – DMGregory
    Jul 21, 2017 at 15:34

2 Answers 2


This is definitely possible, see Pandas3D for a fairly robust example of what you're asking in action. There's some important things to mention though, Python cannot reliably be sandboxed. What this essentially means, is that you cannot pick and choose which python library features you want to expose to your modders. In other words any mod packages that they share with each other could be full of malicious actions, and there's nothing you can do as a developer to prevent this. Knowing however, that this is true about any Python script, maybe you want to leave that aside and ignore it. If not, there is some fairly similar languages to Python, that ARE sandboxy or at least already 'safe' enough to support what you're asking.

Suppose that you don't care if modders write malicious code, and just want to cover yourself with some sort of "this is provided as is, anything from anywhere else isn't my responsibility" clause, then great.

Assuming you choose to go with Python, you'll need to look into the two different options for doing so. One being "embedding" python, and the other being "extending" python. There's a couple of variants to each and they're all pretty fun to work with but have some fundamental differences. Learn about them here. That's going to be your guide if you choose to take this route.

If you decide that you still want to extend your engine by adding a scripting language, but don't mind looking into other options there's a few that have syntaxes similar to python so the learning curve wouldn't be rough. I don't know whether these are safer to sandbox, but I know that they're a bit more targeted at being embedded than Python is.

  1. Cobra
  2. Nim
  • 1
    \$\begingroup\$ I want to embed Python into cpp code. And yeah, I'm exactly following the "my software is harmless, be careful with the mods" philosophy. I know both Python and c++, and I start to get a hang around boost.python, so hopefully things will work the way I want. I will into Cobra and Nim \$\endgroup\$
    – Andarann
    Jul 24, 2017 at 19:24

Without getting into the nitty-gritty...it's possible.

The Python 2.x docs have a short guide on how you could do this. Basically, it involves including the python library and invoking it when you need to. As for using python in a pthon-y way in your program, you will need to hook it up to something like CVars.

Creating scripting files for AI's are not a bad idea, but you will want to make sure you load and unload them in a smart way. You will also need to figure out a serialization scheme to work for you, in order for python to work with C++. The guide I posted touches on that.

  • \$\begingroup\$ You've made no mention that Python can't be sandboxed, which I find to be a fairly important addition to what you've already said. \$\endgroup\$ Jul 21, 2017 at 4:44
  • \$\begingroup\$ I said Python can't be sandboxed, what I meant is, the most used implementations, which match full use according to the Python specifications, cannot be sandboxed. that's not to say a subset implementation which excludes entirely the features making it non-sandbox safe, may not be sandboxed, but that would be a Python subset, not Python. \$\endgroup\$ Aug 4, 2017 at 19:01

Not the answer you're looking for? Browse other questions tagged .