有没有更好的方法从lua中的相对路径要求文件 [英] Is there a better way to require file from relative path in lua

查看:20
本文介绍了有没有更好的方法从lua中的相对路径要求文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录结构如下:

|-- ball.lua
|-- entity.lua
|-- test
    `-- ball_test.lua

我在 test/ball_test.lua 中使用以下代码来要求父目录中的 ball.lua:

I'm using the following code in test/ball_test.lua to require ball.lua from the parent directory:

package.path = package.path .. ";../entity.lua"
require("entity")
package.path = package.path .. ";../ball.lua"
require("ball")

entity.lua 是 ball.lua 的一个依赖.所以我首先需要(实体"),否则我会收到一个模块entity.lua"未找到的错误.这似乎是一种黑客行为,有什么更好的方法可以做到这一点?

entity.lua is a dependency of ball.lua. So I require("entity") first otherwise I get a module 'entity.lua' not found error. This seems like a hack, what's a better way to do this?

推荐答案

package.path = package.path .. ";../?.lua"

两者都适用.

这篇关于有没有更好的方法从lua中的相对路径要求文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