Skip to content

Commit 7a24c14

Browse files
authored
Merge pull request #2522 from RightSorcerer/multithreading_loading
Added a multithreading loading
2 parents b56e693 + aba4c07 commit 7a24c14

File tree

1 file changed

+86
-34
lines changed

1 file changed

+86
-34
lines changed

addons/dialogic/Modules/Character/subsystem_portraits.gd

+86-34
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ func load_game_state(_load_flag:=LoadFlags.FULL_LOAD) -> void:
3535
var character_info: Dictionary = portraits_info[character_path]
3636
var character: DialogicCharacter = load(character_path)
3737
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) + '".')
4254

4355
# Load Speaker Portrait
4456
var speaker: Variant = dialogic.current_state_info.get('speaker', "")
@@ -117,19 +129,29 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
117129
if (not previous_portrait == null and
118130
previous_portrait.get_meta('scene', '') == scene_path and
119131
# Also check if the scene supports changing to the given portrait.
132+
previous_portrait.has_method('_should_do_portrait_update') and
120133
previous_portrait._should_do_portrait_update(character, portrait)):
121134
portrait_node = previous_portrait
122135
info['same_scene'] = true
123136

124137
else:
125138

126139
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)
128146

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.')
131153
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 + '].')
133155

134156
if !portrait_node:
135157
portrait_node = default_portrait_scene.instantiate()
@@ -156,7 +178,8 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
156178
if not fade_animation.is_empty() and fade_length > 0:
157179
var fade_out := _animate_node(previous_portrait, fade_animation, fade_length, 1, true)
158180
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()
160183
else:
161184
previous_portrait.queue_free()
162185

@@ -167,20 +190,26 @@ func _change_portrait(character_node: Node2D, portrait: String, fade_animation:=
167190
## Unless @force is false, this will take into consideration the character mirror,
168191
## portrait mirror and portrait position mirror settings.
169192
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
171194

172-
if latest_portrait.has_method('_set_mirror'):
195+
if latest_portrait and latest_portrait.has_method("_set_mirror"):
173196
var character: DialogicCharacter = character_node.get_meta('character')
174197
var current_portrait_info := character.get_portrait_info(character_node.get_meta('portrait'))
175198
latest_portrait._set_mirror(force or (mirrored != character.mirror != character_node.get_parent().mirrored != current_portrait_info.get('mirror', false)))
176199

177200

178201
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
180205

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)
183208

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)
184213

185214
func _update_character_transform(character_node:Node, time := 0.0) -> void:
186215
for child in character_node.get_children():
@@ -379,7 +408,7 @@ func join_character(character:DialogicCharacter, portrait:String, position_id:S
379408
return
380409

381410
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)
383412
if character_node == null:
384413
return null
385414

@@ -410,9 +439,9 @@ func join_character(character:DialogicCharacter, portrait:String, position_id:S
410439
return character_node
411440

412441

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:
414443
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.')
416445
return null
417446

418447
portrait = get_valid_portrait(character, portrait)
@@ -423,21 +452,31 @@ func add_character(character:DialogicCharacter, container: DialogicNode_Portrait
423452
if not character:
424453
printerr('[DialogicError] Cannot call add_portrait() with null character.')
425454
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)
426462

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)
433466

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
435470

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}
438472

439-
return character_node
473+
_move_character(character_node, position_id)
474+
await _change_portrait(character_node, portrait)
440475

476+
return character_node
477+
else:
478+
push_error('[Dialogic] Failed to load character "' + str(character.resource_path) + '".')
479+
return null
441480

442481
## Changes the portrait of a character. Only works with joined characters.
443482
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
455494

456495
fade_animation = DialogicPortraitAnimationUtil.guess_animation(fade_animation, DialogicPortraitAnimationUtil.AnimationType.CROSSFADE)
457496

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)
459498
dialogic.current_state_info.portraits[character.resource_path].portrait = info.portrait
460499
_change_portrait_mirror(
461500
dialogic.current_state_info.portraits[character.resource_path].node,
@@ -629,7 +668,8 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
629668

630669
if leave_animation and leave_animation_length:
631670
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()
633673
else:
634674
character_node.get_parent().remove_child(character_node)
635675
character_node.queue_free()
@@ -640,7 +680,19 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
640680
continue
641681

642682
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
644696

645697
elif portrait.is_empty():
646698
continue
@@ -655,10 +707,10 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
655707

656708
fade_animation = DialogicPortraitAnimationUtil.guess_animation(fade_animation, DialogicPortraitAnimationUtil.AnimationType.CROSSFADE)
657709

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
660712

661-
_change_portrait(character_node, portrait, fade_animation, fade_length)
713+
await _change_portrait(character_node, portrait, fade_animation, fade_length)
662714

663715
# if the character has no portraits _change_portrait won't actually add a child node
664716
if character_node.get_child_count() == 0:
@@ -677,7 +729,7 @@ func change_speaker(speaker: DialogicCharacter = null, portrait := "") -> void:
677729
var join_animation_length := _get_join_default_length()
678730

679731
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
681733

682734
_change_portrait_mirror(character_node)
683735

0 commit comments

Comments
 (0)