Skip to main content
gamedev.net
🔒 Locked

Where does python fit in?

Started by sipickles Dec 2, 2005 at 12:26 PM 10 replies 1.3k views
Original Post
sipickles
sipickles
[oops - posted this in newtork programming- soz] Hello, I am developing my own architecture for my MMOG. Its a little different to winterdynes! :) I am interested in your opinions regarding development, since I am on the verge of plunging into a first complete build. At present I have a client prog, written in MSVC7 using winsocks to communicate. This is logging into a server, which will validate client moves, etc, and store game info in a MySQL database. The server is also C++. Recently, I've got into python a bit. Its cool! So my first question is - Where do game co's use python? I heard that since its runtime compiled, its good as a scripting language to control AI, objects, etc. Is a python server a bad idea? its slower than C++ being RTcompiled, I think, but really set up for MySQL etc. How does my C++ app talk to python? Many thanks Simon
Arild Fines
Arild Fines
Quote:
Original post by sipickles
So my first question is - Where do game co's use python? I heard that since its runtime compiled, its good as a scripting language to control AI, objects, etc.

You should really take a look at Civilization IV. Pretty much all the game logic is written in Python, and they supply the Python scripts *with* the game, so you can modify them if you want.
Quote:

How does my C++ app talk to python?

Civ IV uses boost::python.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
sipickles
sipickles
ah,

I already have boost installed to help me with stoopid c++ strings!
smr
smr
Battlefield 2 uses it as well. I started writing a server entirely in python, but it was too slow for the "inner-loop" stuff, such as constructing packets and object serialization (I wasn't using pickle, as pickles would consume too much bandwidth). I've settled with C#. I am probably going to use python for scripting.
sipickles
sipickles
so how do you integrate C++ and python, if c++ is gonna do part of the work, and python the scripting?

chollida1
chollida1
BOOST:Python will allow you to connect python scripts and C++ methods.

I suggest you start at www.boost.org/libs/python/doc.

That's where I started:)

Cheers
CHris
CheersChris
chollida1
chollida1
here's their first sample:

char const* greet()
{
return "hello, world";
}

can be exposed to Python by writing a Boost.Python wrapper:

#include
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
def("greet", greet);
}

That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:

>>> import hello
>>> print hello.greet()
hello, world


Cheers
Chris
CheersChris
smr
smr
Quote:
Original post by sipickles
so how do you integrate C++ and python, if c++ is gonna do part of the work, and python the scripting?


Well there are two approaches. The first is to embed python within your C++ application.

The second, and IMO, the best option is to write your program in python, then write optimized modules in C++ for those sections of code that need it the most. This will reduce the amount of C++ code you write, and it is much easier to implement.

For my project I considered this option, but it wouldn't have made much of a difference in speed. My major bottleneck was not code, but rather the slowness of python's class implementation, which keeps all variables as entries in a dictionary.
Binomine
Binomine
Quote:
Original post by sipickles
So my first question is - Where do game co's use python? I heard that since its runtime compiled, its good as a scripting language to control AI, objects, etc.
The usual way is to have the Engine(C++) as the body and your scripting language(python) as the brain.

Write your C++(Engine) code with all the important functions that your Engine is going to need and write your python(scripting) code with all the important game logic decisions.

The perfect design would allow you to rewrite the python code and create a new game(without touching the C++) or drop in new C++ code without modifying the python.

The second way, is to simply write in a C++ / python hybrid language, where you pass off the tasks between C++ and python, depending on how much better each one handles that task. The disadvantage of this process, is that you lose the reusibility of the first way, but at the same time, you can write code much faster this way and it's possible to get a much faster game.

As for a total python server. This would depend on what your bottleneck is going to be. If your server is memory or CPU bound, C++ might be better choice. If your server is bandwidth bound using python would be fine. Weither to write one would soley be based on your resources and the game design.
sipickles
sipickles
BOOST::python seems, like SWIG, to enable you to use C++ functions in Python scripts.

I was thinking more of having a C++ program, accessing Python functions in external files..... is this possible?

Thanks
SiCrane
SiCrane
Yes. You can use PyRun_String() and related functions from the Python C API to run Python scripts from strings. You can also save function references as boost::python::objects and call them directly from C++.
Dobbs
Dobbs
Quote:
Original post by smr
For my project I considered this option, but it wouldn't have made much of a difference in speed. My major bottleneck was not code, but rather the slowness of python's class implementation, which keeps all variables as entries in a dictionary.


Using python's new-style classes with __slots__ can help with that slowness if it's a problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.