Archived

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

enygmyx

question regarding LUA

Recommended Posts

hi all, i have a question regarding script loading. if we send multiple requests to lua to execute the same script does it (internally) load and parse the script every time. or does it have some kind of caching system so that if a request for a previously loaded script is recieved it does load the script again. if it does not cache scripts, is there some easy and efficient way to implement this? i hope that i have expressed myself clearly, thanks in advance, enygmyx.

Share this post


Link to post
Share on other sites
hi ppl,
first of all sorry for the late reply but i was caught up in work ...... i will try to explain what i am doing in my program.
first i open a single lua state, which is used throughout the program, using:
m_pLUAState = lua_open();

then i initialize other things needed and register with Lua the C++ functions that i want to export to my scripts:

lua_register(LUAPtr, FuncNAme, FuncPtr);

then i simply call a script file, which is a simple local text file, using:

lua_dofile(m_pLUAState, Filename );

now what i think happens is that each time lua_dofile is called internally lua opens the file, loads it, parses it etc etc. and then closes it upon returning.

the question that i had (if the assumption above is correct, plz verify this) is since lua already opened and loaded a file once it should not do this again if the same script was called.

is there a way to manage this in a simple and efficient manner? or prefably if lua could do this on its own end.

thanks in advance,

sohyl.

P.S. comments would be really helpful if you suggest some code.

Share this post


Link to post
Share on other sites
quote:
Original post by enygmyx
then i simply call a script file, which is a simple local text file, using:

lua_dofile(m_pLUAState, Filename );

now what i think happens is that each time lua_dofile is called internally lua opens the file, loads it, parses it etc etc. and then closes it upon returning.

the question that i had (if the assumption above is correct, plz verify this) is since lua already opened and loaded a file once it should not do this again if the same script was called.


Well, no, it''ll do it all again. It has to, in case you changed the code in that file or something.

quote:
is there a way to manage this in a simple and efficient manner? or prefably if lua could do this on its own end.


What you probably want instead is to call luaL_loadfile or lua_load just once, to load it in. I''m not sure exactly what you do with it then as I think this part of the API is pretty poorly documented, which is odd as it''s incredibly important. You may get some clue from the source for the auxlib here:
http://www.lua.org/source/5.0/src_lib_lauxlib.c.html




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

Share this post


Link to post
Share on other sites
lua_load() compiles a script and pushes the compiled result as ''function'' in the stack.
if you have a script that look this



cooltable = {}

function foo()
print("I''m da script")
end



immagine that lua_load() will push something like that in the stack


function main()

cooltable = {}

foo = function()
print("I''m da script")
end

end


At this point, to actually load the script, you have to call the function with lua_pcall()

How does this solve you problem? Well, functions in lua are a first class type,
so you can just store them somewhere(in the registriy table for instance),and call them again and again
any time you need without recompile the script.
This will give you the same result of calling lua_dofile() just no compilation involved.

I hope I wasn''t to confusing

Alberto

-----------------------------
The programming language Squirrel
http://squirrel.sourceforge.net

Share this post


Link to post
Share on other sites
Here''s something I was working on a few months ago... you may find it usefull - or not.

Scriptable Classes class

Dave Dak Lozar Loeser

"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."
--anonymous

Share this post


Link to post
Share on other sites