【问题标题】:How to import folders with Lua如何使用 Lua 导入文件夹
【发布时间】:2020-04-08 16:48:48
【问题描述】:

我的一个脚本有问题,我试图在脚本运行时加载一个文件,但我收到一条错误消息,提示他没有找到任何具有我给定名称的文件。

这是我设置加载文件的条件:

function checkAll()
    if (global:getCountFight() >= (LAST_NB_FIGHT+MAX_FIGHT)) then
        LAST_NB_FIGHT = global:getCountfight() 
        printMsg("Déconnecte le personnage pendant "..SLEEP_TIME.." heure(s)")
        global:reconnect(SLEEP_TIME)
    elseif (character:level()<8) then
        goHomeAndLoadTrajet("[Combat] Bouftous")
        setMinMonsters(1)
        setMaxMonsters(8)
        setForceMonsters({}) 
        SLEEP_LVL = 50
        SLEEP_TIME = 6

这是函数 goHomeAndLoadTrajet 调用的内容:

function goHomeAndLoadTrajet(trajetName)
    trajetName = checkTrajetName(trajetName)
    if trajetName ~= u_NEXT_TRAJET and trajetName ~= LAST_TRAJET then
        u_NEXT_TRAJET = trajetName
        goHome()
    end
end

调用:

function checkTrajetName(trajetName)
    if (file_exists(INCLUDES_PATH..trajetName)) then
        return trajetName
    elseif (file_exists(INCLUDES_PATH..trajetName..".lua")) then
        return trajetName..".lua"
    else
        printMsg("Le trajet '"..trajetName.."' n'existe pas !", "ERREUR")
        return
    end

最终调用:

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

所以现在我很困惑..谢谢:)

编辑:INCLUDES_PATH 是一个包含我的路径的变量,
INCLUDES_PATH = "C:\\Program Files (x86)\\SnowbotTouch\\Scripts Lua\\Trajets\\includes\\"

【问题讨论】:

  • 错误信息是否来自checkTrajetName()
  • 是的! @EgorSkriptunoff
  • 直接调用checkTrajetName(trajetName)会发生什么?

标签: file import lua directory load


【解决方案1】:

您无需猜测可能出现的问题;您只需要从io.open 调用中输出错误消息:

function file_exists(name)
   local f, err =io.open(name,"r")
   if f~=nil then
     io.close(f)
   else
     print(err)
   end
   return f~=nil
end

错误消息将包含文件名,因此它应该明确地告诉您文件名、权限或其他方面是否存在问题。

【讨论】:

  • 谢谢!是权限问题!!非常感谢
猜你喜欢
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多