Jump to content
  • Advertisement
Sign in to follow this  
Silvo

Embedding Python/LUA

This topic is 4415 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
Advertisement
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();
luaL_openlibs(g_pState);
lua_register(g_pState, NameString, FunctionPointer);

luaL_dofile(g_pState, FileNameString);

system("pause");
lua_close(g_pState);
return 0;
}



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

HTH!

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[]) {
SquirrelVM::Init();
SquirrelObject helloWorld = SquirrelVM::CompileBuffer(_T("print(\"Hello World\");"));
SquirrelVM::RunScript(helloWorld);
SquirrelVM::Shutdown();
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):


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

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

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

class MyClass {
public:
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[]) {
SquirrelVM::Init();
// This example shows how to redirect print output to your own custom
// print function (the default handler prints to stdout).
sq_setprintfunc(SquirrelVM::GetVMPtr(),printFunc);

#ifdef USE_REMOTE_DEBUGGER
HSQREMOTEDBG rdbg = sq_rdbg_init(SquirrelVM::GetVMPtr(),1234,SQTrue);
if(rdbg) {
// Enable debug info generation (for the compiler).
sq_enabledebuginfo(SquirrelVM::GetVMPtr(),SQTrue);
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 {
printSQDBGError(SquirrelVM::GetVMPtr());
return 0;
} // if
#endif

// 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.
SQClassDef<MyClass>(_T("MyClass")).
func(&MyClass::process,_T("process")).
var(&MyClass::classVal,_T("classVal"));

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 {
SquirrelVM::RunScript(helloSqPlus);
} catch (SquirrelError & e) {
scprintf(_T("Error: %s, %s\n"),e.desc,_T("Squirrel::helloSqPlus"));
} // catch

#ifdef USE_REMOTE_DEBUGGER
if (rdbg) {
sq_rdbg_shutdown(rdbg);
} // if
#endif

SquirrelVM::Shutdown();
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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!