Passing full commands to Lua from C++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Passing full commands to Lua from C++

Post by Moosader »

Hey guys--

I'm working on a game in C++ that is using Lua for it's scripting language, and I'm using Luabind.

I was just wondering, if there's a command in Lua or Luabind where I could essentially just pass Lua a string of anything and have it return a value. I don't want to have to specify in C++ what the parameters or how many of them there are, I just want to pass an entire string and have Lua handle it.

For example, I might want to output a table's value
print( character["Bob"]["name"] )
Or call an existing Lua function
room_GetNeighbors()

etc. etc., for debugging purposes.

Thanks for any help.
-Rach
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Passing full commands to Lua from C++

Post by TheBuzzSaw »

Here is what I use to execute arbitrary Lua scripts on the fly.

Code: Select all

const char* source = "println(\"Hallo!\")";
int status = luaL_loadstring(luaState, source);
if (status)
    reportErrors();
else
    status = lua_pcall(luaState, 0, LUA_MULTRET, 0);
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: Passing full commands to Lua from C++

Post by THe Floating Brain »

I know Luabind has table's which you could probably do that with.
As far as sending a string to Lua I think (although I am not entirely sure, I haven't done Luabind for a few months)
STL types come per-wrapped, you could probably set a std::string as a global in Lua by doing the following...

C++:

Code: Select all

//Assume you have set up your Lua state/interpreter.//
std::string greeting = "Hello!";
luabind::globals( luaState ) [ "greeting" ] = &greeting;
Lua:

Code: Select all

print( greeting )
--Output: Hello!
I hope I answered your question.

- TFB
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
Post Reply