@@ -35,10 +35,22 @@ func load_game_state(_load_flag:=LoadFlags.FULL_LOAD) -> void:
35
35
var character_info : Dictionary = portraits_info [character_path ]
36
36
var character : DialogicCharacter = load (character_path )
37
37
var container := dialogic .PortraitContainers .load_position_container (character .get_character_name ())
38
- add_character (character , container , character_info .portrait , character_info .position_id )
39
- change_character_mirror (character , character_info .get ('custom_mirror' , false ))
40
- change_character_z_index (character , character_info .get ('z_index' , 0 ))
41
- change_character_extradata (character , character_info .get ('extra_data' , "" ))
38
+
39
+ ResourceLoader .load_threaded_request (character_path )
40
+
41
+ var load_status = ResourceLoader .load_threaded_get_status (character_path )
42
+ while load_status == ResourceLoader .THREAD_LOAD_IN_PROGRESS :
43
+ await get_tree ().process_frame
44
+ load_status = ResourceLoader .load_threaded_get_status (character_path )
45
+
46
+ if load_status == ResourceLoader .THREAD_LOAD_LOADED :
47
+ character = ResourceLoader .load_threaded_get (character_path )
48
+ add_character (character , container , character_info .portrait , character_info .position_id )
49
+ change_character_mirror (character , character_info .get ('custom_mirror' , false ))
50
+ change_character_z_index (character , character_info .get ('z_index' , 0 ))
51
+ change_character_extradata (character , character_info .get ('extra_data' , "" ))
52
+ else :
53
+ push_error ('[Dialogic] Failed to load character "' + str (character_path ) + '".' )
42
54
43
55
# Load Speaker Portrait
44
56
var speaker : Variant = dialogic .current_state_info .get ('speaker' , "" )
@@ -117,19 +129,29 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
117
129
if (not previous_portrait == null and
118
130
previous_portrait .get_meta ('scene' , '' ) == scene_path and
119
131
# Also check if the scene supports changing to the given portrait.
132
+ previous_portrait .has_method ('_should_do_portrait_update' ) and
120
133
previous_portrait ._should_do_portrait_update (character , portrait )):
121
134
portrait_node = previous_portrait
122
135
info ['same_scene' ] = true
123
136
124
137
else :
125
138
126
139
if ResourceLoader .exists (scene_path ):
127
- var packed_scene : PackedScene = load (scene_path )
140
+ ResourceLoader .load_threaded_request (scene_path )
141
+
142
+ var load_status = ResourceLoader .load_threaded_get_status (scene_path )
143
+ while load_status == ResourceLoader .THREAD_LOAD_IN_PROGRESS :
144
+ await get_tree ().process_frame
145
+ load_status = ResourceLoader .load_threaded_get_status (scene_path )
128
146
129
- if packed_scene :
130
- portrait_node = packed_scene .instantiate ()
147
+ if load_status == ResourceLoader .THREAD_LOAD_LOADED :
148
+ var packed_scene : PackedScene = ResourceLoader .load_threaded_get (scene_path )
149
+ if packed_scene :
150
+ portrait_node = packed_scene .instantiate ()
151
+ else :
152
+ push_error ('[Dialogic] Portrait node "' + str (scene_path ) + '" for character [' + character .display_name + '] could not be loaded. Your portrait might not show up on the screen. Confirm the path is correct.' )
131
153
else :
132
- push_error ('[Dialogic] Portrait node "' + str (scene_path ) + '" for character [' + character .display_name + '] could not be loaded. Your portrait might not show up on the screen. Confirm the path is correct .' )
154
+ push_error ('[Dialogic] Failed to load portrait node "' + str (scene_path ) + '" for character [' + character .display_name + '].' )
133
155
134
156
if ! portrait_node :
135
157
portrait_node = default_portrait_scene .instantiate ()
@@ -156,7 +178,8 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
156
178
if not fade_animation .is_empty () and fade_length > 0 :
157
179
var fade_out := _animate_node (previous_portrait , fade_animation , fade_length , 1 , true )
158
180
var _fade_in := _animate_node (portrait_node , fade_animation , fade_length , 1 , false )
159
- fade_out .finished .connect (previous_portrait .queue_free )
181
+ await fade_out .finished
182
+ previous_portrait .queue_free ()
160
183
else :
161
184
previous_portrait .queue_free ()
162
185
@@ -167,20 +190,26 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
167
190
## Unless @force is false, this will take into consideration the character mirror,
168
191
## portrait mirror and portrait position mirror settings.
169
192
func _change_portrait_mirror (character_node : Node2D , mirrored := false , force := false ) -> void :
170
- var latest_portrait := character_node .get_child (- 1 )
193
+ var latest_portrait := character_node .get_child (- 1 ) if character_node . get_child_count () > 0 else null
171
194
172
- if latest_portrait .has_method (' _set_mirror' ):
195
+ if latest_portrait and latest_portrait .has_method (" _set_mirror" ):
173
196
var character : DialogicCharacter = character_node .get_meta ('character' )
174
197
var current_portrait_info := character .get_portrait_info (character_node .get_meta ('portrait' ))
175
198
latest_portrait ._set_mirror (force or (mirrored != character .mirror != character_node .get_parent ().mirrored != current_portrait_info .get ('mirror' , false )))
176
199
177
200
178
201
func _change_portrait_extradata (character_node : Node2D , extra_data := "" ) -> void :
179
- var latest_portrait := character_node .get_child (- 1 )
202
+ if not is_instance_valid (character_node ):
203
+ push_error ("[Dialogic] Invalid character node provided." )
204
+ return
180
205
181
- if latest_portrait . has_method ( '_set_extra_data' ) :
182
- latest_portrait . _set_extra_data ( extra_data )
206
+ if character_node . get_child_count () > 0 :
207
+ var latest_portrait := character_node . get_child ( - 1 )
183
208
209
+ if latest_portrait and latest_portrait .has_method ("_set_extra_data" ):
210
+ latest_portrait ._set_extra_data (extra_data )
211
+ else :
212
+ push_warning ("[Dialogic] No portrait found for character node: " + character_node .name )
184
213
185
214
func _update_character_transform (character_node :Node , time := 0.0 ) -> void :
186
215
for child in character_node .get_children ():
@@ -379,7 +408,7 @@ func join_character(character:DialogicCharacter, portrait:String, position_id:S
379
408
return
380
409
381
410
var container := dialogic .PortraitContainers .add_container (character .get_character_name ())
382
- var character_node := add_character (character , container , portrait , position_id )
411
+ var character_node := await add_character (character , container , portrait , position_id )
383
412
if character_node == null :
384
413
return null
385
414
@@ -410,9 +439,9 @@ func join_character(character:DialogicCharacter, portrait:String, position_id:S
410
439
return character_node
411
440
412
441
413
- func add_character (character :DialogicCharacter , container : DialogicNode_PortraitContainer , portrait :String , position_id :String ) -> Node :
442
+ func add_character (character : DialogicCharacter , container : DialogicNode_PortraitContainer , portrait : String , position_id : String ) -> Node :
414
443
if is_character_joined (character ):
415
- printerr ('[DialogicError] Cannot add a already joined character. If this is intended call _create_character_node manually.' )
444
+ printerr ('[DialogicError] Cannot add an already joined character. If this is intended, call _create_character_node manually.' )
416
445
return null
417
446
418
447
portrait = get_valid_portrait (character , portrait )
@@ -423,21 +452,31 @@ func add_character(character:DialogicCharacter, container: DialogicNode_Portrait
423
452
if not character :
424
453
printerr ('[DialogicError] Cannot call add_portrait() with null character.' )
425
454
return null
455
+
456
+ ResourceLoader .load_threaded_request (character .resource_path )
457
+
458
+ var load_status = ResourceLoader .load_threaded_get_status (character .resource_path )
459
+ while load_status == ResourceLoader .THREAD_LOAD_IN_PROGRESS :
460
+ await get_tree ().process_frame
461
+ load_status = ResourceLoader .load_threaded_get_status (character .resource_path )
426
462
427
- var character_node := _create_character_node (character , container )
428
-
429
- if character_node == null :
430
- printerr ('[Dialogic] Failed to join character to position ' , position_id , ". Could not find position container." )
431
- return null
432
-
463
+ if load_status == ResourceLoader .THREAD_LOAD_LOADED :
464
+ character = ResourceLoader .load_threaded_get (character .resource_path )
465
+ var character_node := _create_character_node (character , container )
433
466
434
- dialogic .current_state_info ['portraits' ][character .resource_path ] = {'portrait' :portrait , 'node' :character_node , 'position_id' :position_id }
467
+ if character_node == null :
468
+ printerr ('[Dialogic] Failed to join character to position ' , position_id , ". Could not find position container." )
469
+ return null
435
470
436
- _move_character (character_node , position_id )
437
- _change_portrait (character_node , portrait )
471
+ dialogic .current_state_info ['portraits' ][character .resource_path ] = {'portrait' : portrait , 'node' : character_node , 'position_id' : position_id }
438
472
439
- return character_node
473
+ _move_character (character_node , position_id )
474
+ await _change_portrait (character_node , portrait )
440
475
476
+ return character_node
477
+ else :
478
+ push_error ('[Dialogic] Failed to load character "' + str (character .resource_path ) + '".' )
479
+ return null
441
480
442
481
## Changes the portrait of a character. Only works with joined characters.
443
482
func change_character_portrait (character : DialogicCharacter , portrait : String , fade_animation := "DEFAULT" , fade_length := - 1.0 ) -> void :
@@ -455,7 +494,7 @@ func change_character_portrait(character: DialogicCharacter, portrait: String, f
455
494
456
495
fade_animation = DialogicPortraitAnimationUtil .guess_animation (fade_animation , DialogicPortraitAnimationUtil .AnimationType .CROSSFADE )
457
496
458
- var info := _change_portrait (dialogic .current_state_info .portraits [character .resource_path ].node , portrait , fade_animation , fade_length )
497
+ var info := await _change_portrait (dialogic .current_state_info .portraits [character .resource_path ].node , portrait , fade_animation , fade_length )
459
498
dialogic .current_state_info .portraits [character .resource_path ].portrait = info .portrait
460
499
_change_portrait_mirror (
461
500
dialogic .current_state_info .portraits [character .resource_path ].node ,
@@ -629,7 +668,8 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
629
668
630
669
if leave_animation and leave_animation_length :
631
670
var animate_out := _animate_node (character_node , leave_animation , leave_animation_length , 1 , true )
632
- animate_out .finished .connect (character_node .queue_free )
671
+ await animate_out .finished
672
+ character_node .queue_free ()
633
673
else :
634
674
character_node .get_parent ().remove_child (character_node )
635
675
character_node .queue_free ()
@@ -640,7 +680,19 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
640
680
continue
641
681
642
682
if just_joined :
643
- _create_character_node (speaker , container )
683
+ ResourceLoader .load_threaded_request (speaker .resource_path )
684
+
685
+ var load_status = ResourceLoader .load_threaded_get_status (speaker .resource_path )
686
+ while load_status == ResourceLoader .THREAD_LOAD_IN_PROGRESS :
687
+ await get_tree ().process_frame
688
+ load_status = ResourceLoader .load_threaded_get_status (speaker .resource_path )
689
+
690
+ if load_status == ResourceLoader .THREAD_LOAD_LOADED :
691
+ speaker = ResourceLoader .load_threaded_get (speaker .resource_path )
692
+ _create_character_node (speaker , container )
693
+ else :
694
+ push_error ('[Dialogic] Failed to load speaker "' + str (speaker .resource_path ) + '".' )
695
+ continue
644
696
645
697
elif portrait .is_empty ():
646
698
continue
@@ -655,10 +707,10 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
655
707
656
708
fade_animation = DialogicPortraitAnimationUtil .guess_animation (fade_animation , DialogicPortraitAnimationUtil .AnimationType .CROSSFADE )
657
709
658
- if container .portrait_prefix + portrait in speaker .portraits :
659
- portrait = container .portrait_prefix + portrait
710
+ if container .portrait_prefix + portrait in speaker .portraits :
711
+ portrait = container .portrait_prefix + portrait
660
712
661
- _change_portrait (character_node , portrait , fade_animation , fade_length )
713
+ await _change_portrait (character_node , portrait , fade_animation , fade_length )
662
714
663
715
# if the character has no portraits _change_portrait won't actually add a child node
664
716
if character_node .get_child_count () == 0 :
@@ -677,7 +729,7 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
677
729
var join_animation_length := _get_join_default_length ()
678
730
679
731
if join_animation and join_animation_length :
680
- _animate_node (character_node , join_animation , join_animation_length )
732
+ await _animate_node (character_node , join_animation , join_animation_length ). finished
681
733
682
734
_change_portrait_mirror (character_node )
683
735
0 commit comments