1

3D シーンで、Lua を使用して移動したい位置を持つオブジェクトがあります。

例えば。box.position.x = 10

ボックスにはメタテーブル ("Object") があり、位置 ("Vec") もあります。オブジェクトには__newindex__indexC 関数を呼び出すための と がそれぞれ設定されてNewIndexObjectIndexObjectます。Vec (NewIndexVecおよびIndexVec) と同じです。

オブジェクトには ID があるため、シーンに保存されているリストで識別できbox.positionます。アクセスされたときにすべて問題なく、C 関数 IndexObject が呼び出され、スタックから ID を抽出できます。box.position.x = 10「NewIndexVec」が実行されたときです。が呼び出され、スタック上の唯一のものは {table, x, 10} であるため、オブジェクトを識別して x 位置を変更する方法はありません。

値をローカル状態にプッシュする方法はありますか? ヘルプ!

更新:コードを可能な限り要約した以下で、すぐに返信していただきありがとうございます。このコードを実行すると動作するように見えますが、行き詰まっているところにコメントがあります。配列の最初のオブジェクトを取得しているだけですが、ID で選択する必要があります。よろしくお願いします

struct Obj
{
    std::string id;
    int x,y,z;
    Obj()
    {
        x = 10; y = 20; z = 30;
        id = "12345";
    }
};

//array of external objects
std::vector<Obj> objects;

int NewObject(lua_State * L)
{
    Obj obj;
    objects.push_back(obj);

    lua_newtable(L); 

    luaL_getmetatable(L, "MT_Object");
    lua_setmetatable(L, -2);

    lua_pushstring(L, "id");
    lua_pushstring(L, obj.id.c_str());
    lua_settable(L, 1);

    lua_newtable(L); 
    luaL_getmetatable(L, "MT_Vec");
    lua_setmetatable(L, -2);

    lua_pushinteger(L, obj.x);
    lua_setfield(L, -2, "x"); 

    lua_pushinteger(L, obj.y);
    lua_setfield(L, -2, "y"); 

    lua_pushinteger(L, obj.z);
    lua_setfield(L, -2, "z"); 

    lua_setfield(L, -2, "position");



    return 1;
}

int IndexVec(lua_State * L)
{
    // How do I get the correct object so I can pass its value back
    Obj &dunnoObj =  objects[0];

    std::string key = luaL_checkstring(L,-1);
    if(key == "x")
        lua_pushinteger(L,dunnoObj.x);
    else if(key == "y")
        lua_pushinteger(L,dunnoObj.y);
    else if(key == "z")
        lua_pushinteger(L,dunnoObj.z);
    return 1;
}


int NewIndexVec(lua_State * L)
{
    // How do I know which object's value to update
    Obj &dunnoObj =  objects[0];

    std::string key = luaL_checkstring(L,-2);
    int value = luaL_checkinteger(L,-1);

    if(key == "x")
        dunnoObj.x = value;
    else if(key == "y")
        dunnoObj.y = value;
    else if(key == "z")
        dunnoObj.z = value;

    return 0;
}

int main()
{
    lua_State * L = luaL_newstate();
    luaL_openlibs(L);


    luaL_Reg objreg[] =
    {
        { "new", NewObject },   
        { NULL, NULL }
    };
    luaL_newmetatable(L, "MT_Object");
    luaL_register(L, 0, objreg);
    lua_setglobal(L, "Object");


    luaL_Reg reg[] =
    {
        { "__index", IndexVec },    
        { "__newindex", NewIndexVec },
        { NULL, NULL }
    };
    luaL_newmetatable(L, "MT_Vec");
    luaL_register(L, 0, reg);
    lua_setglobal(L, "Vec");


    int res = luaL_dostring(L, "box = Object.new()   box.position.x = 1000   print(box.id .. \" , \" ..box.position.x .. \" , \" ..  box.position.y .. \" , \" .. box.position.z)");
    if(res)
        printf("Error: %s\n", lua_tostring(L, -1));



    lua_close(L);

    return 0;
}
4

