lua-users home
lua-l archive

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


zweifel wrote:
I guess you are right JC, maybe I will just do a simple "dofile" and worry with performance after. But knowing that it has some way around to increase performance in a easy way is very important. I had already faced problems that were easier to start allover again than to optimize in the way things were implemented.

You're analyzing this way too hard --- this is much easier than you think!

When you load a Lua script it turns into a compiled chunk. Then you run the chunk.

A script like this:

function foo()
  print("foo!")
end

...turns into a chunk which, *when run*, will add a function foo() to the Lua VM. This function will remain there until the VM is destroyed (or more Lua code changes it).

One a function is in the VM, you can easily and efficiently look it up and run it at any time.

So all you need to do is to structure your AI code so that all the scripts get loaded once, on startup, and then all you need to do every frame is:

lua_getglobal(L, "foo");
lua_pcall(L, ...);


GrayFace, I do not get it... do you mean one script with a table where every script would register its own function?

Almost. The Lua VM will keep track of functions for you (all globals are stored in a table called _G). So you don't actually need to go to any extra effort to do so yourself.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "They laughed at Newton. They laughed at Einstein. Of course, they
│ also laughed at Bozo the Clown." --- Carl Sagan