Skip to content

Instantly share code, notes, and snippets.

@padawin
Last active January 19, 2018 21:27
Show Gist options
  • Save padawin/58f6b2dab7bf549be2be95e6f9f52549 to your computer and use it in GitHub Desktop.
Save padawin/58f6b2dab7bf549be2be95e6f9f52549 to your computer and use it in GitHub Desktop.
C++ - Lua

Examples of C++ code interacting with Lua. the first one just executes a Lua script, the second one calls a function from the script the third one calls a function from the scripts which calls a function from the c++ code

Howto

Example 1

make ex1
./helloworld

Example 2

make ex2
./stats

Example 3

make ex3
./generator
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* the Lua interpreter */
lua_State* L;
class Map {
private:
char cells[16];
public:
Map() {
for (int i = 0; i < 16; ++i) {
cells[i] = ' ';
}
}
void setCell(int i, char val) {
if (i >= 16) {
return;
}
cells[i] = val;
}
void print() {
for (int i = 0; i < 16; ++i) {
if (i % 4 == 0) {
printf("\n");
}
printf("%c", cells[i]);
}
printf("\n");
}
};
void generateMap(Map *map, int size) {
/* the function name */
lua_getglobal(L, "generateMap");
/* the first argument */
lua_pushlightuserdata(L, (void*)map);
/* the second argument */
lua_pushinteger(L, size);
/* call the function with 1 arguments,
* return 0 result */
lua_call(L, 2, 0);
}
int map_setCell(lua_State *L) {
int n = lua_gettop(L);
if (n != 3) {
return 0;
}
Map *map = (Map*) lua_touserdata(L, 1);
int i = lua_tonumber(L, 2);
char val = lua_tonumber(L, 3);
map->setCell(i, val);
return 0;
}
int main(int argc, char *argv[]) {
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
const char *file = "generator.lua";
Map map = Map();
/* register our function */
lua_register(L, "map_setCell", map_setCell);
/* run the script */
int res = luaL_dofile(L, file);
if (res) {
printf("Script %s not executed: %d\n", file, res);
printf("cannot run configuration file: %s\n", lua_tostring(L, -1));
}
generateMap(&map, 16);
map.print();
/* cleanup Lua */
lua_close(L);
/* pause */
printf("Press enter to exit...\n");
getchar();
return 0;
}
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* the Lua interpreter */
lua_State* L;
void getRaceInfo(const char* race, int &health, int &intelligence, int &strength) {
/* the function name */
lua_getglobal(L, "getStats");
/* the first argument */
lua_pushstring(L, race);
/* call the function with 1 arguments,
* return 3 result */
lua_call(L, 1, 3);
/* get the result */
health = (int) lua_tointeger(L, -1);
intelligence = (int) lua_tointeger(L, -2);
strength = (int) lua_tointeger(L, -3);
lua_pop(L, 3);
}
int main(int argc, char *argv[]) {
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
const char *file = "stats.lua";
/* run the script */
int res = luaL_dofile(L, file);
if (res) {
printf("Script %s not executed: %d\n", file, res);
printf("cannot run configuration file: %s\n", lua_tostring(L, -1));
}
int health = 0,
strength = 0,
intelligence = 0;
getRaceInfo("elf", health, intelligence, strength);
printf(
"Elves are: %d health, %d intelligence, %d strength\n",
health, intelligence, strength
);
getRaceInfo("human", health, intelligence, strength);
printf(
"Humans are: %d health, %d intelligence, %d strength\n",
health, intelligence, strength
);
getRaceInfo("orc", health, intelligence, strength);
printf(
"Orcs are: %d health, %d intelligence, %d strength\n",
health, intelligence, strength
);
/* cleanup Lua */
lua_close(L);
/* pause */
printf("Press enter to exit...\n");
getchar();
return 0;
}
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* the Lua interpreter */
lua_State* L;
int main(int argc, char *argv[]) {
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
const char *file = "helloworld.lua";
/* run the script */
int res = luaL_dofile(L, file);
if (res) {
printf("Script %s not executed: %d\n", file, res);
printf("cannot run configuration file: %s\n", lua_tostring(L, -1));
}
/* cleanup Lua */
lua_close(L);
/* pause */
printf("Press enter to exit...\n");
getchar();
return 0;
}
-- The generateMap function will be called with a map and with a number of cells
-- to generate.
-- The script has access to the map_setCell function, which expects as
-- arguments:
-- - the map
-- - the cell index
-- - the cell value
--
-- This is an amazing procedural map generator...
function generateMap(map, n) -- n is the size of the map
math.randomseed(os.time())
for i=0,n-1 do
map_setCell(map, i, math.random(97, 122)) -- from a to z
end
end
-- Will just be executed...
print "Hello world"
.PHONY: ex1 ex2 ex3
INC=
LIB=
LINK=-llua -ldl
all: ex1 ex2 ex3
ex1:
g++ execute_script.cpp -o helloworld $(LINK) $(INC) $(LIB)
ex2:
g++ call_script_function.cpp -o stats $(LINK) $(INC) $(LIB)
ex3:
g++ call_cpp_function.cpp -o generator $(LINK) $(INC) $(LIB)
-- The getStats function is needed with a string as argument and must return
-- 3 integers
races = {}
-- {health, intelligence, strength}
races["elf"] = {30, 100, 50}
races["orc"] = {60, 40, 50}
races["human"] = {50, 70, 40}
function getStats(race)
return races[race][3], races[race][2], races[race][1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment