Page 1 of 2

Lua integration question

Posted: Wed Feb 11, 2009 1:44 am
by michaelg1987
When it comes to integrating Lua into your Ogre3d apps, how do you handle calling scripts? Let's say that I have a bunch of objects, each with their own Lua file, and each file with several functions (maybe onTouch, onTick, etc). Should I just read in all of the files and then tell some singleton that has the interpreter loaded to run the appropriate function?

Re: Lua integration question

Posted: Mon Feb 16, 2009 4:15 pm
by scratchyrice
You should check out GameMonkey. Much more efficient, only 50kb's in memory, C syntax too. LUA is embeded from birth as "the only scripting system", When infact for games there are better alternitves such as gamemonkey. Actualy i want to know exactly what scripting system the game "Pirates of the carribean" uses, Because its awesomely modable.

Cheers

Scratchy

Re: Lua integration question

Posted: Sun Feb 22, 2009 2:59 am
by merlinblack
Depends how you are binding functions or not. If you where using luabind, I guess you might not be asking this question. However luabind is quite heavy. There are other lighter similar things like luabridge etc.

Here's how I did it on a Allegro 2d scroller game once. I'd probably use luabridge if I did it again but this might give you some ideas.

I did my binding manually. What I did was have a class (could have been a singleton) with functions for interacting with the one and only lua instance I had. I had a c++ class called Actor and this had OnDeath, OnPickUp, OnHit, etc, which where handled by Lua scripts. Also Actor had a bunch of accessor methods (e.g GetPos) that where bound to Lua. All the c++ accessor methods where static, and where explicitly passed a 'this' pointer from Lua. These where stubs and called non-static methods using the this pointer sent from Lua. The OnDeath etc, called Lua and supplied the 'this' pointer as light user data. All the lua functions for the Actor class where in the Actor lua table.

Once I had that straight, I had had various types of Actors (Killer frogs, the actual hero herself etc) as classes inherited from the Actor class. In Lua, these new classes where copies of the Actor table with adjustments, i.e. most On* methods the same, but some where overridden.

Clear as mud eh?

You could keep your separate files in the same lua state, using tables to keep them apart, i.e. load each file's functions into a separate table. Kind of like having namespaces.

As I said above I'd most likely use a binding library next time. The above method got a bit clumsy. :roll: But it worked. :)

A library like luabind or luabridge does the function stubs, and this pointer stuff for you. luabind will also allow you to bind class varibles and generate get/set functions for you.

There is also 'tolua' and 'tolua++'. I don't know much about these except that they require a pre-compile step. CEGUI uses one, I think.

Re: Lua integration question

Posted: Mon Feb 23, 2009 1:16 pm
by tdev

Re: Lua integration question

Posted: Mon Feb 23, 2009 7:30 pm
by reptor
Toluaa++ is quite easy to use... I didn't use the others so can't comment on them.

Re: Lua integration question

Posted: Mon Feb 23, 2009 11:19 pm
by merlinblack
EDIT: Updated links.

I just remembered this code I wrote awhile ago: http://www.atkinson.gen.nz/code/luaobject.h which you may find useful. I wrote some blerb about it here http://www.atkinson.gen.nz/talking_to_lua.html

While luabind etc help you bind C/C++ to Lua to make it accessible from Lua, this code aims at making it easier to call Lua code from C++. The header file is all you need to use it. LGPL, but I'm really I'm just interested in any improvements people make.

merlinblack

Re: Lua integration question

Posted: Mon Feb 23, 2009 11:30 pm
by nullsquared
scratchyrice wrote:You should check out GameMonkey. Much more efficient, only 50kb's in memory, C syntax too. LUA is embeded from birth as "the only scripting system", When infact for games there are better alternitves such as gamemonkey. Actualy i want to know exactly what scripting system the game "Pirates of the carribean" uses, Because its awesomely modable.

Cheers

Scratchy
Not sure where you're getting your information from. GameMonkey is not industry proven. Lua is. You might also want to check out Squirrel.

Re: Lua integration question

Posted: Tue Feb 24, 2009 12:06 am
by scratchyrice
GameMonkey is not industry proven
While this is true, Britney spears is "industry proven". Does not mean shes any good, Just means shes more advertised ;).

