昨年のリリース以降遊ぶ時間が無かったのですが、少し遊んでみました。
まず手始めに知りたかったのは、シーンの切替方法。
一番簡単な方法としては、Autoloadチュートリアル(wikiだとtutorial=singletons)に記載されている方法。
- プロジェクト設定にある、Autoloadを使用して、事前にスクリプトを読み込んでおく。
- スクリプト内に関数を用意しておく。
- シーンを切り替える際は、スクリプトを呼び出すことで
extends Node var current_scene = null func _ready(): var root = get_tree().get_root() current_scene = root.get_child( root.get_child_count() -1 ) func goto_scene(path): # This function will usually be called from a signal callback, # or some other function from the running scene. # Deleting the current scene at this point might be # a bad idea, because it may be inside of a callback or function of it. # The worst case will be a crash or unexpected behavior. # The way around this is deferring the load to a later time, when # it is ensured that no code from the current scene is running: call_deferred("_deferred_goto_scene",path) func _deferred_goto_scene(path): # Immediately free the current scene, # there is no risk here. current_scene.free() # Load new scene var s = ResourceLoader.load(path) # Instance the new scene current_scene = s.instance() # Add it to the active scene, as child of root get_tree().get_root().add_child(current_scene)
–
任意のスクリプトから切り替えたいシーンファイルを呼び出し。
#add to scene_a.gd func _on_goto_scene_pressed(): get_node("/root/global").goto_scene("res://scene_b.scn")
この方法だと、リソースの読込が完了するまで固まってしまいます。
■
「読み込み中」といった表示を行う場合には
https://github.com/okamstudio/godot/wiki/Background%20loading
を参考にすると良さそう。