lua-users home
lua-l archive

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


kathrin_69 <at> gmx.de <kathrin_69 <at> gmx.de> writes:

> 
> 
> Hi,
> 
> I compile the scripts within my engine using luaL_loadstring.
> After that I want to check if a function named "START" is defined and if 
> so - call resume with that function on the top of the stack.
> 
> so if I have that script:
> 
> function START()
>   print("test")
> end
> 
> i check and try to start it using this code:

> Problem is: lua_getglobal(mpLuaVm, "START"); gets me "nil" on top of the 
> stack and I don't know why.
> Any ideas?
> 
> Thank you in advance!
> 
> 

It sounds like you have a misunderstanding in how loadstring works. 
luaL_loadstring does not actually execute any of the lua code.  It basically
wraps whatever you are loading into an unnamed function and pushes it on to
the top of the stack.  Loading and running are two independent steps.

loaL_loadstring(L, "function START() print("Hello") end") pushes the 
equivalent of:

function()
  START = function()
    print("Hello")
  end
end

onto the stack, which must then be executed with pcall. 

As mentioned above, luaL_dostring does both steps in one line of C code.  It
is actually a macro defined as: 
(luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))