Here I'll show my template for LUA scripts. It is simple class where I show you how to define functions in LUA. This class doesnt have only 1 example: function Text() wich calls function D.text().
Lua.h
Code:
#pragma once
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
class LUA
{
void define_funcs(); //here uou can define your function used in LUA
protected:
lua_State* L; //main LUA object
public:
void init(); //initialization
void del(); //deinitialization
void Run(const char* path); //run script from path
void onInit(); //function calls function INIT() in script
void onUpdate(); //function calls function UPDATE() in script
void onDraw(); //function calls function DRAW() in script
void onShut(); //function calls function SHUT() in script
};
Lua.cpp
Code:
#include "stdafx.h"
#include "main.h"
int DText(lua_State *L); //function DText wich calls D.text
void LUA::define_funcs()
{
lua_register(L, "Text", DText); //Registers function DText() as Text() in script
}
void LUA::init()
{
/* initialization Lua */
L = lua_open();
/* loading main libraries Lua */
luaL_openlibs(L);
// registration functions in LUA
define_funcs();
}
void LUA::del()
{
/* closing LUA */
lua_close(L);
}
//////
void LUA::Run(const char* script)
{
luaL_dofile(L, script);
}
///////
void LUA::onInit()
{
lua_getglobal(L, "Init");
lua_call(L, 0, 0);
}
void LUA::onUpdate()
{
lua_getglobal(L, "Update");
lua_call(L, 0, 0);
}
void LUA::onDraw()
{
lua_getglobal(L, "Draw");
lua_call(L, 0, 0);
}
void LUA::onShut()
{
lua_getglobal(L, "Shut");
lua_call(L, 0, 0);
}
/////Funcs from LUA/////
int DText(lua_State *L)
{
Flt pos_x = lua_tonumber(L, 1);
Flt pos_y = lua_tonumber(L, 2);
CChar8* text = lua_tostring(L, 3);
D.text(pos_x, pos_y, text);
return 0;
}
in main.h you must add #include "lua.h"
next in VS2008 in menu Project click Properties, in left window select Configuration Properties -> Linker -> Input and in Additional dependencies add Lua5.1.lib (libs and dlls in attatchament)
Use example:
define LUA class instantion:
in init():
Code:
Lua.init();
Lua.Run("sample.lua");
in draw(), we dont use update and shut becouse this example shows only D.text():
Code:
Lua.onDraw(); //if you use update in your script, in update() should be Lua.onUpdate()
WARNING: DLL files from attatchament must be on user(player) computer in game folder with .exe file or in C:\Windows\system32
It was example for LUA without luabind or boost, now I will try add luabind