【问题标题】:How to check the value of a boolean (set by user) with a variable string?如何使用变量字符串检查布尔值(由用户设置)?
【发布时间】:2013-09-03 22:15:20
【问题描述】:

用户将布尔值设置为真或假。

确实如此(示例)

ElementNameone = true
ElementNametwo = false
ElementNamethree = true

等等

现在我有一个从文件加载的字符串。名为 name 的字符串可以具有 Nameone、Nametwo、Namethree 等值。一次是其中的任何一个。 现在我希望能够做到这一点

if Element .. name == true then
do something

除非我不知道如何正确执行此操作。

我已经尝试过了

if not not ("Element" .. name) then

但它不起作用。

谁能帮忙?

谢谢

【问题讨论】:

  • 请在您的问题中添加相关语言标签。

标签: string lua boolean


【解决方案1】:

试试这个:

if _G["Element" .. name] == true then
    -- do something
end

请注意,这仅在用户设置的变量(ElementNameone,.. 等)是全局变量时才有效。

【讨论】:

  • 完美运行!非常感谢你的帮助!非常感谢
【解决方案2】:

很可能你解决了错误的问题。

您说“用户”设置了这些变量。如何?最终用户通常不会直接与程序中的变量进行交互。

您能否使用表来代替,以ElementNameone 作为键,truefalse 作为关联值?如果是这样,那就更干净了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2019-06-14
    • 1970-01-01
    • 2017-04-16
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多