tags:

views:

134

answers:

3

Okay, so when I run lua, I get something like:

lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> 

Now, I want a prompt like this, 1) in a GUI application I've written.

My GUI application can provide functions like: get_input_from_screen(); and write_this_crap_out_to_screen(); and more functions I can write as necessary

I also know how to embed a lua interpreter in my C++ code (short tutorial on the web)

What I don't know .. is how to connect the input/output of a lua interpreter with my GUI stuff.

Any help/links apreciated.

Thanks!

+1  A: 

The logic will look like this, I presume.

string s = gui_read();
string result = lua_interpreter(s);
gui_print(result);
Paul Nathan
+2  A: 

Why don't you take a look at the source code for the Lua stand-alone interpreter (lua.c) and see how Reoberto, et al. did it?

Judge Maygarden
+1  A: 

I was looking to do something like this also and just looking through the lua.c didn't really 'click' for me. It wasn't until I read somewhere that you want to implement/overwrite the print() function with your own did everything make sense to me.

The key is to write your own print function in your application that knows how to send lua output to the component you want -- whether that be a widget TextBox, ingame scripting console, another file or what have you. Then all you have to do is register that print function so lua knows about it.

Similarly, to get lua to process scripting input create a function that retrieves the string to process(for example, a widget text box), then pass that string to something like lual_dostring() or similar. Then set your input function to trigger on some event like when the user presses enter or when a button is clicked etc.

This site here has a very nice code example to illustrate.

Victor T.