Sign in to follow this  

Getting output from a Lua script

This topic is 3484 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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
Hi,
Yes I did try to call lua_openlibs (I don't remember the precise name). What is weird is that I did not even need to explicitly load the libs when I used bare Lua but the print function still used to work, whereas with Luabind, print doesn't work at all, either with or without the libraries loaded.

Still, I think it is not such a big problem since when I bound my pwn print function to Lua, I suddenly realized that it was completely useless. lol.
Actually, I just use it now because I'm not that fluent with Lua but when I wrote my own Lua book soon (or not) and code a real game, I don't think I will really need to get output from Lua and if I ever need it, my own new hand-made print and println functions based upon cout will perfectly do what I want.

I may have already said that here, but I think print didn't work with Luabind because I had not included exactly the same headers. I'm not fluent with Lua now so I don't remember precisely the headers I had used, but this is the only reasonable reason I can see.

Cheers dudes!!
Thanks again to all of you!

Share this post


Link to post
Share on other sites

This topic is 3484 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this