[英]Lua - How to use functions from another script
I'm wondering how to use functions from another script in Lua. 我想知道如何使用Lua中另一个脚本的函数。 For example, say
GameObjectUtilities
holds functions that many GameObject
scripts will use. 例如,假设
GameObjectUtilities
包含许多GameObject
脚本将使用的函数。 The Slime
(a GameObject
) script wants to use a function in GameObjectUtilities
. Slime
(一个GameObject
)脚本想要使用GameObjectUtilities
一个函数。
I'm having trouble getting this to work. 我无法解决这个问题。 I've looked here , but I still don't really fully understand.
我看过这里 ,但我还是没有完全理解。 Do I need to create a module or a table to hold the functions in
GameObjectUtilities
for the functions in it to be used in other scripts? 我是否需要创建一个模块或表来保存
GameObjectUtilities
中的函数,以便其中的函数用于其他脚本? If so, what is the best way to go about this? 如果是这样,最好的方法是什么?
It's very odd. 这很奇怪。 It actually does work when I just do it the normal way.
当我以正常方式执行时,它确实有效。 The problem is that when I run my app and it tries to use the script it doesn't work.
问题是,当我运行我的应用程序并尝试使用该脚本时,它不起作用。 I don't get it.
我不明白。
No, you don't have to create a module. 不,您不必创建模块。 If you just create
foo.lua
like this: 如果您只是像这样创建
foo.lua
:
function double(n)
return n * 2
end
And then in your script, require 'foo'
, you will be able to access the double
function just like it was defined in the same script. 然后在你的脚本中,
require 'foo'
,你就可以像在同一个脚本中定义的那样访问double
函数。 Those functions can't get at your locals, but they are created in the same environment -- all module 'name'
does is create a new table and reset the current environment to that table. 这些函数无法在您的本地获取,但它们是在同一环境中创建的 - 所有
module 'name'
都是创建一个新表并将当前环境重置为该表。
So, you can just do: 所以,你可以这样做:
function slimefunc(...) stuff() end
In GameObjectUtils.lua
, and if you require 'GameObjectUtils'
, then Slime
can just use slimefunc
. 在
GameObjectUtils.lua
,如果你require 'GameObjectUtils'
,那么Slime
只能使用slimefunc
。 Or, if you want it to be namespaced: 或者,如果您希望它被命名空间:
utils = {}
function utils.slimefunc(...) stuff() end
And it will be accessible as utils.slimefunc
. 它可以作为
utils.slimefunc
访问。 (If you do that, you'll have to be really careful about not letting your names leak - make judicious use of locals.) (如果你这样做,你必须非常小心不要让你的名字泄露 - 明智地使用当地人。)
You haven't given us enough information. 你没有给我们足够的信息。 For example, you don't say if
GameObjectUtilities
is defined or what its value is. 例如,您没有说明是否定义了
GameObjectUtilities
或它的值是什么。 (I'm guessing it is set to true
.) (我猜它设置为
true
。)
I highly recommend that you buy the second edition of Roberto Ierusalimschy's superb book Programming in Lua , which explains the idiomatic use of require
and module
very simply and clearly. 我强烈建议您购买第二版 Roberto Ierusalimschy的精湛书籍Lua中的编程 ,它非常简单明了地解释了
require
和module
的惯用用法。 It is also an excellent book for anyone using Lua to help get the most out of the language and libraries. 对于使用Lua来帮助充分利用语言和库的人来说,这本书也是一本很好的书。 As luck would have it, there is a free sample chapter which at the moment covers exactly the topic you're looking for.
幸运的是,有一个免费的样本章节 ,目前涵盖了您正在寻找的主题。 But buy the book anyway;
但无论如何要买这本书; it is $25 well spent :-)
这是25美元的花费:-)
If you don't want to buy the book, you can read the free sample chapter , and you can also read about how to do things the "old" way, without module(...)
, because the entire previous edition is free online. 如果你不想买这本书,你可以阅读免费的样本章节 ,你也可以阅读有关如何以“旧”的方式做事,没有
module(...)
,因为整个以前的版本是免费的线上。
One possible short answer is that your "utilities" script should probably create a table and return it . 一个可能的简短回答是你的“实用程序”脚本应该创建一个表并返回它 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.