Cheers

Scratchy

Re: Lua integration question

Posted: Tue Feb 24, 2009 12:15 am
by jacmoe
nullsquared wrote:Not sure where you're getting your information from. GameMonkey is not industry proven. Lua is. You might also want to check out Squirrel.
Bollocks. :wink:

Lua was the only real alternative back in ye olde ages, besides Python. And TCL.

GameMonkey is not industry proven?
It *is* from the industry, in case you wondered.
The GM guys were allowed to release it to the public.
It's not a toy. :wink:

Re: Lua integration question

Posted: Tue Feb 24, 2009 12:51 am
by xavier
scratchyrice wrote:You should check out GameMonkey. Much more efficient, only 50kb's in memory
"Much more efficient"?!?

Each Lua VM is 10K...that makes your suggestion 5 times *less* efficient. ;)

Re: Lua integration question

Posted: Tue Feb 24, 2009 12:57 am
by xavier
jacmoe wrote:
nullsquared wrote:Not sure where you're getting your information from. GameMonkey is not industry proven. Lua is. You might also want to check out Squirrel.
Bollocks. :wink:

Lua was the only real alternative back in ye olde ages, besides Python. And TCL.

...


It's not a toy. :wink:

Yet, it *is* basically Lua.

From a risk standpoint (risk being measured as "how much work will it take for me to get the same out of it as I get out of Lua"), I'd still take Lua over GameMonkey.

Re: Lua integration question

Posted: Tue Feb 24, 2009 1:03 am
by jacmoe
xavier wrote:From a risk standpoint (risk being measured as "how much work will it take for me to get the same out of it as I get out of Lua"), I'd still take Lua over GameMonkey.
I probably would too. :wink:

Re: Lua integration question

Posted: Tue Feb 24, 2009 1:59 am
by merlinblack
jacmoe wrote:
xavier wrote:From a risk standpoint (risk being measured as "how much work will it take for me to get the same out of it as I get out of Lua"), I'd still take Lua over GameMonkey.
I probably would too. :wink:
Plus Lua has some nice books and printed reference manuals. "Programming in Lua" is also available free online.
Lua Gems looks interesting. It has a chapter ( 4 Gems ) on game programming.


<off topic>
scratchyrice wrote:While this is true, Britney spears is "industry proven". Does not mean shes any good, Just means shes more advertised ;).
Heh, I'll have to remember that one for certain meetings with die hard "use only software that EVERYONE including your dog has heard of." types. "Python is built in? Whats that? Perhaps we could get some outfit to add VBA instead?" heard moments before most of the room face palmed. :lol:

</off topic>

Re: Lua integration question

Posted: Tue Feb 24, 2009 3:10 am
by nullsquared
That, and Lua has Luabind. I'd like to see GameMonkey's equivalent.

Re: Lua integration question

Posted: Tue Feb 24, 2009 7:58 am
by Kojack
I don't want to take this thread even further off topic, but the rest of you are having fun...
Here's a fun little rant by the Game Monkey author about how quaternions are used in the games industry by elitists who want to show off their "l33t ski11z", and we should use 3x3 matrices instead: http://www.somedude.net/gamemonkey/foru ... f=12&t=380
:)

