From: Sam Roberts <sroberts@bycast.com>
Reply-To: Lua list <lua@bazar2.conectiva.com.br>
To: lua@bazar2.conectiva.com.br
Subject: Re: A newbie has problems with Lua
Date: Mon, 14 Aug 2006 12:59:45 -0700
On Mon, Aug 14, 2006 at 05:09:53AM -0700, Vegetable wrote:
> Vegetable wrote:
c'est vraiment drole. :-)
> > 1) Sometimes calling my C-function from my lua script, the arguments
given
> > in my script are 'lost' : i call tex_draw(1,2,3,4) and i receive in my
C-
> > function (1,None,3,4).
> >
>
> I add some info :
>
> the C-function is :
> static int tex_draw (lua_State *L)
> {
> int n = lua_gettop(L); /* number of arguments */
> if (n!=4)
> {
> writeStack(L);
> lua_pushstring(L, "mauvais nombre d'arguments passés à
'tex_draw'");
> lua_error(L);
> }
> for (int i=1;i<=4;i++)
> if (!lua_isnumber(L, i))
lua_isnumber() has side-effects, it will convert the argument to a
number if it isn't, and it can (so you can pass "5" and it will become a
number).
I find very useful for debugging to have a "stack print" utility like:
void print_stack(lua_State* L)
{
for(int i = 1; i <= lua_gettop(L); i++) {
switch(lua_type(L,i))
{
case LUA_TNUMBER:
printf("#%d number %f\n", i, lua_tonumber(L,i);
break;
// ... you get the idea!
if called before you call lua_tonumber(), a function like this would
answer the question of what exactly IS on the stack when you get
called, not just what isn't there.
Cheers,
Sam
Mark Edgar wrote: Consider this:
static int tex_draw(lua_State *L)
{
int a = luaL_checkint(L, 1);
int b = luaL_checkint(L, 2);
int c = luaL_checkint(L, 3);
int d = luaL_checkint(L, 4);
static_cast<Texture*>(fl->GetData(a, TYPE_TEXTURE))
->GetImg()->Draw(b, c, d);
return 0;
}
But please use appropriate names for the a,b,c,d variables.
-Mark