Godot:如何仅在脚本中添加脚本?
我问我如何在一个文件中只有一个代码行添加脚本在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
;
如果您转到项目菜单 - &gt 项目设置… - >自动加载选项卡,在顶部,您会看到一个字段,上面写着“路径”,并带有一个带有文件夹图标的按钮。在那里,您可以选择一个文件以添加为自动加载。它可以是场景,也可以是脚本。选择文件后,您可以给它一个名称(Godot将根据文件的名称生成名称,您可以更改它),然后单击“添加”。
当您选择的文件是脚本时,Godot将为脚本扩展的类型创建
node
。您将能够使用自动加载的名称从任何场景的任何脚本中访问
节点
。因此,如果您将脚本添加到自动道路上,例如:
使用名称 - 例如 - “数据”,您应该能够从任何场景中附加到
node
的任何脚本中形成其形式。例如,这样的:添加代码中的自动加载,
如果您正在编写插件,则可能需要添加代码中的自动加载。为此,您可以使用
editorplugin
类的方法add_autoload_singleton
class(如果您正在编写插件,则有一个),例如:可以使用
remove_autoload_singleton
方法:这些等同于从编辑器UI添加和删除自动加载量。 我提醒您,编辑器插件在编辑器中运行。
顺便说一句,自动加载是
node
s配置为在场景树中添加的,但不是在当前场景中。我们也可以直接在运行时做到这一点,但是我们将缺乏在任何地方提供名称的便利。这就是我们的方式:如果您要添加一个场景,则可以做到这一点:
如果您要添加脚本,则可以做到这一点:
因为我们添加了这些
node
s toget_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:
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: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 theEditorPlugin
class (if you are writing an plugin, you have one of those), like this:And to remove it you can use the
remove_autoload_singleton
method: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
Node
s 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:If you are trying to add a scene, you can do this:
And if you are trying to add a script, you can do this:
Since we are adding these
Node
s toget_tree().root
and not toget_tree().current_scene
when Godot changescurrent_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 aNode
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: