如何将数据传递到选定的场景中
假设我们有一个带有多个按钮的主菜单,并且有一个文本文件,其中包含按钮 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你已经有一个自动加载(单例),我会将您想要提供给其他场景的信息放在那里,并让其他场景读取它。
正确,你不能。在加载新场景的那一刻,当前场景已经被卸载,因此它无法真正调用新场景的方法。
除非你控制这个过程。请参阅手动更改场景。
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.
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.