Mailing List Archive

Using Python for Modular Artificial Intelligence code
Hi all,

I've been designing a large game project, and I want it to be easily
extensible. It's a space combat/exploration/trade game, in the spirit of
the original Elite. What I want to be able to do is add new units to the
game while the player is in the middle of a game that has lasted weeks (or
any arbitrary length of time) already.

I've started looking into Python as the AI scripting language for this
purpose, but I'm having trouble seeing exactly how I could do this. I want
a message passing architecture - so one object in the game world sends a
message to another for interpretation (along with a Python tuple for the
parameters of the message perhaps). This requires a two way interface
between my C++ code and the Python script loaded for the particular game
object.

My question is, how can I load the Python script, let my C++ object call
functions within the Python script, and provide a callback for the Python
script to call methods in the C++ object?

Thanks,
Tim

________________________
astro@tne.net.au
tim@ratbaggames.com
Using Python for Modular Artificial Intelligence code [ In reply to ]
Tim Auld <astro@nospam.tne.net.au> wrote:
> Hi all,

...

> My question is, how can I load the Python script, let my C++ object call
> functions within the Python script, and provide a callback for the Python
> script to call methods in the C++ object?

Get the Python documentation at http://www.python.org/doc, then
look at the sections on "Extending and Embedding" and "Python/C
API". There is lot's there on this topic.

Also download the Python source distribution. In the demo
directory you will find an example on embedding. Play with and
expand on that to learn how to do it yourself.

Then ask some more questions on this news group.

- Dave

- Dave

> ________________________
> astro@tne.net.au
> tim@ratbaggames.com
Using Python for Modular Artificial Intelligence code [ In reply to ]
>I've started looking into Python as the AI scripting language for this
>purpose, but I'm having trouble seeing exactly how I could do this. I want
>a message passing architecture - so one object in the game world sends a
>message to another for interpretation (along with a Python tuple for the
>parameters of the message perhaps). This requires a two way interface
>between my C++ code and the Python script loaded for the particular game
>object.
Wouldn't you be better of using a network or distributed architecture. Use
Corba or TCP/IP protocol to let your objects interact. Saves you the trouble
of language-interoperatablility and provides possibilities for multi-user
(You're doing a LARGE game project, not?)

Where do you use C++ for anyway? GUI's? Build them in Python, forget C++
until you are done and than use C++ to speed up critical sections

Ilja
Using Python for Modular Artificial Intelligence code [ In reply to ]
>>I've started looking into Python as the AI scripting language for this
>>purpose, but I'm having trouble seeing exactly how I could do this. I
want
>>a message passing architecture - so one object in the game world sends a
>>message to another for interpretation (along with a Python tuple for the
>>parameters of the message perhaps). This requires a two way interface
>>between my C++ code and the Python script loaded for the particular game
>>object.
>Wouldn't you be better of using a network or distributed architecture. Use
>Corba or TCP/IP protocol to let your objects interact. Saves you the
trouble
>of language-interoperatablility and provides possibilities for multi-user
>(You're doing a LARGE game project, not?)
>
>Where do you use C++ for anyway? GUI's? Build them in Python, forget C++
>until you are done and than use C++ to speed up critical sections
>
>Ilja


This is an interesting idea, using sockets to communicate with the AI
modules, which would have
to be run in separate threads (but perhaps blocking on socket I/O to save
cycles). I was naturally going to use sockets for multiplayer, and having
socket connections to AI modules may even generalise (and simplify) the
system further. The reason I'm using C++ is mainly because I'm familiar
with it, and because of speed. I'm aware that Python is relatively slow, so
I would rather use C++ as a base from the start, and save myself the trouble
of rewriting it all later.

Thanks for your help :)

Tim
Using Python for Modular Artificial Intelligence code [ In reply to ]
On Fri, 30 Apr 1999 23:07:45 +0930, Tim Auld wrote:

>>>I've started looking into Python as the AI scripting language for this
>>>purpose, but I'm having trouble seeing exactly how I could do this. I

>>Where do you use C++ for anyway? GUI's? Build them in Python, forget C++
>>until you are done and than use C++ to speed up critical sections

>system further. The reason I'm using C++ is mainly because I'm familiar
>with it, and because of speed. I'm aware that Python is relatively slow, so
>I would rather use C++ as a base from the start, and save myself the trouble
>of rewriting it all later.

If you write everything in Python, you will wind up with a program which
works far sooner than if you'd written it in C++ (unless you're _REALLY_
good at C++). You can then pull out the parts which need speedup into C
or C++.

It won't be that much trouble to rewrite -- it seldom is, really. The
hard part is getting the design, not writing the program. Python offers
the ideal design language, because it's executable.

>Tim

--
-William "Billy" Tanksley
"But you shall not escape my iambics."
-- Gaius Valerius Catullus
Using Python for Modular Artificial Intelligence code [ In reply to ]
In article <7g94lp$mdu$1@news.worldonline.nl>, Ilja Heitlager wrote:
>
>Where do you use C++ for anyway? GUI's? Build them in Python, forget C++
>until you are done and than use C++ to speed up critical sections
>
>Ilja
>
>
I'd have to agree there: make the game in Python and only code the
critical parts in c/c++ .. the graphics system(if you have one) and
what have you. I tried doing it the other way (deciding what was
extensible and then trying to wrap Python around that) and it leads to
poor design and a lot of wasted time.

