如何将数据传递到选定的场景中

发布于 2025-01-16 06:37:37 字数 571 浏览 1 评论 0原文

假设我们有一个带有多个按钮的主菜单,并且有一个文本文件,其中包含按钮 1、按钮 2 等后面的数据......文本数据被加载到字典数组中。

我们正在加载这样的场景:

# Global singleton provides a data set, i.e.
var mydata = [{...}, { ... }, etc...]

# On the main menu scene we have buttons
func _on_Button1_pressed():
    get_tree().change_scene("res://DataList.tscn")
    newscene???.send_data(mydata[0]) # ????

如何将与 Button1 关联的预加载字典对象传递到即将出现的场景中?

我知道 change_scene() 是“延迟”的,所以我们不能立即为这个场景调用“setter”函数,或者可以吗?我仍在学习 godot,所以我有点迷失,不知道如何做到这一点。

请注意,我期望在用户按下按钮时“构建”和“重建”新场景,并在用户退出时“拆卸”现场。

Assume we have a main menu with multiple buttons, and we have a text file which contains data behind button 1, button 2, etc.... The text data is loaded into an array of dictionaries.

We are loading a scene like this:

# Global singleton provides a data set, i.e.
var mydata = [{...}, { ... }, etc...]

# On the main menu scene we have buttons
func _on_Button1_pressed():
    get_tree().change_scene("res://DataList.tscn")
    newscene???.send_data(mydata[0]) # ????

How do I pass the preloaded dictionary object associated with Button1 into the scene that is about to appear?

I understand that change_scene() is "deferred" so we cant just straight away call a "setter" function for this scene, or can we? I am still learning godot so I am a bit lost as to know how to do this

Note that I am expecting to "build up" and "rebuild" the new scene when the user presses the button, and "teardown" when a user exits the scene.

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

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

发布评论

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

评论(1

挖鼻大婶 2025-01-23 06:37:37

# 全局单例提供一个数据集,即

如果你已经有一个自动加载(单例),我会将您想要提供给其他场景的信息放在那里,并让其他场景读取它。


据我了解,change_scene() 是“延迟”的,因此我们不能立即为此场景调用“setter”函数,或者可以吗?

正确,你不能。在加载新场景的那一刻,当前场景已经被卸载,因此它无法真正调用新场景的方法。

除非你控制这个过程。请参阅手动更改场景

# Global singleton provides a data set, i.e.

If you already have an autoload (singleton), I would put there the information you want to give the other scene, and have the other scene read it.


I understand that change_scene() is "deferred" so we cant just straight away call a "setter" function for this scene, or can we?

Correct, you can't. For the instant where the new scene is loaded the current one is already unloaded, so it can't really call a method on the new one.

Unless you take control of the process. See Change scenes manually.

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