Archived

This topic is now archived and is closed to further replies.

Sneftel

Lua embedding

Recommended Posts

Has anyone had much experience using Lua as an embedded scripting language for their game? I''d be very interested to hear how werll it worked for you, what caveats you discovered, etc. I searched the forums, and found plenty of posts by people suggesting Lua, but not much real-world information about what they''ve done with it. Specifically, I''ll be grafting Lua onto C++ for a (LucasArts-style) adventure game engine. Objects in the game world will be Lua pseudo-objects, with certain callback functions overridden on a per-instantiation basis to change the default behavior (i.e. a locked door''s "use" callback would make sure the key was in the character''s inventory before passing on to the default door use callback). Does this sound reasonable? What have been you guys'' experiences with Lua?

Share this post


Link to post
Share on other sites

I believe Relic are using it for Homeworld 2. No experience personally, but if Relic are using it after evaluating the alternatives (including writing your own scripting language engine) then I guess it can''t be all bad .

Share this post


Link to post
Share on other sites
Baldurs Gate, too. I''m more interested, tho, in game developers around here who have experience in it; Lua''s definitely got the cred, but I''m wondering about what insights into using Lua you guys have... stuff on par with "make your destructor virtual".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I was implenting it in the QuakeWorld console, gave up after 78 hours of intensive hacking....

Share this post


Link to post
Share on other sites
i am using LUA 4.0 at the moment.

very easy to integrate with VC++.

script runs quite fast too..

Just that the scripts are not object oriented and you have to define each function static. quite troublesome sometimes but overall its nothing compared to the power of the LUA language..

Its my duty, to please that booty ! - John Shaft

Share this post


Link to post
Share on other sites
I''m kind of resurrecting this topic to make my question, as it perfectly fits into this topic''s subject.

I''m considering using Lua as the script language for a MMORPG server.

I''d like to code NPC''s AI with it, mainly.

However, the most recent Lua version still doesn''t support concurrent scripts.

That''s certainly something I need, since a server would control over 10.000 NPCs. It''d also make scripting much easier, as I would be able to have blocking functions such as "PromptPlayer".

The missing functionality I feel in Lua is a lua_run_chuck( unsigned char *chuck, unsigned long checksize, lua_State *L, unsigned long maxinstructions ) function, to limit the amount of instructions that a script can execute at once.

I couldn''t find any solution to this problem searching the web.

Can anyone here enlighten me? Or even confirm me that Lua isn''t for my project.

Share this post


Link to post
Share on other sites
Actually, I''m writing a 3D engine for the creation of a real time strategy game and myself and my friend Eric are programming an in-house scripting engine which we''re calling MindScript. Its structure is very similar to C and very easy to use. I hope to get it to the point where I can release it so that other programmers can implement it into their games.

Mindgamez Entertainment

Share this post


Link to post
Share on other sites
I''m using LUA for scripting in my engine.Although I''m not using it''s full power just yet,I did put out a little demo on my engine''s webpage that allows you to control a cube using a script file.The script that comes with the app just bounces the cube up and down but it should give you an idea of how to proceed in modifiying the script to make the cube do other things too.

My engine''s website is at:

bioengine.biosphereent.com

Goto the downloads section and grab the CubeScript demo and try it out.

Like I said,I''m not fully utilising LUA just yet.I''m trying to figure out the best way to have LUA do the AI for a game I''m working on.That''s when things will start to get really interesting

Hope this helps a little.Email me if you have any questions at thegecko@biosphereent.com

Cheers.

Share this post


Link to post
Share on other sites
quote:
Original post by HellRaider
However, the most recent Lua version still doesn''t support concurrent scripts.


That depends on what type of concurrency you want. The development version of Lua includes non-preemptive concurrency, and a patch for the current stable version will give you this too.

If you want preemptive concurrency, you can achieve this through threading, by maintaining a different lua_State for each thread. They are guaranteed not to step on each other''s toes.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by HellRaider
I''m considering using Lua as the script language for a MMORPG server.

I''d like to code NPC''s AI with it, mainly.

However, the most recent Lua version still doesn''t support concurrent scripts.

That''s certainly something I need, since a server would control over 10.000 NPCs. It''d also make scripting much easier, as I would be able to have blocking functions such as "PromptPlayer".

The missing functionality I feel in Lua is a lua_run_chuck( unsigned char *chuck, unsigned long checksize, lua_State *L, unsigned long maxinstructions ) function, to limit the amount of instructions that a script can execute at once.

I couldn''t find any solution to this problem searching the web.

Can anyone here enlighten me? Or even confirm me that Lua isn''t for my project.

This is exactly the reason I don''t have Lua in my game. I can''t afford to have my designers locking up the game every time they accidentally make an infinite loop in an NPC script.