2 に答える 2

0

私があなたのことを正しく理解していれば、あなたは何もする必要はありません。テーブルは参照によって追跡されるため、最初の引数が であるかNewIndexVecどうかについて何も知る必要はありません。boxbox.position

この回答が何らかの理由で機能しない場合は、問題を理解するためにデータ構造に関する詳細情報が必要です。

基本的に、有効な操作であるbox.positionいくつかを返す必要があり、変更したいものを正確に変更します。objobj.x = 10


問題は、同じデータを 2 つの別々の場所に保持しようとしていることです。すべてのデータを C++ 構造体に保持しNewObject、テーブルのふりをするユーザーデータを返します。オブジェクトとpositionフィールドは同じObj*である必要がありますが、異なるフィールドのセットをシミュレートするために異なるメタテーブルを持つ場合があります。

于 2014-06-23T04:37:08.333 に答える
0

ありがとう、私は動作するコードを投稿しました

struct Obj
{
    unsigned int id;
    int x,y,z;
    Obj()
    {
        x = 10; y = 20; z = 30;
        id = rand();
    }
};

//array of external objects
std::map<unsigned int,Obj> objects;

int NewObject(lua_State * L)
{
    Obj obj;
    objects[obj.id] = obj;

    lua_pushinteger(L, obj.id);
    luaL_getmetatable(L, "MT_Object");
    lua_setmetatable(L, -2);

    return 1;
}


int IndexObj(lua_State * L)
{
    unsigned int objid = luaL_checkinteger(L,1);

    std::string key = luaL_checkstring(L,-1);

    if(key == "position" )
    {
        Obj *a = (Obj *)lua_newuserdata(L, sizeof(Obj));
        *a = objects[objid];

        luaL_getmetatable(L, "MT_Vec");
        lua_setmetatable(L, -2);
    }
    else if(key == "id")
        lua_pushinteger(L, objid);  
    else
        lua_pushnil(L);

    StackDump(L);
    return 1;
}

int IndexVec(lua_State * L)
{
    Obj *a = (Obj *)lua_touserdata(L, 1);

    std::string key = luaL_checkstring(L,-1);
    if(key == "x")
        lua_pushinteger(L,a->x);
    else if(key == "y")
        lua_pushinteger(L,a->y);
    else if(key == "z")
        lua_pushinteger(L,a->z);
    return 1;
}


int NewIndexVec(lua_State * L)
{
    Obj *a = (Obj *)lua_touserdata(L, 1);
    Obj &objRef =  objects[a->id];

    std::string key = luaL_checkstring(L,-2);
    int value = luaL_checkinteger(L,-1);

    if(key == "x")
        objRef.x = value;
    else if(key == "y")
        objRef.y = value;
    else if(key == "z")
        objRef.z = value;

    return 0;
}

int main()
{
    lua_State * L = luaL_newstate();
    luaL_openlibs(L);


    luaL_Reg objreg[] =
    {
        { "new", NewObject },   
        { "__index", IndexObj },
        { "__newindex", NewIndexObj },
        { NULL, NULL }
    };
    luaL_newmetatable(L, "MT_Object");
    luaL_register(L, 0, objreg);
    lua_setglobal(L, "Object");


    luaL_Reg reg[] =
    {
        { "__index", IndexVec },    
        { "__newindex", NewIndexVec },
        { NULL, NULL }
    };
    luaL_newmetatable(L, "MT_Vec");
    luaL_register(L, 0, reg);
    lua_setglobal(L, "Vec");

    int res = luaL_dostring(L,  "box = Object.new()   box.position.x = 1000 "
                                "print(box.id .. \" , \" ..box.position.x .. \" , \" ..  box.position.y .. \" , \" .. box.position.z)");
    if(res)
        printf("Error: %s\n", lua_tostring(L, -1));

    lua_close(L);

    return 0;
}
于 2014-06-24T00:47:09.310 に答える