Re: A newbie has problems with Lua
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: A newbie has problems with Lua
- From: Cadé David <codename_68@...>
- Date: 2006年8月16日 11:11:20 +0200
I'm responding to everybody :
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: 2006年8月14日 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