lua-users home
lua-l archive

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

Hi, Karsten.

You are trying very hard to get some kind of support here, but I think nobody is actually understanding what you are saying.

Perhaps you should try to post a message written in german. I believe there is at least one german-speaking person in this list which could at least translate it to english.

Said that, and complementing that I'm not a C++ programmer, I'll try to make your example work, but I'm not sure if this is what you are trying to do and if it will actually work on a C++ environment.

static int MyPrint(lua_State *lua)
    // Get the string at param 1
    // Note that I added a "\n" to 'beautify' the output
    printf("%s\n", lua_tostring(lua, 1));

    // Pushes a string as result
    // I'm using the lua_pushliteral macro, albeit
    // you could just use lua_pushstring or lua_pushlstring
    lua_pushliteral(lua, "Answer Text");

    // How many values this function is returning?
    // In this case, the MyPrint function is just
    // pushing one value to the stack ("Answer Text")
    // but it could be two, three, four, ...
    return 1;


// Pushes into stack your function, with zero upvalues.
lua_pushcclosure(m_pState, MyPrint, 0);

// Put this function in the Globals table.
lua_setglobal(m_pState, "MyPrint");

/* Alternatively, you could also use the macro
   lua_register to do all this job:
   lua_register(m_pState, "MyPrint", MyPrint); */

In the Lua side, you would:

-- Note that I'm using MyPrint, not "trace"
v = MyPrint("42")
print("MyPrint returned: ", v)

Which would print to stdout, under normal temperature and pressure:

MyPrint returned: Answer Text

Good luck and best regards,

ps: I received your "I found the solution" message milliseconds before I hit the Send button, and I hope the comments on my message may be useful to you somehow.