Sign in to follow this  
bossmonkey666

Python scripting for an RPG

Recommended Posts

I have started making a 2D Top Down RPG game engine and i would like to use scripting to control the story of the game. I chose python for the scripting because it seemed to me compared to Lua and Ruby that the syntax is much easier and the features seem vast. My problem is now that I dont understand quite how I need to go about allowing my game to be scripted. I have created a few different classes in C++ to handle the graphics, sound, logging, map loading etc. What is the best way to create the final distributable game: Is it to make the current engine into a dll and call functions from it in Python? Or to keep it as an exe and call python scripts from a new scripting class? I have currently tested SWIG to create the interface between C++ and Python and I have managed to create a test DLL from which I can call functions from. So if I do this for my game is it realistic to create a script to call the intialisation functions from my engine and run the main game loop etc. Also how can I make the *.py files into compiled python files so that when I give out the game the user cant easily change the game through the scripts. I would greatly appreciate if some one can help me understand all of this, thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by bossmonkey666
Also how can I make the *.py files into compiled python files so that when I give out the game the user cant easily change the game through the scripts.


Python automatically generates compiled output when it runs a script. You should see someScript.pyc in the same directory as someScript.py after running (directly or otherwise) that script.

I believe you should also rethink your use of scripting. If you are coding this project alone, you will get very little benefit from going through the work of integrating Python. Yes it will be quick and easy to make changes, but it's also easy to make changes in the C++ code without doing a full project rebuild. Scripting will really only benefit you if a) you have a team of people working on the game and you want to ease content creation and/or b) you want the users to be able to mod the game. If neither of those apply, it makes little since to go forward with it, IMO.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For the compiled version of your py, as stated above, just run your scripts, the compilation occurs at run time...

But I don't agree at all with the second Aldacron point.
Python is a langage that is much easier to program with, and what that's mean is that you will progress much more quickly using python...

So stick with Python, and a C++ layer for whatever need metal speed.

As for the question for the exe. You have two choices :
* Have Python embedded in your C++ exe, so provide an C++ exe. (my current choice)
* Use Python as your main language, and extend it with C++. For the distributable game, use py2exe, or create a mere exe that launch the py program using the dll or just modify the python lib in order to create your own exe !!!

I would go with the second solution ( although, or perhaps because I use the first one ).
Having C++ call the Python doesn't have any obvious advantage, and it's a nightmare to include a Python debugger in this configuration. I had some result with HAP, but HAP is unmaintained, and bugged !!!
And I don't know any other debugger for embedded python (someone ??? )

So using Python as your main program will let you use any nice python debugger you can easily find, and using VC to debug a dll called by python is easy !!

Hope it helps,

Share this post


Link to post
Share on other sites
Quote:
Original post by bossmonkey666
I have started making a 2D Top Down RPG game engine and i would like to use scripting to control the story of the game. I chose python for the scripting because it seemed to me compared to Lua and Ruby that the syntax is much easier and the features seem vast. My problem is now that I dont understand quite how I need to go about allowing my game to be scripted. I have created a few different classes in C++ to handle the graphics, sound, logging, map loading etc. What is the best way to create the final distributable game: Is it to make the current engine into a dll and call functions from it in Python? Or to keep it as an exe and call python scripts from a new scripting class?

I have currently tested SWIG to create the interface between C++ and Python and I have managed to create a test DLL from which I can call functions from. So if I do this for my game is it realistic to create a script to call the intialisation functions from my engine and run the main game loop etc.

Also how can I make the *.py files into compiled python files so that when I give out the game the user cant easily change the game through the scripts.

I would greatly appreciate if some one can help me understand all of this, thanks.


Like the other people said you the interpreter compiles the scripts into .pyc files but as far as I know these are pretty easy to decompile.. And I wouldn't bother with obscuring the scripts. If someone wants to change them and spoil all the fun in the game for themselves, that's their problem. Also, it will be easier to "mod" your game if you don't obscure the scripts. Your game might become more popular if it's easily moddable (I'm trying to do this in my game but alas I don't think my system is versatile and user friendly enough).

I disagree with Aldacron. I think you should use scripting, particulary in a rpg which really requires a lot of scripting (quests, items, dialogues etc.).

Share this post


Link to post
Share on other sites
Thanks for all of the replies.

I think I will stick with using python for my engine for various reasons: I want my game to be moddable, easily modified by me or a friend who might help with the story line, and its a good learning experience.

I think that extending Python with C++ will be easier than the other method. So I will provide my engine in a dll and call functions from it with Python (this will need some testing on my behalf but i hope it works).

With this method will the computer the game is being run on need python installed? (i really wont to ensure no further installations are needed cause then they just wont bother to play the game)

I have heard that the "python23.dll" provides the interpreter so I guess I will just try distributing it with that dll. Though if some one could please clarify this it would be great.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
But I don't agree at all with the second Aldacron point.
Python is a langage that is much easier to program with, and what that's mean is that you will progress much more quickly using python...


I don't disagree with you at all that Python is easy and productive (once you are comfortable with it). I just think it's rather pointless to use it if you are concerned about hiding your code.

Share this post


Link to post
Share on other sites
Oh, I c.

My only concern is that i didnt want a file openable in text pad that would just contain most of the games stuff so any random person could change the story..I want people to have to at least go through the effort of decompiling etc to be able to change it without my concent because if they ask i would just give them all the development files to work with so they can make a new storyline altogether.

Hope that makes more sense..

Share this post


Link to post
Share on other sites
I am having a problem now extending python with C++. I have succesfully created a DLL with the help of swig for the "glue code" and can import it with python and even call functions from it. I was using SDL initially to create the window for the game and the graphics (to allow porting to other OS's) but when I call the "Init" function from the DLL which should create a window it creates it then the window stops responding, it doesnt respond to any clicks and if another window goes infront of it then it wont redraw if it gets focus again.

Also when i tried to draw some text to the window it just crashed.

Any ideas?

Share this post


Link to post
Share on other sites
Do you know about the Windows message pump and all that? Are the messages being handled appropriately? Not much we can say about the crash without seeing the code, although I doubt it has anything to do with the Python side.

Try perhaps calling the DLL from C++ while you debug it?

Share this post


Link to post
Share on other sites
Seems like you will be doing most of the game stuff in Python so I just thought I'd point out this: http://pygame.org/. It's a python wrapper for SDL.. but if you already have an engine working in C++ then it's not really useful.

Share this post


Link to post
Share on other sites
I may look into PyGame but as I do already have a working engine I would rather use my existing stuff.

Yes I do know about the windows message pump, thats what I thought first that I may need to add a new function in the DLL to handle that and run it in a while loop within the main Python script.

Share this post


Link to post
Share on other sites
I have decided to use a C++ exe which calls out to python for quests/dialogue/items but I need some help understanding how this should work. The amount of information on the internet seems very limited. Can some one explain how the main loop of the programme should work to incorporate python like this.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
(i'm the previous anonymous poster)...

From my experience, I would rather stick this extending python with C++, and find how to deal with the pump.

As for the final exe, py2exe is your friend for this... Or you can compile Python as your own exe, that know which module to launch ( but it's REALLY trickier ).

Have a look at this too :
http://twistedmatrix.com/users/glyph/rant/extendit.html


If you want to give some more chance to extending, have a look at Panda3D ( a free professional Python-based Game engine ), and at PyGame to see how they deal with the pump.

If you want embedding, it's really easy, and Python docs are enougth.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this