[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: When are global names registered within th VM?
- From: Dolphin <dhawkins@...>
- Date: Mon, 16 Mar 2009 15:35:44 +0000 (UTC)
kathrin_69 <at> gmx.de <kathrin_69 <at> gmx.de> writes:
> 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()
> 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
START = function()
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))