Getting output from a Lua script

Started by
9 comments, last by KurtCPP 15 years, 10 months ago
Hi all! This question might be very strongly related with one of the previous threads of this forum but I'm not sure. If so, I hope it won't bother you. Here is my problem: I was trying to execute some Lua script from a C++ application using Luabind (it should be the last version of Luabind) but neither luaL_dostring nor luaL_dofile worked. They both return 1. Although I don't have the official docs for Luabind yet, I guess this means that the functions both encountered an error. The print statements within the Lua script doesn't work either. I tried to print constant values and return values from C++ methods but none of them does anything. Could someone indicate whether I need to enable some Lua libraries to get some output or if something needs to be initialized first? My code first opens a Lua state with lua_open and then initializes it for Luabind through luabind::open. Thanks to all in advance.
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Advertisement
I couldnt get the standard output working for lua either, never bothered with stdout before or worked with it. The print function is defined within the file

lbaselib.cpp

specificly the function luaB_print after the line s = lua_tostring(L, -1);, I just hook my debug output function into their, which prints to my debug print functions. I think u can override the stdout functions as they are just global funciton pointers, but i'm not sure, and I build my Lua with my code anyways so I didnt need too.

Also if your working with visual studio, you can modify the error output format of lua to make it compatibale with visual studios format which allows you to jump to the particualr file/line from the ide, which is useful for intergrated developement.

Good Luck!

-ddn

Hey, I was wondering what kind of application you are using to run luabind? My programming friend and myself are attempting to use it in a win32 application and are getting a variety of errors. Its quite frustrating really.

Oh, and the insanely frustrating part is that we have it working just fine in a win32 console app.

I'm willing to post specific error messages and build procedures if that will help.
Hi, thanks for your answers!

I'm actually just making a small test application before running into a real project. It's just a console based sample RPG compiled with G++ under Linux.

I just have a player and many chests with different contents that may be locked or not. My current purpose is to create and configure the chests (and possibly the player) from the Lua code.
Afterwards, I would like to get into the most important part of RPG scripting, that is scripting peculiar behaviours : for instance, I could decide that opening a specific chest would trigger the occurrence of a special event, depending upon the scenario, such as the arrival of a NPC, or a combat, or the chest could turn into a foo (remember Chrono Cross!!), etc.
So I'd first like to succeed to call C++ methods from Lua and, later, to possibly derive classes or override certain methods to get special behaviors, according to a scenario.

Regarding what you told me about stdout, I'd like to know whether there is not a "normal" solution. It seems weird to me that I must have "gimmick" manipulations to perform in order to get it to work. I'll still see whether I can succeed in doing something with the idea you gave me.

Thanks again!
Cheers!

--
edit: simply corrected some sentences
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Lua was written to be very portable C code and uses the very old school output/error functionality. It seems stdout is actually some sort of function pointer or file handle, depending upon implementation.

It clearly states within the lua code that you should override/implement your own print function if needed/nessecary, so it's quite correct to do so for your project. You can use the stdout, but from what i can tell it goes into the console or file, neither which displays to the debug output window when debugging an applciation, which is what i needed.

From reading the Lua messageboard you can also do this wihtin Lua itself by overrding the print function in lua to be some other native print function.

Good Luck!

-ddn
Hi, thanks again!
Your last answer has enlightened me a bit more. Actually, you mean most Lua users are likely to experience output problems because Lua developers couldn't foresee all the systems that would have to display text?
Therefore, you mean it's normal that most people have to customize their Lua code according to the system they are running?

Taking my own case, I'm currently using Linux and I would just need to re-adapt lbaselib functions to get them to work and later, when I go back to visual studio, I'll certainly have other modifications to perform?
Please tell me if I ever completely misunderstand what you told me!

One more question : you mentionned customizing Lua error output format to make it compatible with Visual studio. I was not aware of this possibility, but does that simply means that one just needs to output messages in a predefined format to make VS able to interpret them and to create appropriate links?
If you have links to documents about this, could you give me them please?

Thanks for your advice, cheers!
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
It's not so much a case of customising Lua, more a case of including your own output function in the code that you bind it to.
Lua proably more than any other embeeded scripting language I've tried, is very moddable. That is customizable to the users needs , due to the fact it's only some c files and very well written ( it's on its 5th iteration ). It's very easy to follow what and how Lua works internally.

As I've mentioned you can do most if not all of these modifications within Lua itself using function overrides for base libaries functions( ie print ) to redirect your output to a custom c print function. This is proably the "cleaner" way, as it doesnt change Lua implementation itself.

But Lua in no way limits you to this path only. Since the footprint of Lua is so small, many developers build Lua into their projects staticly and customize it to their needs ( ie modifying output, custom memory manager, add native threading etc.. ) Lua wiki has a page just for these types of patches which extendend Luas capablity but also sometimes break compatablity.

Lua is a language made to be embbeeded and customized it seems, so feel free to do so. Use a source repository to keep track of your changes would be wise.

As for changing the error output to make them friendly for Visual studio. Just a change of error output format in these functions:

luaL_where,addinfo,luaX_lexerror

from "%s:%d: %s" to "%s(%d): %s" the 2nd format is compatibale with visual studio, if you redirect the Lua output to the output window using the OutputDebugString, you can double click onto the error and it will take u to the line/file of that error :D, so much easier to find fix errors that way.

Good Luck!

-ddn


Hi!
Effectively, I found the lua_error function that indicated me where and what was the cause of the error. Is seems that the Lua print function did not exist at all. Since I had no time (or rather no motivation) to modify and recompile lbaselib.cpp as you told me, I just tried to re-define a C++ print function, that would just call "cout<<" on the passed in string. And it worked!!!!
Then I also added a println function.
What seems weird to me is that the print Lua function used to work when I only used Lua without any C++ binding. Maybe there is an inline function defined in one of the Lua headers, I don't know.

Regarding what you told me about VC++, thanks also.
Also thanks to you all that helped me about this concern, cheers!!
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Did you ever call luaL_openlibs or one of the luaopen_* functions in your C++ code? If you didn't, then your Lua state won't have any of the standard Lua functions available, including 'print'.
Problems with Windows? Reboot! - Problems with Linux? Be root!

This topic is closed to new replies.

Advertisement