[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua Performance questions
- From: Peter Cawley <lua@...>
- Date: Wed, 23 Sep 2009 14:30:06 +0100
On Wed, Sep 23, 2009 at 2:10 PM, zweifel <zweifel@gmail.com> wrote:
> do you mean I can load a script called "melee.lua" and then call
>
> lua_getglobal(L, "melee.lua");
> lua_pcall(L, ...);
>
> is that it?
no. luaL_loadbuffer takes a string of source code, and produces a
function on the stack as a result. This function has no name, it
exists only as an index on the stack. If you want to give it a name,
then you can call lua_setglobal to assign to a Lua global, or luaL_ref
to give a C++ int name, or do anything else that you can do with a
stack index. The other lua[L]_load* functions operate similarly.
The important point to understand is that the load functions all
produce a function as output. For example, if you luaL_loadstring the
following snippit:
---
print("Hello World")
print("From Lua")
---
The result is that a function similar to the following is placed on
the Lua stack:
---
function(...)
print("Hello World")
print("From Lua")
end
---
To execute the file multiple times, you call the resulting function
multiple times. To do so easily, it is advisable to give the resulting
function some kind of name so that you can remove it from the stack
and retrieve it again later. As previously mentioned, lua_setglobal is
one method of giving it a name, though if you're using C/C++, then
using luaL_ref might be easier, as then you have an int value which
you can use to retrieve the function.