test-client-godot/scenes/global.gd

116 lines
3.9 KiB
GDScript3
Raw Normal View History

2018-08-06 15:43:56 +00:00
extends Node
var current_scene = null
var character = null
var character_camera = null
var character_creation_mesh = null
2018-08-06 15:43:56 +00:00
var character_creation_camera = null
2018-08-08 08:46:16 +00:00
var player_name = null
var player_color = null
var character_slot = null
2018-08-08 08:46:16 +00:00
# BG loader
var loader
var wait_frames
var time_max = 100 # msec
#var current_scene
var main_scene = null
2018-08-06 15:43:56 +00:00
func _ready():
var root = get_tree().get_root()
# current_scene = root.get_child(root.get_child_count() -1)
current_scene = root.get_node( "Main/login_scene" )
# character = get_tree().get_root().get_node( "Main/Game/Character" )
# character_camera = get_tree().get_root().get_node( "Main/Game/Character/Camera_rotation_helper/Camera" )
# character_creation_camera = get_tree().get_root().get_node( "Main/login_scene/character_creation_menu/v_box_container/h_box_container/center_container/character_creation_scene/camera" )
2018-08-08 08:46:16 +00:00
main_scene = root.get_node( "Main" )
func goto_scene_loading( path ):
get_tree().get_root().get_node("Main/background_loader").show()
loader = ResourceLoader.load_interactive( path )
if loader == null: # check for errors
show_error()
return
set_process(true)
2018-08-06 15:43:56 +00:00
2018-08-08 08:46:16 +00:00
current_scene.queue_free() # get rid of the old scene
# start your "loading..." animation
# get_node("animation").play("loading")
wait_frames = 1
func _process(time):
if loader == null:
# no need to process anymore
set_process(false)
return
if wait_frames > 0: # wait for frames to let the "loading" animation to show up
wait_frames -= 1
return
var t = OS.get_ticks_msec()
while OS.get_ticks_msec() < t + time_max: # use "time_max" to control how much time we block this thread
# poll your loader
var err = loader.poll()
if err == ERR_FILE_EOF: # load finished
var resource = loader.get_resource()
loader = null
set_new_scene( resource )
break
elif err == OK:
update_progress()
else: # error during loading
show_error()
loader = null
break
func update_progress():
print("test: "+str(loader.get_stage())+" / "+str(loader.get_stage_count()) )
var progress = int( (float(loader.get_stage()) / loader.get_stage_count()) *100 )
# update your progress bar?
get_tree().get_root().get_node("Main/background_loader/center_container/progress_bar").value = progress
# or update a progress animation?
# var len = get_node("animation").get_current_animation_length()
# call this on a paused animation. use "true" as the second parameter to force the animation to update
# get_node("animation").seek(progress * len, true)
func set_new_scene( scene_resource ):
get_tree().get_root().get_node("Main/background_loader").hide()
current_scene = scene_resource.instance()
main_scene.add_child(current_scene)
func goto_scene( path ):
2018-08-06 15:43:56 +00:00
# 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:
2018-08-08 08:46:16 +00:00
call_deferred("_deferred_goto_scene", path)
2018-08-06 15:43:56 +00:00
2018-08-08 08:46:16 +00:00
func _deferred_goto_scene( path ):
2018-08-06 15:43:56 +00:00
# Immediately free the current scene,
# there is no risk here.
current_scene.free()
# Load new scene.
var s = ResourceLoader.load( path )
2018-08-06 15:43:56 +00:00
# Instance the new scene.
current_scene = s.instance()
# Add it to the active scene, as child of root.
2018-08-08 08:46:16 +00:00
path.add_child( current_scene )
2018-08-06 15:43:56 +00:00
# Optional, to make it compatible with the SceneTree.change_scene() API.
get_tree().set_current_scene( current_scene )