lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Lua is not written to support C++ in any way, so exceptions are never needed to catch the results of lua functions.

I don't use luaL_check*, so I'm not 100% on those functions, but this should fix the loop. Also, I didn't compile this code, I just wrote it off the top of my head, so there might be an error!


int LCamera::setEffects(lua_State *L)


int arguments = lua_gettop(L);

lua_Number effect;

for (int i = 1; i <= arguments; i++)


effect = luaL_checkint(L,i);

cout << "effect: " << effect << cout;


cout << "number of arguments: " << arguments << endl;


return 0;



To return multiple values, push them onto the stack and then return the number of return values that are on the stack. Remember that the parameters are also on the stack so you will need to pop them off before you push your return values. You are guaranteed to have some standard amount of free space on the stack. I think the default is 16 free places on the stack when your C function is called. If you push more than 16, then you need to check if there is enough free space before you do that.

Jonathan Branam

Systems Analyst
Application Development
Ontario Systems
Tel  765.751.7384

From: [] On Behalf Of Zé Pedro
Sent: Tuesday, June 13, 2006 5:54 AM
Subject: Passing arrays(tables) between C++ and Lua and vice versa


I'm creating some functions in c++ and making them available to lua. Some of this functions required to return and/or receive arrays (in lua arrays are implemented with tables right?).
For example, I have a function called setEffects that can receive an indetermined number of ints. How can I do this? I have tried the following code:
int LCamera::setEffects(lua_State *L)
    int argument = 1;
        lua_Number effect;
        while (true)
            luaL_checktype(L, argument, LUA_TNUMBER);
            effect = luaL_checkint(L,1);
            cout << "effect: " << effect << cout;
    } catch (...)
        //Do nothing. Only checking number of arguments
        cout << "number of arguments: " << argument << endl;

    return 0;
I did this hoping that when there were no more arguments an exception would be thrown and catched in the try-catch block, but it isn't the case. An alternative is to pass as argument an array but how?

The other problem is to return a table. If I have a method that can return several values how can I do it?

Jose Tavares

PS: sorry for the poor english

Believe nothing, no matter where you read it, or who said it,
no matter if I have said it, unless it agrees with your own reason
and your common sense.
- Buda

Para quê ter olhos azuis, se a Natureza deixa os meus  vermelhos?
- Bob Marley

"Don't try to use what you learn from Buddhism to be a Buddhist;
use it to be a better whatever-you-already-are."
- His Holiness the 14th Dalai Lama

Idealism is what precedes experience; cynicism is what follows.
  - David T. Wolf

"Please don't download, because I want to get a pool in my second home."
  - Kanye West

Attention: This message and all attachments are private and
may contain information that is confidential and privileged.
If you receive this message in error, please notify the sender
by reply email and delete the message immediately.