Calling Python functions from C

Markus Ewald Markus_Ewald at gmx.net
Wed Sep 8 07:05:21 EDT 1999


I'm trying to use Python for various things in a game. One of these
things is controlling particles.

I had used SeeR (http://home.elka.pw.edu.pl/~ppodsiad/seer/) in the past
which could easily compile its scripts into bytecode and keep them in
memory, you could then easily call functions in the script.
Only bad thing was it had some funny floating point bugs which
...well... caused some undesired effects on my particles ;)

So, two questions:

My game kept pointers to its particles in a simple linked list.
Each particle type (rain, smoke...) had a script which contained
functions for creating, moving, rendering and destroying the particle.
To add a particle my game just called the creation function in the
script for that particle-type, which just malloced as much memory as the
particle would need, set some initial values and return the pointer to
my game.
This, of course, wouldn't be possible in that way using Python, so what
is the best (quickest) solution ?
Write a Python module for storing particles and let the script keep them
?

Other question:
How can I keep a compiled Python script in memory and call functions
(not executing the whole thing) in it ?
I didn't find any example on the net until now and have already tried
alot...
like getting a callable object from a script from a _node to a
CodeObject to a PyObject with
  PyParser_SimpleParseFile();
  PyNode_Compile();
  PyEval_EvalCode();
and then calling functions to execute something in it...
  PyImport_ExecCodeModule();
  PyObject_CallFunction();
well, didn't work ;..(

Thanks,
and if anyone knows an url with something about embedding python, i'd be
happy :)

-Markus-







More information about the Python-list mailing list