Horde3D

Next-Generation Graphics Engine
It is currently 16.06.2024, 05:27

All times are UTC + 1 hour




Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: 24.08.2008, 04:23 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
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 ?


Top
 Profile  
Reply with quote  
PostPosted: 24.08.2008, 10:40 
Offline

Joined: 05.03.2007, 19:38
Posts: 167
Location: Romania
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.

_________________
Paul


Top
 Profile  
Reply with quote  
PostPosted: 24.08.2008, 22:56 
Offline

Joined: 22.11.2007, 17:05
Posts: 707
Location: Boston, MA
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.

_________________
Tristam MacDonald - [swiftcoding]


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 01:18 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
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?


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 04:42 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
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


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 06:10 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
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.


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 12:12 
Offline

Joined: 22.05.2008, 18:30
Posts: 37
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!


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 18:09 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
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:


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 18:30 
Offline

Joined: 04.04.2008, 16:28
Posts: 13
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:)


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 18:58 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
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:


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 19:19 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
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:


Top
 Profile  
Reply with quote  
PostPosted: 25.08.2008, 20:26 
Offline

Joined: 15.06.2008, 11:21
Posts: 166
Location: Germany
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).


Top
 Profile  
Reply with quote  
PostPosted: 26.08.2008, 01:28 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
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:


Top
 Profile  
Reply with quote  
PostPosted: 26.08.2008, 13:28 
Offline

Joined: 04.04.2008, 16:28
Posts: 13
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.


Top
 Profile  
Reply with quote  
PostPosted: 26.08.2008, 14:25 
Offline

Joined: 22.11.2007, 17:05
Posts: 707
Location: Boston, MA
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

_________________
Tristam MacDonald - [swiftcoding]


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 40 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group