Jump to content
  • Advertisement

Archived

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

incubator01

Python & C++

This topic is 5254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I dont know if any of you guys had any problems with this, but I was just following a tutorial on using boost with python. They say that bjam is used to compile shared libraries that can be used in Python. I just tried this with that simple hello world thing of theirs and all I got were errors about no root etc. When I tried some of their examples I got "You need bjam version 3.1.9" and for those that did work, well, their Jamfiles are waaay different then what hteir site suggests. Now, I dont like such differences and inconveniences. I dont know how you guys deal with this but i am sick and tired of looking for things that dont work as they should. How do you usually implement scripting in your game without all that hassle? Also, I fail to understand why things like Python is even needed for a C++ game.

Share this post


Link to post
Share on other sites
Advertisement
Once I have built the boost libraries, I don''t use bjam at all. I generally rely on the python distutils module to build any extension.

I just tried this with that simple hello world thing of theirs and all I got were errors about no root etc.

Build scripts commonly rely on environment variables.

Also, I fail to understand why things like Python is even needed for a C++ game.

C++ games do not necessarily require scripting, if that''s your question.



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
as I humbly quote the lead developer of VegaStrike: (http://vegastrike.sourceforge.net)

"perhaps python''s not the best language because of its lack of type safety--but as far as speed is concerned...when you''re doing scripting the most important thing is turnaround time--that is how easy it is for average users with no compilers to add to your game...
this is why Vega Strike has the main engine using C++ and the missions and AI scripts using python---easy for extensibility...
if you''re not planning on putting in a scripting engine for your engine how do you plan to make missions that will unfold depending on what a user does"

and so, I seek light through the darkness

Share this post


Link to post
Share on other sites
Well, when your game is scriptable, users can "modify" the game without either access to a compiler or to the game''s source code. And Python has the advantage of being a full-fledged programming language that is also portable and comes with an extensive standard library.

Now, if you tell me precisely what kind of error messages you get, I *might* be able to help.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites

Jamrules: No such file or directory
/usr/share/boost/v1/example/../boost-base.jam:934: in fixup-path-properties
*** argument error
* rule root-paths ( paths * : root )
* called with: ( <sysinclude> : )
* missing argument root
/usr/share/boost/v1/example/../allyourbase.jam:1441:see definition of rule ''root-paths'' being called
/usr/share/boost/v1/example/../boost-base.jam:1199: in expand-build-request
/usr/share/boost/v1/example/../boost-base.jam:1963: in expand-target-subvariants
/usr/share/boost/v1/example/../boost-base.jam:2002: in main-target
/usr/share/boost/v1/example/../boost-base.jam:1349: in declare-local-target
/usr/share/boost/v1/example/../python.jam:438: in extension
Jamfile:7: in load-jamfiles
/usr/share/boost/v1/example/../bootstrap.jam:15: in boost-build
/usr/share/boost/v1/example/boost-build.jam:6: in module scope


but I didnt find anything on hwo to make a jamrules files.

But in any case, I have no need for the scripts to be editable by the users. So if thats the only reason I should use python, then I would better not use it. That alone would simplify things a lot
already

Share this post


Link to post
Share on other sites
quote:
Original post by incubator01
but I didnt find anything on hwo to make a jamrules files.



bjam is a modified version of Perforce Jam. You could start by looking there, I believe they have some doc. Or you can ask on the boost-build mailing list (you can also try the Python C++ SIG for boost:ython questions).

quote:
But in any case, I have no need for the scripts to be editable by the users.


"users" also include the people that design your levels, as your original quote indicated. If the level includes any "logic" of complexity beyond "key X opens door Y" (something that can be encoded as plain data), you''ll probably need some scripting support.

What kind of game are you planning?

(note - I''m installing boost on my Gentoo box right now, so I can''t answer your other question immediately - nor give you a basic makefile to solve the problem).


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
hello.cc
#include <boost/python.hpp>
using namespace boost::python;

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

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



$ g++ -I/usr/include/python2.3 -fPIC -c hello.cc
$ g++ -lboost_python -shared hello.o -o hello.so
$ python

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


Embedding Python into a C++ program first involves extending the program with Python (giving access to the C++ stuff from Python itself), which is what boost:ython (and other tools like SWIG and Elmer) give you access to.

I''m writing a short intro for you (as well as a demo C++ program)


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
thanks I'll try that out.
Oh, and I use gentoo as well

and the game I am going to make is something like vegastrike, like the old privateer.

[edited by - incubator01 on June 5, 2004 10:38:30 PM]

Share this post


Link to post
Share on other sites

#include <boost/python.hpp>
using namespace boost::python;

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

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

int main()
{
// Disable the automatic ''import site''

// Use when end user doesn''t have a ''real'' python installation.

// Py_NoSiteFlag = 1;


// The "hello" module is built-in, not in an external script file

// The "inithello" function initializes the module (boost provides it)

PyImport_AppendInittab("hello", inithello);

// Start the interpreter

Py_Initialize();

// Import the module

// There is a way to do it programmatically from C, but I''ve been trying

// for a bit without success, so I do it from python.

PyRun_SimpleString("import hello");

// Do our hello world example

PyRun_SimpleString("hello.greet()");

// Terminate the interpreter

Py_Finalize();
}


Build with g++ -lpython2.3 -lboost_python -I/usr/include/python2.3 hello.cc -o hello

Now, note that the string passed to python is hard-coded in the application. If you were to do it ''for real'', that string would come from your data/level/mission files (or would be a file in and of itself, with a function like PyRun_File). And yeah, scripting would be good for your particular game.

As I said in the source, I''ve been trying to get the C-side import to work (I have done it before), but tonight I just can''t. So I felt would would beneficiate more from a less-than-perfect example now than a ''perfect'' one at some underterminate point in the future.

Good luck. I can often be found in #gamedev (on AfterNet) if you have more questions.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
thanks. This is very helpful! :D

but how exactly to scripts fit in games? I mean, to what purpouse?
I dont quite understand that part.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!