Godot:如何仅在脚本中添加脚本?

发布于 2025-02-04 15:29:55 字数 68 浏览 3 评论 0原文

我问我如何在一个文件中只有一个代码行添加脚本在godot中自动加载 - >运行功能并获取从此文件到2。文件的VAR。

Im asking how i can add a script to autoload in godot with only lines of code in one file -> to run functions and get vars from this file to a 2. file.

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

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

发布评论

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

评论(1

那片花海 2025-02-11 15:29:55

;

如果您转到项目菜单 - &gt 项目设置… - >自动加载选项卡,在顶部,您会看到一个字段,上面写着“路径”,并带有一个带有文件夹图标的按钮。在那里,您可以选择一个文件以添加为自动加载。它可以是场景,也可以是脚本。选择文件后,您可以给它一个名称(Godot将根据文件的名称生成名称,您可以更改它),然后单击“添加”。

当您选择的文件是脚本时,Godot将为脚本扩展的类型创建node

您将能够使用自动加载的名称从任何场景的任何脚本中访问节点


因此,如果您将脚本添加到自动道路上,例如:

extends Node

var name:String

使用名称 - 例如 - “数据”,您应该能够从任何场景中附加到node的任何脚本中形成其形式。例如,这样的:

print(Data.name)

添加代码中的自动加载,

如果您正在编写插件,则可能需要添加代码中的自动加载。为此,您可以使用editorplugin类的方法add_autoload_singleton class(如果您正在编写插件,则有一个),例如

add_autoload_singleton(name, path)

:可以使用remove_autoload_singleton方法:

remove_autoload_singleton(name)

这些等同于从编辑器UI添加和删除自动加载量。 我提醒您,编辑器插件在编辑器中运行。


顺便说一句,自动加载是node s配置为在场景树中添加的,但不是在当前场景中。我们也可以直接在运行时做到这一点,但是我们将缺乏在任何地方提供名称的便利。这就是我们的方式:

get_tree().root.add_child(node)

如果您要添加一个场景,则可以做到这一点:

var packed_scene:PackedScene = load(path) # or preload
get_tree().root.add_child(packed_scene.instance())

如果您要添加脚本,则可以做到这一点:

var script:Script = load(path) # or preload
get_tree().root.add_child(script.new())

因为我们添加了这些node s to get_tree()。​​根,而不是get_tree()。​​current_scene当godot更改crenty> current_scene时,它们不会受到影响。

因此,我们需要在场景树中附加到节点访问自动加载的脚本的原因是因为它们自动加载是场景树中的node因此,我们需要访问场景树以访问自动加载。


替代方案

我们有两个自动加载的替代方案。它们并不完全相同,但是功能上有一些重叠。为了避免再解决这个答案,我将仅提及它们:

  • 带有静态方法的命名类
  • 自定义资源(搜索基于资源的通信)。

Adding a script only autoload form the editor

If you go to the Project menu -> Project Settings… -> Autoload tab, on the top you will see a field that says "path", and an button with the icon of a folder. There you can select a file to add as autoload. It can be a scene or it can be a script. Once you have chosen the file, you give it a name (Godot will generate a name based on the name of the file, you can change it) and then click "Add".

When the file you selected is a script, Godot will create a Node for it of the Type the script extends.

You will be able to access the Node from any script of any scene using the name given to the auto-load.


Thus, if you add an script to autolaod, for example:

extends Node

var name:String

With the name - for example - "Data", you should be able to acess it form from any script attached to a Node in any scene. For example, like this:

print(Data.name)

Adding an autoload from code

If you are writing a plugin, you might want to add an autoload from code. To do this you can use the method add_autoload_singleton of the EditorPlugin class (if you are writing an plugin, you have one of those), like this:

add_autoload_singleton(name, path)

And to remove it you can use the remove_autoload_singleton method:

remove_autoload_singleton(name)

These are equivalent to adding and removing the autoload from the editor UI. I remind you that editor plugins run in the editor.


By the way, the autoloads are Nodes configured to be added in the scene tree, but not under the current scene. We can do that in runtime directly too, however we would lack the convenience of having the name available everywhere. This is how we would do it:

get_tree().root.add_child(node)

If you are trying to add a scene, you can do this:

var packed_scene:PackedScene = load(path) # or preload
get_tree().root.add_child(packed_scene.instance())

And if you are trying to add a script, you can do this:

var script:Script = load(path) # or preload
get_tree().root.add_child(script.new())

Since we are adding these Nodes to get_tree().root and not to get_tree().current_scene when Godot changes current_scene they will not be affected.

Thus, the reason why we need a script attached to Node in the scene tree to access the autoload is because they autoload is a Node in the scene tree, and thus we need access to the scene tree to access the autoload.


Alternatives

We have two alternatives to autoloads. They are not exactly the same, but they have some overlap in functionality. To avoid making this answer any longer, I'll just mention them:

  • a named class with static method
  • custom resources (search for resource based communication).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文