Log in

View Full Version : Loading Multiple Script Files with LUA


Dan MacDonald
12-12-2003, 10:45 AM
I've grubbed around a little but I’ve been unable to find any documentation on how lua handles loading of multiple script files into the lua state. Does it replace the existing one? or just append the new lua symbols from subsequent files? I know I could find this out experimentally fairly easily, however I would appreciate being able to read how lua handles this.

I guess I’m really asking a deeper question, for those of you who have used LUA in your games, how do you manage multiple script files and an ever increasing amount of script functions?

DGuy
12-12-2003, 05:05 PM
When you run a lua script within a lua state, any globals (data or functions ) defined by that script become part of that state's global enviornment and are usable by any subsequent scripts run within the same lua state.

Lua 5.0 offers the ability to change the global enviornment of a function (i.e. with each call to the function you can set the globals that are available for use)
As to the second part of your question, how to handle multiple scripts files, I do believe it has alot to do with the nature of your application, but if it helps any, users have reported, via the lua mailing list, having success using both one "giant" state, and with using hundreds of individual states. It both cases, minimal or acceptable penalties have been realized in the area of resource usage (i.e. memory and/or cpu).

HTH,
David

patrox
12-13-2003, 01:20 AM
Speaking of lua, is it possible to declare and free variables in lua ?

here's my thought :
would it be possible to do a "blitz basic" using lua+ptk ?

thanks
pat.

Dan MacDonald
12-13-2003, 05:46 AM
You can declare variables with global scope in LUA.

The following is a sample LUA script.

gGuardSplashDelay = 500;

function test( actor )
if actor == "katsu" then
local actorID = GetActorID("guard1");
SetActorMovementPattern(actorID,0, 535, 1028, 535, 750);
end
end


And here is how you would access the Global In That Script from C++...


//puts the script global on the lua stack
lua_getglobal(m_pLuaState, "gGuardSplashDelay");

//retrives the global from the top of the stack
value = lua_tonumber(m_pLuaState,1);

//pops the global off the stack
lua_pop(m_pLuaState,1);

tristanj
12-13-2003, 06:00 AM
Here's a thread I made on flipcode about LUA. I thought it might be helpful to read the replies there, even though the topic's not the same.

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00005313&forum=general&id=-1