【问题标题】:How to make a function to read values from the lua file如何制作一个从lua文件中读取值的函数
【发布时间】:2016-01-02 20:54:03
【问题描述】:

好吧,我正在学习lua,我有一个问题,我正在尝试在lua中创建一个阅读函数bool。

我有一个功能会在我得分为真或假时禁用或启用。

这个函数被称为useappenabled,在月球上我无法应用它,之前我以libconfig的形式使用并正常运行之前写成如下。

文件如下:

Enableapp = 
{
    Useapp = true;
};

现在在塑造 libconfig 之前阅读如下,请注意 useappenabled 函数应用于输入值,即如果我放在 Useapp 上,则为 true 或 false。

if (config_lookup(&onf, "Enableapp"))
        if (config_setting_lookup_bool(cf, "Useapp", &SelectValue))
            useappenabled = SelectValue;

所以我尝试将代码libconfig更改为lua,但是我无法读取useappenabled函数,代码如下在lua中。

lua_getglobal(L, "Enableapp");
    lua_pushstring(L, "Useapp");
    lua_tonumber(L, useappenabled);

我认为问题出在 lua_tonumber 上,我需要或多或少地做这样的事情:

useappenabled = value_the_Useapp;

但是我现在开始 lua,谁能告诉我如何应用函数 useappenabled 来等于 Useapp 值。

【问题讨论】:

    标签: lua


    【解决方案1】:

    无法在月球上应用它

    这是大多数地球软件的问题。通常的困难是到达月球。

    我需要做一些或多或少这样的事情:

    useappenabled = value_the_Useapp;
    
    lua_getglobal(L, "Enableapp"); // push the table onto the stack
    lua_getfield(L, -1, "Useapp"); // index table with "Useapp" and push result onto the stack
    useappenabled = lua_toboolean(L, -1); // get the value off the top of the stack into your C code
    

    【讨论】:

    • 非常感谢 Mud,完美运行,实际上我不知道可以这样做。一个问题是我可以看看如何在 C 中应用 lua 的任何材料?
    • 另一个问题,例如,如何计算代码中现有 lua_getglobal 的数量。 printf ("there's 32 configuration. \n");
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多