You might want to look on Google for the ''yield'' patch for Lua, written by Thatcher Ulrich, I believe. I exchanged a few brief emails with him, and while I decided that it still wouldn''t suit my needs, it might suit yours. (I think I came to decide that having a few thousand Lua_states would be too memory intensive.)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
We are using LUA in our RTS massively for AI and mission scripting. You are able to control nearly everything from within the LUA scripts, from unit behaviour to thread creation to sound volumes. I''m really happy with LUA, it''s easy to use and executes fast. I guess LUA could suit your needs very well, but I have no experience with other better known script languages like python or tcl, so you might consider examining them as well.

Regards,
Matt

Share this post


Link to post
Share on other sites
Hey Matt, would you be able to post any short examples of your scripts? A lot of people on the forums consider using Lua but rarely get any examples on how exactly it can be useful.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
i have a sample .. its from my 3d engine.


  
function Abort(title,msg)
MessageBox(title,msg);
PostQuitMessage();
end

function InitWIN32()
-- createwindow name width height fullscreen
local ret = CreateWindow("Polaron",800,600,0);

if ret ~= 0 then
Abort("win32",Win32GetErrorString(ret));
end
end

function InitGL()
local ret = GLLoad();

if ret == 0 then
Abort("opengl","cannot start opengl");
end

GLInitCamera();
GLSetCameraPosition( 500, 35, 370, 501, 35, 370);


srt1 = GLCreateSkybox();
srt2 = GLSetSkyboxDimension(500,0,500,2000,2000,2000);
srt3 = GLSetSkyboxTextures("back.bmp","front.bmp","bottom.bmp","top.bmp","left.bmp","right.bmp");

if srt1 == 0 or srt2 == 0 or srt3 == 0 then
Abort("opengl","cannot load skybox");
end


local mapsize = 1024;
ret = GLInitHeightmap("terrain.raw",mapsize,16,"terrain.bmp");
if ret == 0 then
Abort("opengl","cannot load heightmap");
end



GLCreateQuadTree(mapsize,mapsize,5);

-- CreateMesh("dropship.3ds",500,30,500);

SetPlayerModel("tris.md2","rhino.bmp",500,30,500);
end

function ConfigureEngine()
-- set input 0 = win32 1 = directinput
SetInputType(0);

BindKey("w","forward");
BindKey("s","backward");
BindKey("a","strafe_left");
BindKey("d","strafe_right");
BindKey("c","console");
BindKey("lcontrol","crouch");
end

function debugmodes()
-- debug mode (1 to activate, 0 to deactivate)
-- 1 for wireframe
-- 2 for heightcells
-- GLMode(1,0);
end

ConfigureEngine();
InitWIN32();
InitGL();
debugmodes();



this script is to enable debuging using my engine. it just creates a level with 1 player model and lets you run in it.

Its my duty, to please that booty ! - John Shaft

Share this post


Link to post
Share on other sites
This is a sample of the scripting language that myself and Eric are developing for the MindEngine... we're calling it MindScript

        
#include func.mnd


//main script file



function main()
{
int X
int Y
X=10
Y=30
if ( X < Y )
{
printl("Hey X is smaller than Y, go figure")
}

int newnumber
newnumber = testfunction( 10 , 15 , 20)

do
{
X = X + 1
}
loopuntil ( X > Y )

printl("Hey, Now X is 31!")
}

function testfunction( a , b , c )
{
int temp
temp = a * b * c
return ( temp )
}


that was just a script showing some of the basics of the interpreter. We've added the ability to create functions that execute actual c++ code (compiled as part of the interpreter in separate header). This allows us to make a function called DrawSprite( imgname , x , y , x2 , y2... etc and then have it call like the actual D3DXSprite Draw command and pass it the variables to draw the sprite on screen. Perhaps not the best example of its growing usefulness but wait, I'll have better examples later and some real stuff for you to download =)

Mindgamez Entertainment

Edited for grammar =)

[edited by - neurokaotix on August 13, 2002 1:28:24 AM]

Share this post


Link to post
Share on other sites
One thing I haven''t seen explained is how these scripts can call in game functions.

I would guess they would have to be C functions, or static C++ member functions, but how does the Virtual Machine that interperates the scripts call them?

Just curious.

Share this post


Link to post
Share on other sites
Well for MindScript we create a function in a special header file that the interpreter calls called builtin.h. Inside of builtin.h we create our internal functions like say we want to add a function called LoadAMesh( meshname , x , y...etc. We create a new object called LoadAMesh and create the parameters that it will take. Then we write what is to happen when LoadAMesh is read in a script file. In this case we would have it take the variables you pass through it and then proceed to load a mesh at the designated x y an z.

Mindgamez Entertainment

Share this post


Link to post
Share on other sites
So if I understand correctly you would the VM would get an opcode for a function call, followed by the function "index". The index would lookup into a table to figure out the number of params and such like and the function to call?

Thanks.

Share this post


Link to post
Share on other sites