Thread: Integrating lua into C++ application

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Integrating lua into C++ application

    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;
         }
    }

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It appears as if you are using an older version of Lua so you might want to start there. You can check out their site for tons of resources, add-ins, and other cool stuff in Lua. You can also purchase the 2nd edition of the Lua reference on Amazon.

    I have integrated Lua into my code base as well as other code bases. I'm not sure why you are attempting to call a function that the manual says do not call.


    EDIT:
    I have attached a class that handles setting Lua up. It by no means will give you everything you will need to fully integrate Lua into a system but it should get you started.
    Last edited by VirtualAce; 03-12-2011 at 11:41 AM.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Thank you Bubba, a very helpful post, and thanks very much for the source code, its helping me alot. I didn't realise I was using a old version of lua, but you'll be glad to hear that that is no more, lol. And to answer your question, I'm calling a function which the manual said not to call because the book I'm reading is a little dated, and I didn't realise :P Needless to say, from now on, I'll treat the book's source code with a pinch of salt. lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need to edit text from within my application.
    By spiroth10 in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2006, 03:15 PM
  2. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM
  4. Win application not very portable
    By swed in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2001, 11:17 AM