使用NLua,“需求"如何实现?关键字作品 [英] Using NLua, how does the "require" keyword work

查看:121
本文介绍了使用NLua,“需求"如何实现?关键字作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些包含我的业务逻辑的Lua代码,我想使用NLua在.NET中运行.

I have a some Lua code that contains my business logic, which I would like to run inside .NET using NLua.

我的一些lua文件当前使用"require"关键字来引用其他文件中的函数.现在,如果我使用例如ZeroBrane Studio.但是,一旦使用"DoFile"将代码加载到NLua中,就会出现一些找不到模块"错误.

Some of my lua file currently use the "require" keyword in order to reference functions in other files. Now, this works fine if I run the code using e.g. ZeroBrane Studio. However, once I use "DoFile" to load the code into NLua, I get some "Module not found" errors.

我的问题是;在NLua中运行代码时,可以使用需要"的概念吗?如果没有,那么在文件上运行DoFile之前,我是否需要删除这些行(当然要确保以正确的顺序运行文件)?

My question is; can I use the concept of "require", when running the code inside NLua? If not, would I then need to remove these lines, before running DoFile on the files (and of course make sure to run the files in the correct order)?

更新:我想也许我只需要将Lua文件夹添加到package.path中即可.我通过执行DoString("package.path = '<my_path>' .. package.path");这是最佳实践"吗?

Update: I think perhaps I simply need to add my Lua file folder to the package.path. I solved it by doing a DoString("package.path = '<my_path>' .. package.path"); Is this "best practise"?

推荐答案

搜索开始的根目录位置是可执行文件运行的位置,而不是运行DoString的文件所在的位置.

The root directory location the search starts in is the location where your executable is running, not where the file you ran DoString on is located.

以下是使用heaps.lua作为示例文件的NLua所需位置的一些示例:

Here are some examples of locations NLua require will look, using heaps.lua as an example file:

没有文件'./heaps.lua'
没有文件'./heaps/init.lua'
没有文件'./lua/heaps.lua'

no file './heaps.lua'
no file './heaps/init.lua'
no file './lua/heaps.lua'

示例:

可执行路径C:\foo\bar.exe

脚本路径C:\baz\qux.lua

qux.lua需要heaps,NLua将在C:\foo\中搜索堆,而不会搜索C:\baz\.

qux.lua has a require for heaps, NLua will search in C:\foo\ for heaps and will not search C:\baz\.

这篇关于使用NLua,“需求"如何实现?关键字作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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