   Embedding Lua in a C++ Class?
  I'm using Lua in a cpp project of mine, and I'd really like to encapsulate the functionality inside a class to hide away the gritty details of starting Lua and registering functions.  Unfortunately it's not working as I'd hoped.I get this error:error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'int (struct lua_State *)' to 'int (__cdecl *)(struct lua_State *)'It seems like it's unhappy registering functions that belong to a class, although its registering them to an lua_State object that lives inside the same class as the functions do.  Any way around this?H File://Class: LuaScript//Description: Encapsulates Lua functionality, each instance of this class can read from a separate script file.class LuaScript {public://Function: LuaScript//Arguments: script_file - name of file to use as a script//Returns: None//Description: Initializes Lua VM and sets up to read from script fileLuaScript(string script_file);//Function: ExecuteNext//Arguments: None//Returns: None//Description: Executes the next line of the scriptvoid ExecuteNext();private://Misc. Methods://Function: RegisterFunctions//Arguments: None//Returns: None//Description: Handle registering all glue functionsvoid RegisterFunctions();//Glue Methods (prefixed with "l_") static int l_DIO_SetDirection(lua_State *L);//Variables:lua_State* LuaVM;FILE* scriptFile;};CPP File:#include "LuaScript.h"//Function: LuaScript//Arguments: script_file - name of file to use as a script//Returns: None//Description: Initializes Lua VM and sets up to read from script fileLuaScript::LuaScript(string script_file){//Initialize LuaLuaVM = lua_open();//Load Lua base librariesluaL_openlibs(LuaVM);//Open the script text filescriptFile = fopen(script_file.c_str(), "r");   RegisterFunctions();}//Function: ExecuteNext//Arguments: None//Returns: None//Description: Executes the next line of the scriptvoid LuaScript::ExecuteNext(){char command[40];if(!feof(scriptFile))fgets(command, 40, scriptFile);//Run a line of the scriptluaL_dostring(LuaVM, command);}//Function: RegisterFunctions//Arguments: None//Returns: None//Description: Handle registering all glue functionsvoid LuaScript::RegisterFunctions(){lua_register(LuaVM, "DIO_SetDirection", l_DIO_SetDirection);}//Lua Syntax:  DIO_SetDirection(int port, bool input)//Description: Sets direction for DIO port.  The 2nd argument should be//true to make the port an input, and false to make it an output.int LuaScript::l_DIO_SetDirection(lua_State *L){int port = lua_tonumber(L, 1);int input = lua_tonumber(L, 2);if(input)SeaIo_SetDirection(hDIODevice, port, Input);elseSeaIo_SetDirection(hDIODevice, port, Output);return 0;}[Edited by - Dovyman on May 25, 2006 10:52:15 AM]
  Try making the function you register static? I don't see the prototype in your header to see how its declared.   ;  That would make sense that it needs to be static, I should have thought of that one.  It seems to be working now :)Thanks!   ;  Maybe I was too hasty before..It does work to make the functions static, but then I cannot access member variables of the class, which defeats the purpose of putting them all in the class to begin with.   ;  The basic lua function bindings don't allow you to bind non static member functions. Normal member functions require than an instance of the class be known as well. You may want to look into one of the many lua binding libraries out there .   