Advertisement

Embedding Python into game engines.

Started by January 21, 2002 12:45 AM
27 comments, last by -=Code=- 22 years, 10 months ago
I am NOT saying that Python is going to render graphics. I am asking HOW it would call the gfx. Of course if s/he was to make python call gfx routines, yes he could do it, but then, the interpreter would have to run all of those lines of code.

You see he archetecture on which Python is made dissallows functions from other languages without modifing the code greatly.

You can''t just call a program in Python to show a line without reading the ''Extending Python'' tutorial.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
You can write a C function and call it from Python. It''s pretty easy; even the Alice website has a section on it in their advanced help.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Advertisement
That is exactly why it''s under advanced help. And either way, you have to then RUN the program from Python then.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
quote: Original post by Andrew Nguyen
That is exactly why it''s under advanced help.

Go take a look; there''s nothing "advanced" about it. the only reason it''s under "advanced help" is because Alice is designed for non-programmers. For programmers it''s a walk in the park.
quote: And either way, you have to then RUN the program from Python then.

Wrong again. You call the C functions simply by importing the extension module. For example, say I made a Python module called GSDK to provide access to the graphics engine SDK from Python. To use it in Python, I''d simply do the following:
import GSDKGSDK.InitGraphics()GSDK.DrawText("This is NOT hard!")GSDK.UpdateScreen()GSDK.Wait(10000)GSDK.TermGraphics() 

Any further under-informed opinions, Mr. I-Am-Python?

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
I understand that C->Python is easy. Now show me Python->C (Don''t go off using swig! ... although...)
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
I''m using embedded Python in the game I''m working on. My program(C++) loads a python interpreter, feeds it some startup scripts, and then makes calls into the runtime occasionally.

On the other end, my program provides extensions, so that scripts have native hooks into the game itself. A big chunk of the python code that gets called turns around and calls native methods that I''ve provided.

I''m limiting the python scripts to logic-only stuff, I wouldn''t do any graphics with it. And I tried to offload as much as possible into the extension code.

I should note though, that this isn''t easy. There is a *lot* of support code that goes into making this happen. If it wasn''t for the fact that I like python so much, I wouldn''t have even bothered. For the simple game I''m working on it''s gross overkill, but the experience will serve me later when I''m working on something much more scriptable.

If anyone is interested, I''ll get my code cleaned up and maybe post it somewhere. Perhaps even a short tutorial or something on using python as a scripting language.

Take care,
Bill
Advertisement
Darn, a post that can go both ways... :p
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Check out Crystal Space http://crystal.sourceforge.net/ and CEL http://cel.sourceforge.net/ There is a Python plugin that is part of the engine. It uses SWIG, however it might give you a clue how to implement it.

Also in the Python docs are how to do C->Python w/o SWIG
I meant Python->C WITHOUT SWIG
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
If you''re interested, I''ve written an IRC client that supports Python as a scripting language, and I have to say it''s been very easy.

And yes, I call functions both ways, Python -> C and C -> Python. I do not use SWIG. I haven''t looked into SWIG because there''s just not much to calling C functions from Python:

#define PythonFunction(name) \	PyObject* name(PyObject* self, PyObject* args)PythonFunction(BaoIrc_MessageBox){	char* sMessage;	UINT nType = MB_OK;	char* sCaption = 0;	if (!PyArg_ParseTuple(args, "s|iz:MessageBox", &sMessage, &nType, &sCaption))		return NULL;	if (!sCaption)		sCaption = "BaoIrc";	int res = ::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), sMessage, sCaption, nType);	return Py_BuildValue("i", res);} 


You can download a very buggy version of BaoIrc from http://students.washington.edu/bqn/baoirc


This topic is closed to new replies.

Advertisement