Jump to content
  • Advertisement
Sign in to follow this  
alextoti99

Lua and c++

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

Advertisement

for this code:

#pragma once
#include <iostream>

#include "lua/lua.hpp" //needed for Lua

/************************************************************************/
/*  LuaEngine class
/*
/*  implements a wrapper around the lua state object required
/*  to run user defined lua scripts. It is a non-copyable class
/*  as the application should only include one instance of the engine
/*  and execute scripts on that engine's state
/************************************************************************/
class LuaEngine
{
public:
    //Constructor
    LuaEngine(void) : m_L(luaL_newstate()) { luaL_openlibs(m_L); }
    LuaEngine(const LuaEngine& other);  //non-construction copy
    LuaEngine& operator=(const LuaEngine&); //non-copy
    ~LuaEngine(void) { lua_close(m_L); } //Destructor clean-up

                                         //returns lua state object
    lua_State* L();

    //Execute Lua File
    void ExecuteFile(const char* file);

    //Execute Lua Expression (contained in string)
    void ExecuteString(const char* expression);

private:
    //member lua state
    lua_State* m_L;

private:

    //handle errors
    void report_errors(int state);
};


#include "LuaEngine.h"

//returns lua state object
lua_State* LuaEngine::L()
{
    return m_L;
}

//helper function to report errors in evaluated lua scripts
void LuaEngine::report_errors(int state)
{
    if (state != 0)
    {
        std::cerr << "ERR: " << lua_tostring(m_L, state) << std::endl;
        lua_pop(m_L, 1); //remove error
    }
}

//executes a lua script file
void LuaEngine::ExecuteFile(const char* file)
{
    if (file == NULL)
        return;

    int state = luaL_dofile(m_L, file);
    report_errors(state);
}

//execute a lua expression contained in string
//
//(could contain a full script with line breaks)
void LuaEngine::ExecuteString(const char* expression)
{
    if (expression == NULL)
    {
        std::cerr << "ERR: null expression passed to script engine!" << std::endl;
        return;
    }

    int state = luaL_dostring(m_L, expression);
    report_errors(state);
}


#include "LuaEngine.h"

//macro definition for *safe* deletion of pointer object
#define SAFE_DELETE(x) { if(x) delete x; x = nullptr; }

//entry point for application
int main()
{
    //create lua engine
    LuaEngine* engine = new LuaEngine();

    //execute specified file file
    engine->ExecuteFile("test.lua");

    SAFE_DELETE(engine); //delete engine from memory

    system("PAUSE"); //<iostream>: function waits for user input
    return 0;
}


im getting error: LNK1120
LNK2019

Severity    Code    Description    Project    File    Line    Suppression State
Error    LNK1120    8 unresolved externals    LuaScripts    C:\Users\alex_\Desktop\LuaScripts\Debug\LuaScripts.exe    1    

Severity    Code    Description    Project    File    Line    Suppression State
Error    LNK2019    unresolved external symbol _luaL_loadfile referenced in function "public: void __thiscall LuaEngine::ExecuteFile(char const *)" (?ExecuteFile@LuaEngine@@QAEXPBD@Z)    LuaScripts    C:\Users\alex_\Desktop\LuaScripts\LuaScripts\LuaEngine.obj    1    

Share this post


Link to post
Share on other sites

You may want to take a look at Selene, it's a small and, from my understanding, very well made library that does the dirty job for you.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!