Not to make myself like an expert (I'm nowhere near!), but I would say
implement the WHOLE thing in python, using a simple wrapper around your
graphics libs, until you have the design for the thing worked out. As an
interpreted language, Python makes iterative design possible - a good
thing. Then, build Python extensions for the specific parts that need to
be in c or c++.. this leaves the bulk of your game extendable, without
having to designate it as extendible. A good design will also help you
and your designers experiment with different AI schemes. And don't
forget that if any of those schemes requires a few tight native code
methods, you can dynamically mix them in, even in the field after
distribution.

I have a very basic libGGI wrapper available (actually, I'd be a fair
bit embarrassed about releasing to the public as it is,so ask me to email
if you'ld like to see it) for this very purpose. Audio could be done the
same way.


-Jim Meier
Using Python for Modular Artificial Intelligence code [ In reply to ]
>My question is, how can I load the Python script, let my C++ object call
>functions within the Python script, and provide a callback for the Python
>script to call methods in the C++ object?
have a look at the LLNL Python extensions, especially their CXX
extension. its really great for creating Python/C++ applications ...

http://xfiles.llnl.gov/python.htm


kaweh
Using Python for Modular Artificial Intelligence code [ In reply to ]
>If you write everything in Python, you will wind up with a program which
>works far sooner than if you'd written it in C++ (unless you're _REALLY_
>good at C++). You can then pull out the parts which need speedup into C
>or C++.
>
>It won't be that much trouble to rewrite -- it seldom is, really. The
>hard part is getting the design, not writing the program. Python offers
>the ideal design language, because it's executable.


You may be right - Python looks very flexible and powerful, but I'd still
like to stick with something I'm familiar with and I know speed won't be a
problem. I'm using a few different APIs: OpenGL, Winsock, DirectX - so
using Python would require learning how to access each of these, which
doesn't sound tempting considering I know how to do it in C/C++ right now.
I also figured out a better way to have modular AI code: loading DLLs
containing the code controlling the units. The multi-threading with socket
communication would have been too slow.

You guys really love Python don't ya ;) must be good!

Regards,
Tim
Using Python for Modular Artificial Intelligence code [ In reply to ]
Tim Auld defends his decision to do it in C++:

> ... The multi-threading with socket
> communication would have been too slow.

Snicker. You can saturate a LAN connection from a Python socket.
Granted, if you rewrite in C, you'll use a bit less CPU.

Unlike say Java, most of this stuff is a very thin wrapper around the
C library facility. If you know the C API, the Python API will be a
no-brainer.

I can certainly respect your argument about learning curves, but this
is exactly the type of thing I'd prototype in Python first, before
translating to C++. And maybe skip the last step, if I could...

- Gordon
Using Python for Modular Artificial Intelligence code [ In reply to ]
Tim Auld wrote:

>The reason I'm using C++ is mainly because I'm familiar
>with it, and because of speed. I'm aware that Python is relatively slow,
so
>I would rather use C++ as a base from the start, and save myself the
trouble
>of rewriting it all later.


I did some work in C and later I had to switch over to Java. What a
relief!!!!!!
Increased productivity, no more pointers!!!!!! Then I discovered Python,
which I love even better. Now I can test interactively. Full COM support
(interactive!!!!)
Short code-debug cycles (no compilation). This is what you need to develop.

Don't go blind on speed and don;t be afraid to code things twice! You read
more and more nowadays about the two-language approach. A scripting language
like Python and a system language like C++ or Java (I know, it sounds a bit
odd).
You can learn Python in a few days and it will really improve your
productivity

Ilja Heitlager
Using Python for Modular Artificial Intelligence code [ In reply to ]
>> ... The multi-threading with socket
>> communication would have been too slow.
>
>Snicker. You can saturate a LAN connection from a Python socket.
>Granted, if you rewrite in C, you'll use a bit less CPU.


The "multi-threading with socket" solution I was talking about refered to
using sockets between C/C++ program threads, not Python. Just about
anything can saturate a network. It would have been slow for the game due
to the task switching and that Windows doesn't handle lots of threads well,
as well as the unnecessary data transfer.

Regards,
Tim
Using Python for Modular Artificial Intelligence code [ In reply to ]
>>The reason I'm using C++ is mainly because I'm familiar
>>with it, and because of speed. I'm aware that Python is relatively slow,
>so
>>I would rather use C++ as a base from the start, and save myself the
>trouble
>>of rewriting it all later.
>
>I did some work in C and later I had to switch over to Java. What a
>relief!!!!!!
>Increased productivity, no more pointers!!!!!! Then I discovered Python,
>which I love even better. Now I can test interactively. Full COM support
>(interactive!!!!)
>Short code-debug cycles (no compilation). This is what you need to develop.

>
>Don't go blind on speed and don;t be afraid to code things twice! You read
>more and more nowadays about the two-language approach. A scripting
language
>like Python and a system language like C++ or Java (I know, it sounds a bit
>odd).
>You can learn Python in a few days and it will really improve your
>productivity


Sure, I can appreciate the programming ease which these higher level
languages bring (I think Java is really clean), but when you are working on
games you kinda need to go "blind on speed" to a large degree. It's no good
having a game that was easy to program if it runs like a dog. And perhaps
you can speed it up by writing parts in C, but I've had several years
experience in C already, and I enjoy it. Tools for C/C++ and improving a
lot also - I love the function parameter help VC++ 6.0 gives you, the browse
info, and the debugger. There's also incremental compile/link. Add to this
great online help resources, mountains of great books, and support from
other programmers, and there's still a great case for C/C++. You can learn
the syntax to any language in a few days, but a language is more than just
syntax. There are many small things you need to learn, and the different
libraries/APIs/tools/experience etc. that come along with it.

This is not supposed to be a "my language is better than yours" debate :)
please let's agree to disagree - I really think Python is good, it's just
that using it doesn't weigh up in this situation.

Regards,
Tim