Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以保存完整的;路径“;到C函数中的Lua表字段?_C_Lua - Fatal编程技术网

是否可以保存完整的;路径“;到C函数中的Lua表字段?

是否可以保存完整的;路径“;到C函数中的Lua表字段?,c,lua,C,Lua,如果Lua中有这样一个全局对象: global_object = { } global_object.stat_group_1 = { } global_object.stat_group_2 = { } global_object.stat_group_3 = { } global_object.stat_group_1.stat_1 = 1 -- value changes with time global_object.stat_group_1.stat_2 = 2 -- value ch

如果Lua中有这样一个全局对象:

global_object = { }
global_object.stat_group_1 = { }
global_object.stat_group_2 = { }
global_object.stat_group_3 = { }
global_object.stat_group_1.stat_1 = 1 -- value changes with time
global_object.stat_group_1.stat_2 = 2 -- value changes with time
global_object.stat_group_1.stat_3 = 3 -- value changes with time
-- ... and same thing for other stat_groups
int global_object_ref;
int stat_group_1_ref;
int stat_1_ref;

//assume this function has been called before any of the get_* functions
int start ( lua_State * L )
{
    lua_getfield ( L, LUA_RIDX_GLOBALS, "global_object" );
    lua_pushvalue ( L, -1 );
    global_object_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_group_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    return 0;
}

//this is the prefered option. I would like this to be possible
int get_stat1_v1 ( lua_State * L )
{
    //stat_1 can have different values in the Lua table at different moments
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}

//this is an alternative, in case v1 doesn't work. Would this work?
//again, remember stat_1 can have different values at different moments.
int get_stat1_v2 ( lua_State * L )
{
    lua_rawgeti ( L, LUA_REGISTRYINDEX, global_object_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_group_1_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}
我的问题是关于
luaL\u ref
lua\u rawgeti
lua\u getfield
。我是否可以使用luaL_ref保存每个
stat
的路径,以避免显式调用堆栈上的所有路径,如下所示:

global_object = { }
global_object.stat_group_1 = { }
global_object.stat_group_2 = { }
global_object.stat_group_3 = { }
global_object.stat_group_1.stat_1 = 1 -- value changes with time
global_object.stat_group_1.stat_2 = 2 -- value changes with time
global_object.stat_group_1.stat_3 = 3 -- value changes with time
-- ... and same thing for other stat_groups
int global_object_ref;
int stat_group_1_ref;
int stat_1_ref;

//assume this function has been called before any of the get_* functions
int start ( lua_State * L )
{
    lua_getfield ( L, LUA_RIDX_GLOBALS, "global_object" );
    lua_pushvalue ( L, -1 );
    global_object_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_group_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    lua_getfield ( L, -1, "stat_1" );
    lua_pushvalue ( L, -1 );
    stat_group_1_ref = LuaL_ref ( L, LUA_REGISTRYINDEX );

    return 0;
}

//this is the prefered option. I would like this to be possible
int get_stat1_v1 ( lua_State * L )
{
    //stat_1 can have different values in the Lua table at different moments
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}

//this is an alternative, in case v1 doesn't work. Would this work?
//again, remember stat_1 can have different values at different moments.
int get_stat1_v2 ( lua_State * L )
{
    lua_rawgeti ( L, LUA_REGISTRYINDEX, global_object_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_group_1_ref );
    lua_rawgeti ( L, LUA_REGISTRYINDEX, stat_1_ref );

    //is this the value of the field stat_1?
    int value_of_stat_1 = lua_tointeger ( L, -1 );

    return 1;
}
请注意,
v2
将所有保存的引用调用到堆栈上。这样行吗

编辑:根据@Nicol Bolas的回答,我想提出一个
v3
。如果表和子表从未被垃圾收集,但它们的值不断更新(假设子表的整个结构是一棵树,每个子表是一个分支,每个基本值是一片叶子。树的结构在执行期间保持不变,但叶子会被更新)


你可以这样做,但你基本上会放弃任何形式的垃圾收集。注册表是Lua状态的一部分,因此只要这些表在注册表中,它们就必须存在。因此,在取消注册或关闭Lua状态之前,它们引用的任何对象都将存在


您并没有真正保存“路径”。您正在保存存储在这些位置的表中的实际值。因此,如果表的值发生更改,注册表中存储的内容将不会更新。保存
stat_1的值将是它当前的值,而不是它可能更改的值。

我明白了!非常感谢你的明确回答。您认为将路径“保存”到
stat\u group\u 1
并使用
lua\u getfield
调用字段
stat\u 1
是否有效?因为我不认为每次调用表的某个字段时都会重新创建该表。我想它只是更新了。如果将表想象成一棵树,我认为叶子(实际值)会得到更新,但分支(子表)在整个执行过程中保持不变。我必须测试一下。但是假设是这样的话,你认为Lua和C有可能共享一棵价值树吗?@FinnTheHuman:这完全取决于Lua脚本的功能。表是Lua中的值,因此可以用新表替换它们。但就像整数值一样,只有当你替换它们时,它们才会被替换。正如
global_object.stat_group_1.stat_1=1
将一个值替换为另一个值一样,
global_object.stat_group_1={}
也将该表项的当前值替换为不包含任何内容的新表。我明白了,这完全是你的Lua脚本在做什么和没有做什么的问题。非常感谢。