Lua and c++

Started by
6 comments, last by Kercyn 7 years, 3 months ago

hi all, how can i use Lua (for scripts) in my c++ game?

Advertisement

Lua API docs: https://www.lua.org/pil/24.html

An example: http://lua-users.org/wiki/SimpleLuaApiExample

Binding generators: http://lua-users.org/wiki/BindingCodeToLua

If you have any more specific questions after reading those links, feel free to ask. :)

Sean Middleditch – Game Systems Engineer – Join my team!

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
Did you remember to link lua.lib?

i downloaded compiled lua threw this: http://luadist.org/ but there are no libs

Ah yes, that’s right— there are no official prepackaged versions of Lua. You are free to include the entirety of Lua into your project, or you can use a pre-built package here: http://luabinaries.sourceforge.net/

found it downloaded from other source : https://code.google.com/archive/p/luaforwindows/downloads (thx :) )

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.

This topic is closed to new replies.

Advertisement