本文介绍了lua检查多个值是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢用Roblox制作游戏,我喜欢用lua编写代码.编码良好的游戏我发现自己在问一个值是否在很多情况下都等于另一个值,这可能会使代码行很长,并且可能会重复很多.

例如:

如果x.ClassName =='衬衫'或
x.ClassName =='附件'或
x.ClassName =='裤子'或
x.ClassName =='CharacterMesh'然后
-做某事
结束

这样做不仅很麻烦,而且只是一遍又一遍地问同样的事情,稍加修改.
我想知道lua中是否有办法询问一个值是否等于多个不同的单独值

例如:

如果x.ClassName ==('Shirt'或'Accessory'或'Pants'或'CharacterMesh ')然后
-做事
结束

I like to make games in Roblox and I code in lua. Well coding games I find myself asking if a value equals another value a lot and it can make really long lines of code and can be quite repetitive.

For example:

if x.ClassName == 'Shirt' or
x.ClassName == 'Accessory' or
x.ClassName == 'Pants' or
x.ClassName == 'CharacterMesh' then
-- do thing
end

doing that not only can be very messy but is also just asking the same thing over and over again slightly modified.
I was wondering if there was a way in lua to ask if a value equals multiple diferant separate values

For example:

if x.ClassName == ( 'Shirt' or 'Accessory' or 'Pants' or 'CharacterMesh' ) then
-- do thing
end

推荐答案

Lua中没有本地方法可以执行此操作,但是您可以实现一个辅助函数set来实现.

There's no native way to do this in Lua but you can implement a helper function set to achieve that.

function set(...)
   local ret = {}
   for _,k in ipairs({...}) do ret[k] = true end
   return ret
end

local classname = 'Shirt'
if set('Shirt', 'Accessory', 'Pants', 'CharacterMesh')[classname] then
   print('true')
end

这篇关于lua检查多个值是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:57