简体   繁体   English

Lua-如何在vars中使用常量?

[英]Lua - How to use constants with vars?

I don't really have a clue of lua so sry if this is dumb ^^' 我真的没有lua的线索,所以请问这是愚蠢的^^'

I have a constants(?) like this: 我有一个这样的常量(?):

Config.name

It content is "true" or "false". 它的内容是“ true”或“ false”。 I set this constant-name(or member name?) dynamicly, so it can be for example: Config.george, Config.steve or Config.tim. 我动态设置此常数名称(或成员名称?),因此可以是例如:Config.george,Config.steve或Config.tim。 Now I want to check this constant, but I don't know how the syntax is. 现在,我想检查此常量,但是我不知道语法如何。

I want something like this: 我想要这样的东西:

for _, friend in pairs(friends) do
    if Config.friend.name then
        print("He is checked!")
    end
end

The "friend.name" should be the name of that friend, george for example. “ friend.name”应该是那个朋友的名字,例如乔治。 How is that done? 怎么做?

It looks like you're checking whether a given name is set to true in the Config table. 您似乎正在检查Config表中给定名称是否设置为true。 Assuming friends is a table of names you want to check against, the code would be: 假设friends是您要检查的姓名表,则代码为:

local friends = { 'george', 'steve', 'tim', } 
-- ...
for _, friend in pairs(friends) do
  if Config[friend] then
    print(friend.." is checked!")
  end
end

Note that ipairs can also work here or just iterate by index: 请注意, ipairs也可以在这里工作或仅按索引进行迭代:

for i = 1, #friends do
  if Config[ friends[i] ] then
    print(friends[i] .. " is checked!")
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM