Advertisement Jump to content
Sign in to follow this  

Embedding Python/LUA

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

OK, ive looked everywhere, or at least, in most places, on a way to embed python in c++. After trying this, i tried LUA, no such luck. All I want to do is enable my C++ application to execute python/LUA scripts. So, I write a function or similar in python/LUA, store that in a seperate file, and in my C++ program, call that file/function, and it runs it with the python/LUA interpreter. This seems to be somethig that many people could benefit from, do you know a way I can do this, or a source that will tell me?

Share this post

Link to post
Share on other sites
Did you try reading the manual?

Both Python and Lua come with manuals, that document how to do exactly that.

There are several libraries that provide additional features on top of the bindings provided by the libraries, too; SWIG is one; boost::python is another (specific to Python); don't know if there's a boost::lua yet.

Embedding LUA in a C++ program is literally a matter of a few lines of code; it's very simple. The manual has good examples.

Share this post

Link to post
Share on other sites
Hmm, decided to go with LUA (had a bit of experience with making a WoW AddOn). After looking through their manual, I can't find any decent examples on how to embed LUA with C++ in it. It has loads of info on syntax, but no info on embedding it.

Share this post

Link to post
Share on other sites
If you're using a newer version of LUA then some of the names have changed and thus old tutorials are kind of rendered moot.

Here's an example I whipped up using the newest version:

lua_State* g_pState = 0;

int main()
g_pState = lua_open();
lua_register(g_pState, NameString, FunctionPointer);

luaL_dofile(g_pState, FileNameString);

return 0;

Everything else is the say as in old the old tutorials (at least as far as I have seen.)


Share this post

Link to post
Share on other sites
If you are open to another lua-like scripting language with (closer to) C++ style syntax, and have Visual Studio 2005 (VS8), you can have Squirrel (scripting language) with SqPlus (binding system) compiled and running in about 5 minutes (gcc is also supported with basic makefiles).

Hello World:

int main(int argc,char * argv[]) {
SquirrelObject helloWorld = SquirrelVM::CompileBuffer(_T("print(\"Hello World\");"));
return 0;
} // main

Binding a class and interacting between Cpp and script, optional remote debugger usage (forum reformated the listing; see the .zip source file for original formatting):

void printSQDBGError(HSQUIRRELVM v) {
const SQChar *err;
if(SQ_SUCCEEDED(sq_getstring(v,-1,&err))) {
scprintf(_T("SQDBG error : %s"),err);
}else {
scprintf(_T("SQDBG error"),err);
} // if
} // printSQDBGError

static void printFunc(HSQUIRRELVM v,const SQChar * s,...) {
static SQChar temp[2048];
va_list vl;
scvsprintf( temp,s,vl);
} // printFunc

// This class will be instantiated and used in script.

class MyClass {
int classVal;
// See examples in testSqPlus2.cpp for passing arguments to the constructor (including variable arguments).
MyClass() : classVal(123) {}
bool process(int iVal,const SQChar * sVal) {
scprintf(_T("classVal: %d, iVal: %d, sVal %s\n"),classVal,iVal,sVal);
classVal += iVal;
return iVal > 1;
} // process

int main(int argc,char * argv[]) {
// This example shows how to redirect print output to your own custom
// print function (the default handler prints to stdout).

HSQREMOTEDBG rdbg = sq_rdbg_init(SquirrelVM::GetVMPtr(),1234,SQTrue);
if(rdbg) {
// Enable debug info generation (for the compiler).
scprintf(_T("Waiting for SQDBG connection..."));
// Suspends the app until the debugger client connects.
if(SQ_SUCCEEDED(sq_rdbg_waitforconnections(rdbg))) {
printf("SQDBG: connected.\n");
} // if
} else {
return 0;
} // if

// See examples in testSqPlus2.cpp for script read-only vars, constants,
// enums, static/global functions, variable arguments, constructor arguments,
// passing/returning classes/structs by value or by address, etc.

SquirrelObject helloSqPlus = SquirrelVM::CompileBuffer(_T(" local myClass = MyClass(); \n local rVal = myClass.process(1,\"MyClass1\"); \n print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n rVal = myClass.process(2,\"MyClass2\"); \n print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n print(\"classVal: \"+myClass.classVal); \n "));

try {
} catch (SquirrelError & e) {
scprintf(_T("Error: %s, %s\n"),e.desc,_T("Squirrel::helloSqPlus"));
} // catch

if (rdbg) {
} // if

return 0;
} // main

The remote debugger is portable and uses the Eclipse environment (supports syntax highlighting, etc.).

You may also want to check out Angel Script (a statically-typed scripting language (lua and Squirrel are dynamically typed)).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!