Archived

This topic is now archived and is closed to further replies.

HexDump

How To do this using c++ & python (Best way)

Recommended Posts

Hello guys, I´m looking for a system where I could load different entitiy types dinamicalyand them instanciate them by demand. For example, I could have my maps and my enemy references put in there, an my engine could load it and instruct python to load the enemies classes and then instanciate the type I need using the info in the map. I'd like to know the best way to accomplish this using c++ and python because I can´t make it out. For example, where should be the object manager placed? in C++ side or python side. Where should be created the objects in the c++ or python side? what should be estored in the object manager py_objects, Object IDs?, should I need any parent class in C++ and then inherit all the object classes I create with python?. Well all this kind of this if what I need to know. thanks in advance, HexDump. [edited by - HexDump on October 7, 2003 7:03:01 PM]

Share this post


Link to post
Share on other sites
Python classes are objects like any others.
Objects can be serialized with the pickle (or cPickle) module.
The shelve module adds a dictionary-like interface on top of it.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on October 7, 2003 7:04:24 PM]

Share this post


Link to post
Share on other sites
Fruny could you explain me how to achieve what I get? I need some short of explanation mate.Sorry but your answer is not enough for me, I´m don´t have too much experience with python yet.


HexDump.

Share this post


Link to post
Share on other sites
Unless you''re more precise in explaining whet you want, no, I can''t give a more detailed explanation. Python already support dynamic loading (heck *everything* in Python is dynamic).
You can easily create objects given their class name, you can save and load whole objects to the disk (meaning you could save your whole level to the disk and directly reload it). etc.

What exactly are you trying to achieve ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
I would like to code in python a new enemy and make it ready to be used in my game without recompiling. I mean, the order should be:

1 - Engine Loads the scene
2 - Engine get the types of enemy that lives in this scene
3 - Engine instructs python to load the "enemy types (classes)"
to python
4 - Game Creates intances as it is needed, using information in the map.


Hope this is clearer, thanks in advance,

HexDump.

Share this post


Link to post
Share on other sites
The python __import__ function lets you load a module given its name. You can use the object it returns to refer to the module and use it to access whatever enemy classes you need.

Note that it is generally much easier to have your Python code drive your C++ code than the other way around. Let Python keep track of your enemies, game logic and all that stuff and just make C++ do the rendering (exposing the engine as a python extension module)

Look in the python documentation covering "Extending and Embedding". Check out the Numarray extension module to manage your graphic data (e.g. vertex arrays)


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites