Python or Lua
ok here i go now i want to know what i should use i want to create a scripting system so that my designers can create their own scripts for our game. Now i was looking at both Python and Lua. Which is easier to use and implement with C++. I was looking at both languages and saw all the wrapper code you needed to make them work with C++. What would be easier to use.
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
A lot of people say Lua is easier, but then there are probably more decent tools to automate doing it with Python. However, using an automated tool generally means that you need to have several C++ functions or C++ classes that you''re happy to translate directly across. In most ways it is going to depend on what sort of C++ code you need to expose to the scripting language.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
January 05, 2004 07:59 PM
I''m currently looking at both languages for a game I''m working on. Having written some sample applications to embed both languages in C++, and also to export C++ functions to both languages, I can tell you that Lua is hands down easier to embed and work with. It is pretty much designed for this purpose, and it just compiles right in with your code. It''s also got very fast and easy to use facilities for binding your C++ functions to Lua.
Python is more work to embed and expose, partly because it''s not small, and it''s not compiled into your program (at least not the way I''m doing it). Using the actual Python C API to embed and expose Python is not so pretty, so you''ll probably be looking at using an additional library or wrapper generator (I''m using Boost.Python, which seems pretty cool) to expose your C++ code and classes. Python definately has a lot more steps to go through, and the added complexity and potential liability of another layer of software in the wrapper library you choose.
But for me, the question isn''t which language is easier but which will be more powerful and/or better suited to my purposes over the long run. The reason I''m still looking at Python even though Lua is such a breeze to embed is that Python is a bit bigger, more mature and featurefull. For example, with a wrapper like Boost.Python, you can expose your C++ classes to Python code, and even derive Python classes from C++ base classes. Lua by default does not have classes, you have to implement them yourself. So for me, the question of the moment is not which one is easier to work with, because I know it''s Lua, but whether or not I need all the extra features that Python provides. I would rather go with something that is more powerful and has more features than I think I need than get stuck either compromising with the feature set of another language or trying to hack extensions in. The Python feature I''m really hanging on right now is Python generators, which I think would be perfect for implementing AI. They are a form of microthreads built into the language, and what you can do is write a function that runs for a while and returns a result (they use the keyword yield instead of return) and then when you call the function again, rather than starting over it continues right where it left off, on the statement following the yield, with all previous state (local variable values, etc) saved.
There is an example of this in an article on Python in Gamasutra:
http://www.gamasutra.com/features/20020821/dawson_01.htm
and I believe this would be a much nicer way to implement AI than state machines, aside from the benefits of being able to keep the AI in scripts instead of C++.
Python is more work to embed and expose, partly because it''s not small, and it''s not compiled into your program (at least not the way I''m doing it). Using the actual Python C API to embed and expose Python is not so pretty, so you''ll probably be looking at using an additional library or wrapper generator (I''m using Boost.Python, which seems pretty cool) to expose your C++ code and classes. Python definately has a lot more steps to go through, and the added complexity and potential liability of another layer of software in the wrapper library you choose.
But for me, the question isn''t which language is easier but which will be more powerful and/or better suited to my purposes over the long run. The reason I''m still looking at Python even though Lua is such a breeze to embed is that Python is a bit bigger, more mature and featurefull. For example, with a wrapper like Boost.Python, you can expose your C++ classes to Python code, and even derive Python classes from C++ base classes. Lua by default does not have classes, you have to implement them yourself. So for me, the question of the moment is not which one is easier to work with, because I know it''s Lua, but whether or not I need all the extra features that Python provides. I would rather go with something that is more powerful and has more features than I think I need than get stuck either compromising with the feature set of another language or trying to hack extensions in. The Python feature I''m really hanging on right now is Python generators, which I think would be perfect for implementing AI. They are a form of microthreads built into the language, and what you can do is write a function that runs for a while and returns a result (they use the keyword yield instead of return) and then when you call the function again, rather than starting over it continues right where it left off, on the statement following the yield, with all previous state (local variable values, etc) saved.
There is an example of this in an article on Python in Gamasutra:
http://www.gamasutra.com/features/20020821/dawson_01.htm
and I believe this would be a much nicer way to implement AI than state machines, aside from the benefits of being able to keep the AI in scripts instead of C++.
As for boost.python, there''s a wrapper out there called luabind, also based on boost, which will do the same for lua.
Easy binding of your classes, makes it a breeze to work with. Downside, luabind can be very heavy on the compiler due it''s massive use of templates.
Lua has a feature called coroutines which can be used for threading (suspending and resuming scripts).
Once you''re down with binding it''s the simple decision which language style is more to your liking.
I personally heavily dislike the "feature" of python which enforces indention of the code (the indention is used to form code blocks). I usually do indent my code anyways but i sure as hell don''t want my script not to work because of a misalignment of tabs.
Easy binding of your classes, makes it a breeze to work with. Downside, luabind can be very heavy on the compiler due it''s massive use of templates.
Lua has a feature called coroutines which can be used for threading (suspending and resuming scripts).
Once you''re down with binding it''s the simple decision which language style is more to your liking.
I personally heavily dislike the "feature" of python which enforces indention of the code (the indention is used to form code blocks). I usually do indent my code anyways but i sure as hell don''t want my script not to work because of a misalignment of tabs.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
quote: Original post by EndurionDon''t use tabs; make whitespace visible; or don''t use tabs.
I personally heavily dislike the "feature" of python which enforces indention of the code (the indention is used to form code blocks). I usually do indent my code anyways but i sure as hell don''t want my script not to work because of a misalignment of tabs.
Did I mention "Don''t use tabs"?
uh well with Lua will i have to compile my game every time i modify the scripts or are the scripts saved outside of the main app.
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
Lua stores scripts separately from the executable (unless you force it to only load script text from the executable).
Lua is so much smaller that it should be the obvious choice, unless you *KNOW* that you *REALLY* need all of the features of Python.
Lua is so much smaller that it should be the obvious choice, unless you *KNOW* that you *REALLY* need all of the features of Python.
enum Bool { True, False, FileNotFound };
January 06, 2004 06:24 PM
You don''t have to recompile at all, at least if you don''t implement it in a very strange and weird way ![](smile.gif)
What''s great is that with lua in your developper version of the game you can have all the lua scripts be in text based form and then just compile them with the lua compiler when you distribute your game without doing any modifications to your code.
I don''t have any experience with python so I can''t talk about it. You might also want to look at Ruby. It''s a very nice OO scripting language though I wouldn''t recommend it if the other people supposed to use the scripting language don''t know how to code in a language already.
![](smile.gif)
What''s great is that with lua in your developper version of the game you can have all the lua scripts be in text based form and then just compile them with the lua compiler when you distribute your game without doing any modifications to your code.
I don''t have any experience with python so I can''t talk about it. You might also want to look at Ruby. It''s a very nice OO scripting language though I wouldn''t recommend it if the other people supposed to use the scripting language don''t know how to code in a language already.
quote: Original post by Oluseyiquote: Original post by EndurionDon''t use tabs; make whitespace visible; or don''t use tabs.
I personally heavily dislike the "feature" of python which enforces indention of the code (the indention is used to form code blocks). I usually do indent my code anyways but i sure as hell don''t want my script not to work because of a misalignment of tabs.
Did I mention "Don''t use tabs"?
Thanks
![](smile.gif)
I never use tabs, i always convert tabs to space.
[rant]TABS ARE TEH EVIL!!1[/rant]
Just wanted it mentioned though...
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
quote: Original post by hplus0603Actually, that''s an inaccurate assertion.
Lua is so much smaller that it should be the obvious choice, unless you *KNOW* that you *REALLY* need all of the features of Python.
One of the factors that you should consider when choosing a "glue" or extension language is who will be using it. If this is a one-person project, then you can select based on your coding style preference (hate/love whitespace delimiting, etc) or the ease of integration. If the scripting language is to be used by, say, artists writing behavior scripts, then the learnability and comprehensibility of the language - as well as giving them a fair amount of expressive power - become primary concerns.
Python is extremely easy to learn. Programmers used to C-style block-delimited code may find Python''s use of whitespace annoying, but it makes sense to non-programmers (just give them an environment set up to replace tabs with whitespace). Python is easy to read. Python is very powerful. I''m not knocking on Lua; I don''t have any hands-on experience with it, but I find its syntax to not have those benefits for non-technical users.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement