# LUA Command Window

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

## 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 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){   assert(theOutputStream!=NULL);   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;   theOutputStream=&warn;   lua_pushcfunction(myState, LuaState::myPrint);   lua_setglobal(myState, "print");

Cheers,

Bob