如何在Roblox中调用其他脚本文件中的函数

发布于 2024-07-26 01:59:25 字数 103 浏览 4 评论 0原文

我在工作区中嵌入了一个包含函数的脚本文件。 我想从嵌入工作区子对象中的脚本文件调用这些函数。 我不想将这些函数复制并粘贴到多个脚本文件中。 我认为如果可能的话,面向对象的方法将是最好的。

I have a script file embedded in the Workspace that contains functions. I would like call these functions from script files embedded in child objects of the Workspace. I don't want to have to copy and paste these functions into multiple script files. I figured the object oriented approach would be best if its possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

驱逐舰岛风号 2024-08-02 01:59:25

_G 的替代方法是使用全局可用的表共享。 Shared 的使用方式与 _G 相同,但您必须在变量标识符之前指定“shared”,这与 _G 不同,在 _G 中,您可以只写变量名称而无需 _G (ROBLOX 中不再有)。 Shared 在以下上下文中使用:

shared["variablename"] = value

就像在全局堆栈中一样,_G。
共享的示例用法:

脚本 1

shared["rprint"] = function(array) for i,v in pairs(array) do print(i, v) end end

脚本 2

repeat wait() until shared["rprint"]
shared.rprint({"Hello, ", "How", " are", " you?"})

该脚本的输出将是“Hello, How are you?”

An alternative to _G is to use the also globally avaliable table, shared. Shared is used the same way _G is, but you must specify "shared" before the variable identifier, unlike _G, where you can merely write the name of the variable without the _G (not anymore in ROBLOX). Shared is used in the following context:

shared["variablename"] = value

Just like in the global stack, _G.
Example usage of shared:

Script 1

shared["rprint"] = function(array) for i,v in pairs(array) do print(i, v) end end

Script 2

repeat wait() until shared["rprint"]
shared.rprint({"Hello, ", "How", " are", " you?"})

The output of this script would be "Hello, How are you?"

烟若柳尘 2024-08-02 01:59:25

最简单的方法是使用 _G 或共享。

在一个脚本中,

_G.myFunction = function(Arguments)

-- blah

end

在另一个脚本中,您将使用此代码。

重复 wait() 直到 _G.myFunction ~= nil

_G.myFunction()

这也适用于共享全局表,而不是 _G。

The simplest way would be to use _G or shared.

In one script,

_G.myFunction = function(Arguments)

-- blah

end

In another script, you would use this code.

repeat wait() until _G.myFunction ~= nil

_G.myFunction()

This would also work with the global table shared, instead of _G.

你怎么这么可爱啊 2024-08-02 01:59:25

我知道之前已经说过了,但只需使用普通的_G或共享即可访问它。

脚本一

_G.myFunction = function()
     print("Hello, myFunction!")
end

脚本二

repeat wait() until _G.myFunction()
_G.myFunction()

输出

你好,我的函数!

I know it has been said before, but just use the normal _G or shared to access it.

Script one

_G.myFunction = function()
     print("Hello, myFunction!")
end

Script two

repeat wait() until _G.myFunction()
_G.myFunction()

Output

Hello, myFunction!

筱果果 2024-08-02 01:59:25

我会使用 BindableEvents 或 RemoteEvents。 我认为这是比使用 _G 更好的方法。 这将使您能够将所有内容保留在本地。 您可以使用 Bindableevents 和 RemoteEvents 来触发函数并在脚本之间来回发送所需的数据。 使用 BindableEvents 进行服务器/服务器通信,使用 RemoteEvents 进行服务器/客户端-客户端/服务器通信。

http://wiki.roblox.com/index.php?title=API :类/BindableEvent

I would use BindableEvents or RemoteEvents. I think this is a better approach than using _G. This will allow you to keep everything local. You can use Bindableevents and RemoteEvents to trigger functions and send as much data as you need back and forth between scripts. Use BindableEvents for server/server communication and RemoteEvents for server/client-client/server communications.

http://wiki.roblox.com/index.php?title=API:Class/BindableEvent

十级心震 2024-08-02 01:59:25

您可以将该函数设为全局。 在
一个脚本可以做到这一点:

_G.myFunction = function() print("Hello World") end 
  

在另一个脚本中执行以下操作:

重复 wait() 直到 myFunction myFunction() 
  

通过定义一个函数是_G,你必须
等待脚本执行
分配功能,然后你可以
即使没有也调用该函数
指定_G。

这是行不通的,因为由于 ROBLOX 更新,现在只要访问其中的项目就必须索引 _G。

正如我上面提到的,你也不能在 ROBLOX 中使用 dofile() 。

回答这个问题:您需要在一个脚本中创建一个函数到全局表 - _G 中,方法是添加 _G.MyFunction = function(parameters) 结尾。 在另一个脚本中,您需要在 _G 表中访问它 - _G.MyFunction()。

ROBLOX 脚本编写者出现的一个常见问题是您尝试在创建 _G 内的函数之前访问它。 解决这个问题的一个简单方法是添加一个等待时间,直到它被创建,正如 Camoy 的帖子所建议的:

repeat wait() until _G.MyFunction() 

You can make the function global. In
one script do this:

_G.myFunction = function() print("Hello World") end

In another script do this:

repeat wait() until myFunction myFunction()

By defining a function is _G you must
wait for the script to execute
assigning the function, then you can
call the function even without
specifying _G.

This won't work because, due to ROBLOX updates, you now have to index _G whenever you access items inside it.

You cannot use dofile() in ROBLOX, either, as I saw mentioned above.

In answer to the question: you need to create a function in one script into the global table - _G, by adding _G.MyFunction = function(parameters) end. In another script, you need to access it inside the _G table - _G.MyFunction().

A common problem that appears for ROBLOX scripters is that you try to access your function inside _G before it is created. A simple way to solve this is to add a wait until it is created, as suggested from Camoy's post:

repeat wait() until _G.MyFunction() 
半仙 2024-08-02 01:59:25

您可以将该函数设为全局。 在一个脚本中执行此操作:

_G.myFunction = function() print("Hello World") end

在另一个脚本中执行此操作:

repeat wait() until myFunction myFunction()

通过将函数定义为 _G,您必须等待脚本执行分配该函数,然后即使不指定 _G,也可以调用该函数。

You can make the function global. In one script do this:

_G.myFunction = function() print("Hello World") end

In another script do this:

repeat wait() until myFunction myFunction()

By defining a function is _G you must wait for the script to execute assigning the function, then you can call the function even without specifying _G.

高跟鞋的旋律 2024-08-02 01:59:25

您可以使用幸运地添加的模块脚本。 您可以将函数放在那里,然后在其他地方调用和使用它们!

You could use Module Scripts which were thankfully added. You could put the functions in there, then call and use them anywhere else!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文