Right now I've become extremely interested in Falcon PL. I can't believe I've never looked at it before, because it's been around for 6 years and it's pretty cool. It combines a heap of programming paradigms like procedural (we all know that one), object oriented (with multiple inheritance), functional (unnamed functions, lambda calculus, other stuff which hurts my head), prototype (lua style object oriented, clone an object and modify it on the fly), message passing (add attributes to objects, broadcast messages to all objects with matching attributes) and tabular (kind of like a spreadsheet, combining data and lambda expressions). Plus there's coroutines, compile time meta programming, memory buffers (blocks of raw data you can do binary operations and stuff to, like parsing a binary file format), cross platform support, and embedding in C++ looks nice (although I haven't tried it yet). So far I haven't seen a single thing I don't like. I haven't decided yet if the syntax is as easy as Lua for beginner coders, I'll try replacing my 2d engine's lua system with Falcon and see how the game scripts look.

Anyway, back to the topic:
For lua, I load all the scripts in at the beginning (compiling is fast, but I'd prefer to get it out of the way before play starts) so they can set themselves up. All lua binding is done by hand, I don't use things like luabind or tolua. I support both userdata handles and text strings for identifying objects, and use a non object oriented style (functions which take an object, instead of objects with a method.
So I can do either of these:

Code: Select all

playership = addObject("ship1", "player")
setPosition(playership , 512,384)
or

Code: Select all

addObject("ship1", "player")
setPosition("player" , 512,384)
Every update I look for a lua function called update and pass it the delta time. In lua I can replace the update function at any time, as a form of state machine. So if I do update = menuupdate then the menu code will receive updates every logic step.

But at the moment I generally use one function to control all entities, I don't have separate lua scripts for each entity.

Re: Lua integration question

Posted: Tue Feb 24, 2009 8:42 am
by xavier
Kojack wrote:I don't want to take this thread even further off topic, but the rest of you are having fun...
Here's a fun little rant by the Game Monkey author about how quaternions are used in the games industry by elitists who want to show off their "l33t ski11z", and we should use 3x3 matrices instead: http://www.somedude.net/gamemonkey/foru ... f=12&t=380
Everyone is entitled to their own opinions, I suppose, even when they are wrong.

I guess Havok, to name one off the top of my head, are a bunch of elitists who would rather do things the hard way, considering internally they use the same vec/quat/vec transform package as does Ogre (who I guess are also a bunch of elitists), and Bullet, and...and only use rotation matrices in their tools. It wouldn't 'have anything to do with storage or speed, of course -- it's them just being 1337ists ;)

Re: Lua integration question

Posted: Tue Feb 24, 2009 11:09 am
by Kojack
Of course that's not enough reason to ignore the language, I just found it kind of funny. :)

Re: Lua integration question

Posted: Tue Feb 24, 2009 11:17 am
by merlinblack
Ah found it. Page 503, Game Programming Gems 7 - Automatic Lua Binding System by Julien Hamaide.

Interesting stuff. Its no more automatic than luabind in my opinion however it takes a preprocessor macro approach, rather than template meta-programming. This might make it compile faster and result in smaller code to luabind perhaps. Perhaps not. :wink:

< more off topic >
Ooooh, there's a GPG 8.... oh its a best of.....<looks at table or contents>....wtf "1.4 Squeezing More Out of Assaert" :shock: ......"2.3 Quarternion Compression"... heh :wink:
</ more off topic >

Re: Lua integration question

Posted: Tue Feb 24, 2009 4:38 pm
by scratchyrice
Here's a fun little rant by the Game Monkey author about how quaternions are used in the games industry by elitists who want to show off their "l33t ski11z", and we should use 3x3 matrices instead: http://www.somedude.net/gamemonkey/foru ... f=12&t=380
I do not really want to start a war here, But i could not agree more. Except, If a programmer uses quateroins instead of another rotation system, It simply shows off their dedication to looking cool, and their mathematical "leet ski11z" (lol nice phrase). I've been using simple Euler angles, for 9 years now, And ive never come across any performance, or any other issues. I know in theory there are problems like the gimbals lock, But in reality I've never come across any of these problems. But then again, In theory a 1.00ghz processor, Is faster than a 0.99999ghz processor, But in reality we will never notice anyway - So it does not matter.
Everyone is entitled to their own opinions, I suppose, even when they are wrong.
I'm sorry, but I have to say this sounds extremely stubborn. Whoever said you are right?

Cheers

Scratchy

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:04 pm
by Kojack
I'm sorry, but I have to say this sounds extremely stubborn. Whoever said you are right?
166 years of mathematics?
:)

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:19 pm
by xavier
scratchyrice wrote:Except, If a programmer uses quateroins instead of another rotation system, It simply shows off their dedication to looking cool, and their mathematical "leet ski11z" (lol nice phrase). I've been using simple Euler angles, for 9 years now, And ive never come across any performance, or any other issues. I know in theory there are problems like the gimbals lock, But in reality I've never come across any of these problems. But then again, In theory a 1.00ghz processor, Is faster than a 0.99999ghz processor, But in reality we will never notice anyway - So it does not matter.
I can't really give your 9 years of experience with Euler angles much weight, if you've never come across gimbal lock. That just tells me you haven't really done anything non-trivial, or have come across it but didn't recognize it (and have some hideous system of hacks and workarounds in place to deal with it).

To be honest, by your statements, I have to put you and the ranter in the same category -- those who don't understand quaternions and are afraid of them. So those who *do* understand them and use them, must be ridiculed as "elitist".

By your logic, we should all still just be happy to use C (or even assembly) and leave that new-fangled C++, C#, Haskell, script-language-du-jour, to those elitists dedicated to showing off their 1337 ski11z. Isn't assembly good enough for everyone?

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:31 pm
by scratchyrice
To be honest, by your statements, I have to put you and the ranter in the same category -- those who don't understand quaternions and are afraid of them.
You are right, I do not understand them, And so i am afraid of them. I've tried and tried to understand how they work, But then again I'm a programmer, NOT a mathematician. I want to spend my time learning to program, Not learn to use some rotation method, Which cannot be represented visually, And so in mine and most cases, outweigh the benefits.
By your logic, we should all still just be happy to use C (or even assembly) and leave that new-fangled C++, C#, Haskell, script-language-du-jour, to those elitists dedicated to showing off their 1337 ski11z. Isn't assembly good enough for everyone?
C++ vs Assembly gives REAL benefits. Yes Q's may be faster, But will we notice it in reality? Or is it just something in the back of people's head telling them they should use Q's simply because they are on paper, Slightly faster. In fact, By your logic - You prefer the harder, faster, Less understandable visually method (Just like assembly), Rather than the easy to understand, And visually imaginable method - euler angles, Like C++.
new-fangled C++, C#, Haskell, script-language-du-jour, to those elitists dedicated to showing off their 1337 ski11z.
That does not make sence, Making a game in assembly will always be harder and faster than making a game in c++, So the leets would want to use assembly no? Same with Q's vs Euler. Q's are faster, But the stupid complexities make it alot less worth while.

That being said, I can understand why it is used in engines such as ogre, As small performance impacts like that are very important. But higher up, like when using the ogre engine to make a game, No-one want the hastle of Q's - Or at least i don't. Like, If i wanted to make an object point east, In euler angles, we can instantly say (Using ogre's coord system) that if we rotate the object, about the x axis -90, The object will face east. With Q's where would i start? I would have to learn precisely how it works - spending years of my life - to be able to make that very usefull type of judgement. It's just not worth it in the long run... For most applications.

Cheers

Scratchy

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:40 pm
by jacmoe
Quaternions are superior. They store orientations using 4 numbers instead of three (Euler angles).
Interpolating between two Euler angles is problematic, to say the least.
Gimbal lock rears it's ugly head.

Don't try and understant quaternions. Just use them. :wink:

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:44 pm
by CABAListic
I don't know, I find axis/angle rotation a lot more imaginative/intuitive than Euler angles. And constructing a quaternion from axis/angle is very straight-forward. In fact, you don't even have to know, because any sensible Quaternion class provides the relevant conversion functions for you. Not understanding the maths behind quaternions doesn't mean you can't use them ;)

Re: Lua integration question

Posted: Tue Feb 24, 2009 5:49 pm
by scratchyrice
Don't try and understant quaternions. Just use them.
Indeed, Well i am atm for ogre. Its just annoying not knowing what's actually going on. Also, If i want to do a test for rotation in my engine, I have to convert from angles to quateroins, which kind of defeats the purpose really. I guess when i get an editor ready, I can just store all angles as quateroins, and not have to know what's actually going on. However, ive heard of all these advantages, such as being able to interpolate better, But how would i even work out how to do that, without first knowing how Q's work?
I don't know, I find axis/angle rotation a lot more imaginative/intuitive than Euler angles. And constructing a quaternion from axis/angle is very straight-forward. In fact, you don't even have to know, because any sensible Quaternion class provides the relevant conversion functions for you. Not understanding the maths behind quaternions doesn't mean you can't use them
Indeed, But as i just said, If you're converting from angles anyway, does it not defeat the purpose?

Cheers

Scratchy