lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Also, consider a more usual solution. Here is a rough example just to illustrate it: 1) Use dofile for all Lua scripts in Scripts directory when your program loads
2) In scripts declare event handlers, like
function OnFrame()
(if there can be only one OnFrame function)
table.insert(OnFrame, funciton()
(to have many OnFrame event handlers)
3) Call OnFrame from your program

This way you can put many functions in one script, scripts can have initialization code and you can change event handlers under some circumstances (like, setting OnFrame = MoveHandler when a charecter is about to move).

----- Original Message ----- From: Peter Cawley
To: Lua list
Sent: Saturday, September 19, 2009 11:24 PM
Subject: Re: Lua Performance questions

On Sat, Sep 19, 2009 at 5:16 PM, zweifel <> wrote:
hmm, these are a good solution when I just want to run the script once.

But in my case I do want to run the Lua script every frame. I just wanted to
know if I should bother with a hash_table and luaL_loadbuffer or just use
luaL_dofile because Lua already does some caching of files that was already
read before.

If you want to run a script multiple times, then load it once
(lua_load, luaL_loadbuffer, luaL_loadfile, etc.), save the result (a
Lua function) somewhere, and then run it (lua_call / lua_pcall)
multiple times. You should assume that reading a file is expensive,
and that the parsing done by lua_load (and other load functions which
all call lua_load) is also expensive - avoid calling any load function
per frame, and avoid even more load functions which read files.