Jump to content
  • Advertisement
Sign in to follow this  

LUA Command Window

This topic is 3292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I'm using an in-game console to debug and change my engine's global settings and to load models during run-time. In addition, I would really like to create an external LUA command window that runs in debug mode. This way I can load additional scripts, view globals, view debug info, and call functions directly. I guess a simple example would be to have a print() statement in one of my LUA scripts and view it in the command window. Is this possible? I can't seem to find any information on the subject, so I would greatly appreciate some direction. What I don't want to do is bind my console to LUA, but maybe this is the only way...

Share this post

Link to post
Share on other sites
redirecting lua's output to somthing other than std out can be tricky. What I ended up doing was redefining the print() function to redirect it to a c++ stream and then set the stream to one of my own streams that goes to log files/console/std out.

Here's the function I used to do that:

int LuaState::myPrint(lua_State *L)

int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++)
const char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
if (i>1)
*theOutputStream << '\t';
*theOutputStream << s;
lua_pop(L, 1); /* pop result */

*theOutputStream << std::endl;
return 0;

This is almost exactly what Lua's print() function actually does (taken from their source) but instead of using fprint(), I used my custom c++ stream. You can use whatever meets your needs.

To replace the function, I simply set this as the global "print" function.

//register the new print()
debug << "changing lua print function" << endl;

lua_pushcfunction(myState, LuaState::myPrint);
lua_setglobal(myState, "print");



Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!