ページ 1 / 1
LuajitでC++の構造体に代入したい
Posted: 2016年4月14日(木) 23:47
by tanaka
Lunajitに構造体を読み込ませることまではできるのですが、Luajitのほうで代入操作(enemy[1].xxx = 10)をしてもC++の構造体の値が変化しません。どうにか直接 構造体の値を変更したいのですが・・・
調べても日本語の情報が少なくて理解が進まず全然わからなくて困っています。
下記はLuajitのコードになります。
コード:
ffi = require("ffi")
ffi.cdef[[
typedef struct ENEMY_SHOT {
double x[8000];
double y[8000];
double range;
}foo_t[800];
int dofoo( foo_t *f, int n);
]]
function plus()
enemy = ffi.new("foo_t")
enemy.range = 10
end
Re: LuajitでC++の構造体に代入したい
Posted: 2016年4月22日(金) 07:56
by naohiro19
Luaには「スタック」という概念を用いて実装されます。
以下のプログラムはLuaのスタック状態を表示するものです(Win32コンソールアプリケーション限定)。
コード:
#include <lua.hpp>
#include <cstdio>
static void PrintStackItem(lua_State* L, int index){
switch(lua_type(L, index)){
case LUA_TNUMBER:
//数値アイテムの内容表示
printf("index %2d: type = %s : %f\n", index,
lua_typename(L, index), lua_tonumber(L, index));
break;
case LUA_TBOOLEAN:
//ブール値アイテムの内容表示
printf("index %2d: type = %s : \"%s\"\n",
index, lua_typename(L, index),
lua_toboolean(L, index) ? "true" : "false");
break;
case LUA_TSTRING:
//文字列アイテムの内容表示
printf("index %2d: type = %s : \"%s\"\n",
index, lua_typename(L, index),
lua_tostring(L, index));
break;
default:
// その他ならば型だけ表示
printf("index %2d: type = %s\n", index, lua_typename(L, index));
break;
}
}
void PrintStack(lua_State* L) {
printf("----- stack -----\n");
int top = lua_gettop(L);
// 正のインデックスで指定
for(int i = top; i >= 1; i--){
PrintStackItem(L, i);
}
printf("----------------\n");
// 負のインデックスで指定
for(int i = -1; i >= -top; i--){
PrintStackItem(L, i);
}
printf("----------------\n");
}