Lua integration question

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
michaelg1987
Halfling
Posts: 71
Joined: Sat Mar 10, 2007 5:14 pm

Lua integration question

Post 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?
scratchyrice
Gnome
Posts: 324
Joined: Thu Apr 27, 2006 9:14 pm
Location: United Kingdom - England
x 1

Re: Lua integration question

Post 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
Intel I5 750 OC'd to 4.2Ghz per core, Geforce 580GTX 1.5Gb GPU, 280GTX for PhysX, 8Gb DDR 3 ram
User avatar
merlinblack
Goblin
Posts: 223
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7
Contact:

Re: Lua integration question

Post 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.
Last edited by merlinblack on Mon Feb 23, 2009 1:39 pm, edited 1 time in total.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
tdev
Silver Sponsor
Silver Sponsor
Posts: 244
Joined: Thu Apr 12, 2007 9:21 pm
Location: Germany
x 14

Re: Lua integration question

Post by tdev »

reptor
Ogre Magi
Posts: 1120
Joined: Wed Nov 15, 2006 7:41 pm
Location: Finland
x 5

Re: Lua integration question

Post by reptor »

Toluaa++ is quite easy to use... I didn't use the others so can't comment on them.
User avatar
merlinblack
Goblin
Posts: 223
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7
Contact:

Re: Lua integration question

Post 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
Last edited by merlinblack on Wed Apr 08, 2009 11:18 am, edited 1 time in total.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Re: Lua integration question

Post 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.
scratchyrice
Gnome
Posts: 324
Joined: Thu Apr 27, 2006 9:14 pm
Location: United Kingdom - England
x 1

Re: Lua integration question

Post 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
Intel I5 750 OC'd to 4.2Ghz per core, Geforce 580GTX 1.5Gb GPU, 280GTX for PhysX, 8Gb DDR 3 ram
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Lua integration question

Post 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:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Lua integration question

Post 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. ;)
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Lua integration question

Post 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.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Lua integration question

Post 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:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
merlinblack
Goblin
Posts: 223
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7
Contact:

Re: Lua integration question

Post 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>
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Re: Lua integration question

Post by nullsquared »

That, and Lua has Luabind. I'd like to see GameMonkey's equivalent.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7156
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 532

Re: Lua integration question

Post 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.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Lua integration question

Post 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 ;)
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7156
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 532

Re: Lua integration question

Post by Kojack »

Of course that's not enough reason to ignore the language, I just found it kind of funny. :)
User avatar
merlinblack
Goblin
Posts: 223
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7
Contact:

Re: Lua integration question

Post 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 >
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
scratchyrice
Gnome
Posts: 324
Joined: Thu Apr 27, 2006 9:14 pm
Location: United Kingdom - England
x 1

Re: Lua integration question

Post 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
Intel I5 750 OC'd to 4.2Ghz per core, Geforce 580GTX 1.5Gb GPU, 280GTX for PhysX, 8Gb DDR 3 ram
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7156
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 532

Re: Lua integration question

Post by Kojack »

I'm sorry, but I have to say this sounds extremely stubborn. Whoever said you are right?
166 years of mathematics?
:)
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Lua integration question

Post 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?
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
scratchyrice
Gnome
Posts: 324
Joined: Thu Apr 27, 2006 9:14 pm
Location: United Kingdom - England
x 1

Re: Lua integration question

Post 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
Intel I5 750 OC'd to 4.2Ghz per core, Geforce 580GTX 1.5Gb GPU, 280GTX for PhysX, 8Gb DDR 3 ram
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Lua integration question

Post 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:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: Lua integration question

Post 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 ;)
scratchyrice
Gnome
Posts: 324
Joined: Thu Apr 27, 2006 9:14 pm
Location: United Kingdom - England
x 1

Re: Lua integration question

Post 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
Intel I5 750 OC'd to 4.2Ghz per core, Geforce 580GTX 1.5Gb GPU, 280GTX for PhysX, 8Gb DDR 3 ram
Post Reply