A newbie has problems with Lua

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

A newbie has problems with Lua

Vegetable
I have some problems :

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).

2) In a Lua-function i have written it says it can't index (?) on a line
where is only 'end'.


Someone can say me what is going wrong? Or you need more info?
-- 
View this message in context: http://www.nabble.com/A-newbie-has-problems-with-Lua-tf2102860.html#a5795187
Sent from the Lua - General forum at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Luiz Henrique de Figueiredo
> 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).
> 
> 2) In a Lua-function i have written it says it can't index (?) on a line
> where is only 'end'.
> 
> 
> Someone can say me what is going wrong? Or you need more info?

We sure need more info. See http://www.catb.org/~esr/faqs/smart-questions.html

Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Vegetable
In reply to this post by Vegetable


Vegetable wrote:
> 
> 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))
        {
            writeStack(L);/* does not affect the stack*/
            lua_pushstring(L, "argument non numÃrique donnà à 'tex_draw'");
            lua_error(L);
        }
    static_cast<Texture*>(fl->GetData(
        lua_tointeger(L,1),TYPE_TEXTURE))
                        ->GetImg()->
                            Draw(lua_tointeger(L,2),
                                lua_tointeger(L,3),
                                lua_tonumber(L,4));
    return 0;
}


The error come in 

function draw_map()
        for i=0,map_size_x-1 do
                for j=0,map_size_y-1 do
                      tex_draw(abs(map[i][j]),(i-x_map)*32,(j-y_map)*32,1)
                end
        end
end

I have organized my program to show pages, and it draws the map only in page
0.

When i change the page and i come back to the page 0 there is the error.
Adding a print(".") instruction before the tex_draw(...) solves the
problem..... 






-- 
View this message in context: http://www.nabble.com/A-newbie-has-problems-with-Lua-tf2102860.html#a5795533
Sent from the Lua - General forum at Nabble.com.



Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Fabian Peña
I think that, you have problems of corruption of the memory in your
code,by  example, one buffer overflow in a certain function of âCâ of
your program.
This can cause to the corruption of environment lua and his stack
explaining the use of print (â. â) that randomly seems to resolve the
problem.

Bad english??. I'm sorry :-)


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Joe Smith
In reply to this post by Vegetable

"Vegetable" wrote in message [hidden email]">news:5795533.post@......

The error come in

function draw_map()
       for i=0,map_size_x-1 do
               for j=0,map_size_y-1 do
                     tex_draw(abs(map[i][j]),(i-x_map)*32,(j-y_map)*32,1)
               end
       end
end

That code looks odd...

Check that you really want
"(i-x_map)" and not "(x_map-i)".
Same with y_map.


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Vegetable



From: "Joe Smith" <[hidden email]>
Reply-To: Lua list <[hidden email]>
To: [hidden email]
Subject: Re: A newbie has problems with Lua
Date: Mon, 14 Aug 2006 12:15:11 -0400


"Vegetable" wrote in message [hidden email]">news:5795533.post@......

The error come in

function draw_map()
       for i=0,map_size_x-1 do
               for j=0,map_size_y-1 do
                     tex_draw(abs(map[i][j]),(i-x_map)*32,(j-y_map)*32,1)
               end
       end
end

That code looks odd...

Check that you really want
"(i-x_map)" and not "(x_map-i)".
Same with y_map.



x_map and y_map is the position of the tile at the left and the top of the screen.

If i do x_map-i, if x_map==0 then i do (-i) what is not what i want

The problem is that something corrupt lua's internal state and i don't know what...

_________________________________________________________________
Retrouvez tout en un clin d'oeil avec la barre d'outil MSN Search ! http://desktop.msn.fr/


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Vegetable
In reply to this post by Fabian Peña



From: Fabian Peña <[hidden email]>
Reply-To: Lua list <[hidden email]>
To: Lua list <[hidden email]>
Subject: Re: A newbie has problems with Lua
Date: Mon, 14 Aug 2006 12:42:39 -0300

I think that, you have problems of corruption of the memory in your
code,by  example, one buffer overflow in a certain function of â??Câ?? of
your program.
This can cause to the corruption of environment lua and his stack
explaining the use of print (â??. â??) that randomly seems to resolve the
problem.

Bad english??. I'm sorry :-)


Your english is fine, I'm not english too.
Ok I'll try to chase them. I don't know where they hide but i'll find them!

_________________________________________________________________
Retrouvez tout en un clin d'?il avec Windows Desktop Search ! http://desktop.msn.fr/


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Sam Roberts-2
In reply to this post by Vegetable
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


Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Joe Smith
In reply to this post by Vegetable

"Cadé David" <[hidden email]> wrote in message [hidden email]">news:BAY107-F2114C15FA9C19A38414A92944E0@......



From: "Joe Smith" <[hidden email]>
Reply-To: Lua list <[hidden email]>
To: [hidden email]
Subject: Re: A newbie has problems with Lua
Date: Mon, 14 Aug 2006 12:15:11 -0400


"Vegetable" wrote in message [hidden email]">news:5795533.post@......

The error come in

function draw_map()
       for i=0,map_size_x-1 do
               for j=0,map_size_y-1 do

tex_draw(abs(map[i][j]),(i-x_map)*32,(j-y_map)*32,1)
               end
       end
end

That code looks odd...

Check that you really want
"(i-x_map)" and not "(x_map-i)".
Same with y_map.


>x_map and y_map is the position of the tile at the left and the top of the
screen.

Ok. And I assume i=0, and j=0 is also supposed to place at the top left of the screen correct? 0-(top of screen) gives a negative number under normal programing co-ordinate systems.
same with 0-(left of screen)

Perhaps I am wrong, but it looks like you will be drawing in places other than were you want. If you end up drawing offscreen that could potentially be a problem.



Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Mark Edgar
In reply to this post by Vegetable
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

Reply | Threaded
Open this post in threaded view
|

Re: A newbie has problems with Lua

Vegetable
In reply to this post by Sam Roberts-2
I'm responding to everybody :


From: Sam Roberts <[hidden email]>
Reply-To: Lua list <[hidden email]>
To: [hidden email]
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


My function writeStack did that! And the second argument was LUA_NONE.

Joe Smith wrote :
Perhaps I am wrong, but it looks like you will be drawing in places other than were you want. If >you end up drawing offscreen that could potentially be a problem.

No there is no problem with that. The only problem is efficiency... I should enhence this function.

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

My file system is a bit complicated ; fl is a fileList that keeps for each file the name and the data associated in an abstract class. GetData loads the data with the constructor depending on TYPE_TEXTURE if it is not loaded before ; and gives the data loaded else.
The parameters are :
a = index of the file in the filelist(int)
b = x(int)
c = y(int)
d = size(float)



-------------------------------



I have found the problem !

I tried to access and write non allocated memory...
In C, I use a table to get the length of a character. But for non english chars like 'é'<0! So on all my functions using text, i use now unsigned char* and not char*.

It solved the issue!

_________________________________________________________________
Découvrez Windows Live Messenger : le futur de MSN Messenger ! www.windowslivemessenger.fr