Horde3D
http://horde3d.org/forums/

Why using LUA and Python scripts in game programming ?
http://horde3d.org/forums/viewtopic.php?f=1&t=469
Page 1 of 3

Author:  Siavash [ 24.08.2008, 04:23 ]
Post subject:  Why using LUA and Python scripts in game programming ?

I've seen many projects and teams that they use LUA and Python to script their game AI.
Why using them when C++ codes are 10x up to 20x faster ?

Author:  SpOOky [ 24.08.2008, 10:40 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

it has 3 uses in my project:

1)rapid prototyping. bodge together a rough version version of the app to see how things work together, because at this stage things are mode likely to change and without the need for recompiling things move faster; then for the final version recode in c++ where needed. (in this case you must have most of the engine exposed to the script language)

2)scripts used at loadtime, like npc's behavior, quest scripts. at this point performance isn't the most important thing for me. i always imagine a slider that has at one end performance and the other ease of work so i try to balance everything; use scripts because they are easier to work with than c++, while still having small load times.

3)for elements that tend to have variables that change offen, like settings for example.

Author:  swiftcoder [ 24.08.2008, 22:56 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Siavash wrote:
Why using them when C++ codes are 10x up to 20x faster ?
Because it isn't. Well written code in Stackless Python is often faster than the same rewritten in C++ (the high level code is much easier to optimise). Python code is also considerable faster to write than C++, again primarily due to being high-level.

In addition, AI is very rarely a bottleneck in games - you have to be doing some pretty fancy AI to overshadow rendering and physics.

Author:  DarkAngel [ 25.08.2008, 01:18 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Siavash wrote:
I've seen many projects and teams that they use LUA and Python to script their game AI.
Why using them when C++ codes are 10x up to 20x faster ?


Script execution might only take up 5% of the total execution time in a frame. If you speed that up by 10x, then you've gained an extra 4.5% of performance. This equates to a 30fps game being increased to 31.35fps.

So why would you choose to use scripts and sacrifice 1.4fps?
* 1-2fps it isn't really that much! If the user has v-synch enabled than the fps is going to be reduced to 30 anyway!
* C++ is designed for a professional programmer - if you let the game designer write C++ code then they will probably crash your game. It's much safer for non-professional programmers to use a safe scripting language.
* Scripts can be edited while the game is running - this is great for tweaking game-play!


If you do want to use scripts but are worried about performance, then why not try a JIT version of one of the scripting languages?

Author:  Siavash [ 25.08.2008, 04:42 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Thank you very much friends for cutting edge ideas and trix .

At last I have some n00b questions that makes any fried chicken to laugh :lol:

1.Which scripting language between Python and LUA or JIT versions is most powerful and easy to use depending on your experiences :?:

2.Does the JIT versions provide the ability to change scripts in realtime :?:

3.How to integrate them to game :!: (Please explain step by step as I'm n00b to this :idea: )

Again, thanks for your ideas and helps. :arrow: KEEP MOVING FRIENDS

Author:  DarkAngel [ 25.08.2008, 06:10 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

1) The syntax for Lua and Python is very different, but the capabilities are pretty much the same.
I suggest looking at some introductory tutorials for each of them and seeing which one you like better

2) Yes, the JIT versions generally act exactly the same way as the original versions, except that they have the ability to convert some of the code into native CPU assembly code.
If you unload/edit/reload a script, it should execute the edited version of it.

3) This varies widely depending on the scripting language.

With Lua it is kind of complicated, so there are some additional libraries that make it easier, here is an example showing how to use the LuaBind library to link Lua and C++ code together.

Author:  stuckie [ 25.08.2008, 12:12 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

I did this for my Honours Project - looking at why a script-based engine is a good idea, and specifically at Lua and Python :)

From my research, it depends greatly on what you're doing as to which language to go for, along with how you want to implement scripts within your engine. Lua handles quick configuration stuff very well, and can be bound to an engine very quickly. Python has incredible data structure support and can mangle lists with the best of them, along with having a huge library of extension modules available to you and a massive army of community supporters.

However, although Python is very powerful, it can also be very bloated, depending on what you want to do. Don't get me wrong, I really like Python, but for what I was aiming for, it was really unusable for the engine I created as the only "help" I was ever given was to write the game in Python, and extend Python with bits of my engine re-written into extension modules for it to call as libraries.

Lua, on the other hand, is just a dozen or so C files. You can fully integrate Lua into your engine ( as I have done ) or do the same as what I was recommended with Python - write the bulk of the game in Lua, and extend Lua with Modules.

Basically the two philosophies are Binding and Extending... Lua is much easier to Bind than Python - if you want to embed Lua within your Engine... otherwise they're both pretty even due to they can both have their interpreters as external DLL files. As I didn't go down the Extend route, I can't tell you which is easier for that. However, with Binding you've got a lot of overhead to consider. As DarkAngel pointed out, there are helper libraries such as LuaBind which'll get you on your feet quickly. Others I've seen and would recommend are SWIK ( which also has Python support ) and ToLua++ ( for C++ support ). Again, however, for my purposes, none of these were what I was after, so I went and bound Lua manually using the Proxy Method.

I'd also highly recommend that you choose a scripting language before implementing the majority of your engine, as contrary to popular belief, dropping in a scripting language near the end of an engine's production can be a total nightmare... working it in throughout engine development is FAR easier, and also means that your script language's power increases, as the engine's power increases.. so you can see the results of your labour immediately :) which is always good!

If you can give me till the weekend, I'll be starting to post up some Wiki stuff from how I utilised Horde in our game, along with how we scripted it in Lua, what we did to the converter, and a general post-mortem of how things went... I've just been lazy recently and taking a break for a bit after a ten week marathon code session!

Author:  Siavash [ 25.08.2008, 18:09 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

stuckie wrote:
If you can give me till the weekend, I'll be starting to post up some Wiki stuff from how I utilised Horde in our game, along with how we scripted it in Lua, what we did to the converter, and a general post-mortem of how things went... I've just been lazy recently and taking a break for a bit after a ten week marathon code session!


Thank you very much stuckie for helping n00bs :oops:

Already I have a little knowledge about Python basics, I will do some research about Lua to find the best :roll:

Author:  lzdude69 [ 25.08.2008, 18:30 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Quote:
Because it isn't. Well written code in Stackless Python is often faster than the same rewritten in C++


Dude, I want to see where you got that info from. I frankly don't see how that is even possible, but if it is...the cheers to Python:)

Author:  Siavash [ 25.08.2008, 18:58 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

DarkAngel wrote:
With Lua it is kind of complicated, so there are some additional libraries that make it easier, here is an example showing how to use the LuaBind library to link Lua and C++ code together.


Thanks a lot about those pretty ideas about LuaJIT and LuaBind .
They are so much useful :idea:

Author:  Siavash [ 25.08.2008, 19:19 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Thank you very much guys for guidance to find the best for my needs :oops:

Depending on some of community members the Python is the best because of it's wide range of components and community to help.
I have found a tool named Psyco. Psyco is a JIT for Python and it is same as LuaJIT but it have some problems such as High Memory usage :? (Depending on their offical website)

Then I have found the Lua easier, powerful and fastest one. :idea:
It's codes are very similar to C++ and JavaScript and they are pretty easy to learn.
Lua integrates to C++ code easily (Using LuaBind) and by equipping that with LuaJIT it will generate bineries that they are at least 4x faster than the normal Lua scripts.

Finally it's the best choice for n00bs like me ! :wink:

Other members, don't forgot to post your useful helps to forum and wiki :arrow:

Author:  phoenix64 [ 25.08.2008, 20:26 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Remember that LuaJIT only works on x86, not on x64 though, that's why I always prefer plain Lua (of course you can combine it, but for me it's not worth the effort).

Author:  DarkAngel [ 26.08.2008, 01:28 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

lzdude69 wrote:
Quote:
Because it isn't. Well written code in Stackless Python is often faster than the same rewritten in C++
Dude, I want to see where you got that info from. I frankly don't see how that is even possible, but if it is...the cheers to Python:)

I've heard this be called "Impedance Mismatching" before - "In any notational system, some systems are able to be represented more concisely or efficiently than others."

To solve the same problem in Python and C++ you might approach it from different perspectives and solve it in different ways due to the notational differences between the two languages.
In some cases it may be that the most natural algorithm to use in Python just turns out to be a more efficient (think big-oh notation) algorithm than the one which is most easily expressed in C++.
Of course you could later port the python algorithm over to C++, but the resulting C++ code might be bloated, hard to read or inefficiently implemented compared to the original python representation :wink:

Author:  lzdude69 [ 26.08.2008, 13:28 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

Quote:
To solve the same problem in Python and C++ you might approach it from different perspectives and solve it in different ways due to the notational differences between the two languages.
In some cases it may be that the most natural algorithm to use in Python just turns out to be a more efficient (think big-oh notation) algorithm than the one which is most easily expressed in C++.
Of course you could later port the python algorithm over to C++, but the resulting C++ code might be bloated, hard to read or inefficiently implemented compared to the original python representation


An algorithm well expressed in Python, and horribly expressed in C++, is hardly fair or accurate when comparing the performances of both. Also the Python Wiki claims that is is sometimes faster than C++, but they fail to provide any numbers or comparisons. But then again, it doesn't really matter, the whole point of Python/Lua is to be embedded/extend an application.

Author:  swiftcoder [ 26.08.2008, 14:25 ]
Post subject:  Re: Why using LUA and Python scripts in game programming ?

lzdude69 wrote:
An algorithm well expressed in Python, and horribly expressed in C++, is hardly fair or accurate when comparing the performances of both. Also the Python Wiki claims that is is sometimes faster than C++, but they fail to provide any numbers or comparisons.
There is also the matter of optimisations - high level languages are much simpler to optimise. If you look at C/C++, they are designed to map fairly closely to the underlying assembly code. This is good if you are hand optimising, but if you want to leave it up to the compiler, the compiler doesn't have much to work with. Python is modelled closely on concepts, rather than assembly, so the compiler/interpreter has an incredible amount of leeway to optimise. Then add in JIT, which lets the interpreter optimise specific sections of code on the fly, based on their usage patterns...

Quote:
But then again, it doesn't really matter, the whole point of Python/Lua is to be embedded/extend an application.
I do the bulk of my coding directly in python - not because the resulting executable is faster than C++ (it often isn't), but because it cuts the amount of code I have to write by about 85% :D

Page 1 of 3 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/