I've just finished my last project, and now I want to start on something new. I've started getting to grips with OpenGL and its going well at the moment, although I want to integrate lua into my program so that I can use lua for some aspects of the game, and so i've started trying to integrate lua into my program, however, if I do it the way it explains in books and tutorials (at least all the books/tutorials i've read), the program crashes.
If I start the lua state as shown below, the program crashes on the line 'luaopen_io (m_luaState);', and I've tried looking on the internet for solutions, and apparently the solution is that all the luaopen function calls shouldnt be used, they are internal functions designed to be called by lua and not the coder. Anyway, I can't think of or find an alternative function call which does the same thing. Does anyone here have any experience of integrating lua into an application? My code is below, although since I know where the error is, I don't know how useful it'll be, I'm including it more for the sake of completeness.
Code:
/* lua_module.h */
#ifndef _LUA_MODULE_H_
#define _LUA_MODULE_H_
#include <iostream>
#include <string.h>
/* Include the Lua modules */
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
class CGameLua
{
private:
lua_State *m_luaState;
public:
CGameLua()
{
m_luaState = NULL;
}
~CGameLua()
{
lua_close(m_luaState);
m_luaState = NULL;
}
/* Init / Deinit Methods */
bool InitLua();
void DeinitLua();
};
#endif
Code:
/* lua_module.cpp */
#include "lua_module.h"
bool CGameLua::InitLua()
{
m_luaState = lua_open ();
if( m_luaState != NULL )
{
// Load util libs into lua
luaopen_base (m_luaState);
luaopen_table (m_luaState);
luaopen_string (m_luaState);
luaopen_math (m_luaState);
luaopen_debug (m_luaState);
// luaopen_io (m_luaState); <-------------- Problem is this line
//luaopen_loadlib (m_luaState);
return false;
}
return true;
}
void CGameLua::DeinitLua()
{
if( m_luaState != NULL )
{
lua_close(m_luaState);
m_luaState = NULL;
}
}