1. ホーム
  2. スクリプト・コラム
  3. ルア

C++でLuaの設定ファイルやレスポンス関数を呼び出す例

2022-01-06 14:45:42

Luaはスクリプト言語であり、コンパイルが不要で軽快に動作することが最大の特徴です。C言語のフレームワークを書いたら、Luaで対応する処理を変更することで機能を変更でき、再コンパイルする必要がありません。以下は、Luaのリソース・メソッドをC言語で呼び出すサンプル・プログラムです。
C++側です。

// Lua1.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include <stdio.h> 
 
extern "C" { // If you don't use extern you will get a link error and compile to a C++ file 
#include <lua.h> 
#include <lualib.h> 
#include <luxlib.h> 
} 
 
 
void load (lua_State *L,int *width,int *height){ 
  lua_getglobal(L,"width"); //get the value of the variable in Lua and put it on the stack 
  lua_getglobal(L,"height"); 
 
  if(!lua_isnumber(L,-2)) // top of the stack is -1, then decrement in turn 
    luaL_error(L,"`width' should be a number\n"); 
 
  if(!lua_isnumber(L,-1)) 
    luaL_error(L,"`height' should be a number\n"); 
  *width = (int)lua_tonumber(L,-2); //convert top-of-stack elements to numbers 
  *height = (int)lua_tonumber(L,-1); 
} 
 
int add(lua_State *L,int a, int b){  
  int sum=0; 
  lua_getglobal(L,"add"); // the function is also a variable, so a take 
  lua_pushnumber(L,a); //pass the variable in 
  lua_pushnumber(L,b);   
    // the second parameter is the number of parameters to call the function, the third is the number of return values, the fourth parameter is the error handling function, normal operation returns 0 
  if(lua_pcall(L,2,1,0) ! = 0){   
    luaL_error(L,"the lua load error! %s",lua_tostring(L,-1)); 
  } 
     // the return value of the function has been passed to the stack 
  if(!lua_isnumber(L,-1)){  
    luaL_error(L,"the func run error! %s",lua_tostring(L,-1)); 
  } 
 
  sum = (int)lua_tonumber(L,-1); //take the top element of the stack and convert it to a number, default is double 
   lua_pop(L,1) //Remove the return element 
  return sum; 
} 
 
int get_num(lua_State *L){ 
   
  lua_getglobal(L,"roll_num"); 
  if(lua_pcall(L,0,1,0) ! = 0){ 
    luaL_error(L,"the lua load error! %s",lua_tostring(L,-1)); 
  } 
 
  if(!lua_isnumber(L,-1)){ 
    luaL_error(L,"the func run error! %s",lua_tostring(L,-1)); 
  } 
   int n = (int)lua_tonumber(L,-1); 
  lua_pop(L,1); 
  return n; 
} 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
  lua_State *L = lua_open(); 
  luaL_openlibs(L); // methods added by the new version of the library 
  if(luaL_loadfile(L,"cof.lua") || lua_pcall(L,0,0,0)){ 
    luaL_error(L,"loadfile error! %s \n",lua_tostring(L,-1)); 
  } 
 
  int w=1,h=2; 
  int sum; 
  load(L,&w,&h); 
  printf("width is %d ,height is %d\n",w,h); 
  sum=add(L,w,h); 
  printf("the sum is: %d \n",sum); 
   
   
  bool flag = true; 
  char c; 
  while(flag){ 
    printf("Do you want to roll the number? \n"); 
    scanf("%c",&c); 
    if(c == 'y' || c=='Y'){ 
      printf("rolling the number.... \n the num is %d \n",get_num(L)); 
    } 
    else flag=false; 
    getchar(); 
  } 
 
  getchar(); 
  return 0; 
} 


Luaファイルです。

-- config test 
 
width = 200 
height = 500 
 
function add(a,b) 
  return a+b 
end 
 
math.randomseed(os.time()) 
function roll_num() 
  return math.random(6) 
end