简体   繁体   中英

Lua Set Functions in ComputerCraft

I have a ComputerCraft program set to turn on a siren when any non-whitelisted players are near:

sensor = peripheral.wrap("top")
function arraysubset(a, b)
   local s = set(b)
   for _, el in pairs(a)
      if not s[el] then
         return false
      end
   end
   return true
end
function sirenOn() rs.setBundledOutput("back",colors.blue) end
function sirenOff() rs.setBundledOutput("back",0) end
while 1 do
  playersNear = sensor.getPlayerNames()
  allowedPlayers = {"VirtualDXS","jettrom","Shad0wlurker16","Demonicmobster","FireFang0113","riggs135","DaisySnow123","MasterAlex930"}
  if playersNear[1] ~= nil then
    if arraysubset(playersNear,allowedPlayers) then sirenOff() else sirenOn() end
  else sirenOff() end
end

However, on line 3 I get an attempt to call nil. This makes me think that the set() function is not present on computercraft. I'm wondering:

  1. Is there another (maybe better) way to find if array a is a subset of array b and
  2. If not where can I get an API with the set() function?

Rereading the source for the subset() code, I am seeing that the code I used required more code from earlier in the answer:

function set(list)
   local t = {}
   for _, item in pairs(list) do
       t[item] = true
   end
   return t
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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