Last active
August 29, 2016 02:31
-
-
Save voidproc/971a83bc0bfc4817243e861bc96beaac to your computer and use it in GitHub Desktop.
Lua 5.2.4 + LuaBridge minimal example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Siv3D : http://play-siv3d.hateblo.jp/ | |
#include <Siv3D.hpp> | |
// Lua : https://www.lua.org/ | |
#include <lua.hpp> | |
#pragma comment(lib, "lua52.lib") | |
// LuaBridge : https://github.com/vinniefalco/LuaBridge | |
#include <LuaBridge.h> | |
using namespace luabridge; | |
class A | |
{ | |
public: | |
A(const int val) : val_(val) | |
{ | |
} | |
int f() | |
{ | |
Println(L"f() : val=", val_); | |
return 123 + val_; | |
} | |
private: | |
int val_; | |
}; | |
void Main() | |
{ | |
// Initialize Lua | |
lua_State* L = luaL_newstate(); | |
luaL_openlibs(L); | |
luaL_dostring(L, | |
"function luafunc(e)\n" | |
" return e:f()\n" | |
"end\n" | |
); | |
// Register C++ classes using LuaBridge | |
getGlobalNamespace(L) | |
.beginNamespace("foo") | |
.beginClass<A>("A") | |
.addFunction("f", &A::f) | |
.endClass() | |
.endNamespace(); | |
// Call Lua function | |
LuaRef luafunc = getGlobal(L, "luafunc"); | |
A a(90000); | |
int ret = luafunc(&a); | |
Println(ret); | |
WaitKey(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment