[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: [NOOB] "remember" function
- From: David Given <dg@...>
- Date: Thu, 23 Aug 2007 21:05:13 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Shane Lee wrote:
> I believe I see what you mean. I included the C code in my earlier
> reply for your perusal, however I am not aware of any other methods of
> obtaining the results I require other than what I have already
Yep, that's what you're doing.
The thing to remember is that the lua_State, L, remembers all functions
defined by your scripts; and that a function()...end sequence in the script is
actually a *statement* that, when executed, defines a function.
So by running the script *once*, you define all your functions. Once defined,
you can call them; you just don't need to redefine them again.
> It does help tremendously. It tells me that I have absolutely no idea
> what I am doing :-P
Actually, most of your code is right; it's just, ahem, not quite in the right
The only thing you need to do is to split the code up so that luaL_dofile() is
only called once, and everything from lua_getglobal() onwards is called three
times. That should work. You also need to use lua_pcall() instead of
lua_call(). If an error occurs in the Lua script, Lua will attempt to throw an
exception using an internal longjmp-based mechanism; lua_pcall() will catch
these. lua_call() doesn't, so if something goes wrong, Bad Stuff happens.
You also need to be aware that const char* strings on the Lua stack only
remain valid for as long as the Lua string is on the stack... so as soon as
you call lua_pop(), ret dies.
┌── ｄｇ＠ｃｏｗｌａｒｋ．ｃｏｍ ─── http://www.cowlark.com ───────────────────
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----