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

时间:2022-08-24 15:01:09

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.

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

8 个解决方案

#1


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:

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

shared["variablename"] = value

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

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

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?"

这个脚本的输出是“你好,你好吗?”

#2


I found what I was looking for in this tutorial on Exposing public functions written by the notorious jediknightkrazy.

我在本教程中找到了我在寻找暴露由臭名昭着的jediknightkrazy编写的公共函数的内容。

#3


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.

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

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

这不起作用,因为由于ROBLOX更新,您现在必须在访问其中的项目时索引_G。

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

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

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().

在回答这个问题时:你需要在一个脚本中创建一个函数到全局表中 - _G,通过添加_G.MyFunction = function(参数)结束。在另一个脚本中,您需要在_G表中访问它 - _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:

ROBLOX脚本程序出现的一个常见问题是,您在创建之前尝试访问_G中的函数。解决此问题的一种简单方法是在Camoy的帖子中建议添加等待直到创建它:

repeat wait() until _G.MyFunction() 

Hope this helps! -pighead10

希望这可以帮助! -pighead10

#4


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

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

Script one

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

Script two

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

Output

Hello, myFunction!

#5


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.

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

#6


The simplest way would be to use _G or shared.

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

In one script,

在一个脚本中,

_G.myFunction = function(Arguments)

_G.myFunction = function(Arguments)

-- blah

end

In another script, you would use this code.

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

repeat wait() until _G.myFunction ~= nil

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

_G.myFunction()

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

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

#7


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.

我会使用BindableEvents或RemoteEvents。我认为这比使用_G更好。这将允许您保持本地的一切。您可以使用Bindableevents和RemoteEvents来触发函数,并在脚本之间来回发送所需的数据。使用BindableEvents进行服务器/服务器通信,使用R​​emoteEvents进行服务器/客户端 - 客户端/服务器通信。

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

#8


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

您可以使用感谢添加的模块脚本。您可以将功能放在那里,然后在其他任何地方调用并使用它们!

#1


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:

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

shared["variablename"] = value

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

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

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?"

这个脚本的输出是“你好,你好吗?”

#2


I found what I was looking for in this tutorial on Exposing public functions written by the notorious jediknightkrazy.

我在本教程中找到了我在寻找暴露由臭名昭着的jediknightkrazy编写的公共函数的内容。

#3


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.

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

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

这不起作用,因为由于ROBLOX更新,您现在必须在访问其中的项目时索引_G。

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

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

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().

在回答这个问题时:你需要在一个脚本中创建一个函数到全局表中 - _G,通过添加_G.MyFunction = function(参数)结束。在另一个脚本中,您需要在_G表中访问它 - _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:

ROBLOX脚本程序出现的一个常见问题是,您在创建之前尝试访问_G中的函数。解决此问题的一种简单方法是在Camoy的帖子中建议添加等待直到创建它:

repeat wait() until _G.MyFunction() 

Hope this helps! -pighead10

希望这可以帮助! -pighead10

#4


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

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

Script one

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

Script two

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

Output

Hello, myFunction!

#5


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.

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

#6


The simplest way would be to use _G or shared.

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

In one script,

在一个脚本中,

_G.myFunction = function(Arguments)

_G.myFunction = function(Arguments)

-- blah

end

In another script, you would use this code.

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

repeat wait() until _G.myFunction ~= nil

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

_G.myFunction()

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

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

#7


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.

我会使用BindableEvents或RemoteEvents。我认为这比使用_G更好。这将允许您保持本地的一切。您可以使用Bindableevents和RemoteEvents来触发函数,并在脚本之间来回发送所需的数据。使用BindableEvents进行服务器/服务器通信,使用R​​emoteEvents进行服务器/客户端 - 客户端/服务器通信。

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

#8


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

您可以使用感谢添加的模块脚本。您可以将功能放在那里,然后在其他任何地方调用并使用它们!