Skip to content

Commit 2ab6612

Browse files
committed
Add DialogicStylesUtil and other performance fixes
The new best way to preload a style is with Dialogic.Styles.preload_style("name_or_path")
1 parent e6faa48 commit 2ab6612

File tree

9 files changed

+106
-44
lines changed

9 files changed

+106
-44
lines changed

addons/dialogic/Core/DialogicGameHandler.gd

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func _ready() -> void:
165165

166166
clear()
167167

168+
DialogicResourceUtil.update_event_cache()
169+
168170
dialog_ending_timeline = DialogicTimeline.new()
169171
dialog_ending_timeline.from_text("[clear]")
170172

@@ -207,10 +209,10 @@ func start_timeline(timeline:Variant, label_or_idx:Variant = "") -> void:
207209
# load the resource if only the path is given
208210
if typeof(timeline) == TYPE_STRING:
209211
#check the lookup table if it's not a full file name
210-
if (timeline as String).contains("res://") or (timeline as String).contains("uid://"):
211-
timeline = load((timeline as String))
212+
if "://" in timeline:
213+
timeline = load(timeline)
212214
else:
213-
timeline = DialogicResourceUtil.get_timeline_resource((timeline as String))
215+
timeline = DialogicResourceUtil.get_timeline_resource(timeline)
214216

215217
if timeline == null:
216218
printerr("[Dialogic] There was an error loading this timeline. Check the filename, and the timeline for errors")
@@ -243,7 +245,11 @@ func start_timeline(timeline:Variant, label_or_idx:Variant = "") -> void:
243245
func preload_timeline(timeline_resource:Variant) -> Variant:
244246
# I think ideally this should be on a new thread, will test
245247
if typeof(timeline_resource) == TYPE_STRING:
246-
timeline_resource = load((timeline_resource as String))
248+
if "://" in timeline_resource:
249+
timeline_resource = load(timeline_resource)
250+
else:
251+
timeline_resource = DialogicResourceUtil.get_timeline_resource(timeline_resource)
252+
247253
if timeline_resource == null:
248254
printerr("[Dialogic] There was an error preloading this timeline. Check the filename, and the timeline for errors")
249255
return null

addons/dialogic/Core/DialogicResourceUtil.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static func update() -> void:
1313
update_directory('.dtl')
1414
update_label_cache()
1515
update_audio_channel_cache()
16+
DialogicStylesUtil.update_style_directory()
1617

1718

1819
#region RESOURCE DIRECTORIES

addons/dialogic/Core/DialogicUtil.gd

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ static func _update_autoload_subsystem_access() -> void:
100100

101101

102102
static func get_indexers(include_custom := true, force_reload := false) -> Array[DialogicIndexer]:
103-
if Engine.get_main_loop().has_meta('dialogic_indexers') and !force_reload:
103+
if Engine.get_main_loop().has_meta('dialogic_indexers') and not force_reload:
104104
return Engine.get_main_loop().get_meta('dialogic_indexers')
105105

106106
var indexers: Array[DialogicIndexer] = []
107-
108107
for file in listdir(DialogicUtil.get_module_path(''), false):
109108
var possible_script: String = DialogicUtil.get_module_path(file).path_join("index.gd")
110109
if ResourceLoader.exists(possible_script):
@@ -297,40 +296,6 @@ static func _get_value_in_dictionary(path:String, dictionary:Dictionary, default
297296
#endregion
298297

299298

300-
301-
#region STYLES
302-
################################################################################
303-
304-
static func get_default_layout_base() -> PackedScene:
305-
return load(DialogicUtil.get_module_path('DefaultLayoutParts').path_join("Base_Default/default_layout_base.tscn"))
306-
307-
308-
static func get_fallback_style() -> DialogicStyle:
309-
return load(DialogicUtil.get_module_path('DefaultLayoutParts').path_join("Style_VN_Default/default_vn_style.tres"))
310-
311-
312-
static func get_default_style() -> DialogicStyle:
313-
var default: String = ProjectSettings.get_setting('dialogic/layout/default_style', '')
314-
if !ResourceLoader.exists(default):
315-
return get_fallback_style()
316-
return load(default)
317-
318-
319-
static func get_style_by_name(name:String) -> DialogicStyle:
320-
if name.is_empty():
321-
return get_default_style()
322-
323-
var styles: Array = ProjectSettings.get_setting('dialogic/layout/style_list', [])
324-
for style in styles:
325-
if not ResourceLoader.exists(style):
326-
continue
327-
if load(style).name == name:
328-
return load(style)
329-
330-
return get_default_style()
331-
#endregion
332-
333-
334299
#region SCENE EXPORT OVERRIDES
335300
################################################################################
336301

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
@tool
2+
class_name DialogicStylesUtil
3+
extends Node
4+
5+
static var style_directory := {}
6+
7+
#region STYLES
8+
################################################################################
9+
10+
static func update_style_directory() -> void:
11+
return ProjectSettings.get_setting('dialogic/layout/style_directory', {})
12+
13+
14+
static func build_style_directory() -> void:
15+
style_directory.clear()
16+
17+
var default := get_default_style_path()
18+
if ResourceLoader.exists(default):
19+
style_directory[""] = default
20+
21+
var styles: Array = ProjectSettings.get_setting('dialogic/layout/style_list', [])
22+
for style_path in styles:
23+
if not ResourceLoader.exists(style_path):
24+
continue
25+
# TODO this is bad
26+
var resource: DialogicStyle = load(style_path)
27+
style_directory[resource.name] = resource
28+
ProjectSettings.set_setting('dialogic/layout/style_directory', style_directory)
29+
ProjectSettings.save()
30+
31+
32+
static func get_default_style_path() -> String:
33+
return ProjectSettings.get_setting('dialogic/layout/default_style', '')
34+
35+
36+
static func get_default_layout_base() -> PackedScene:
37+
return load(DialogicUtil.get_module_path('DefaultLayoutParts').path_join("Base_Default/default_layout_base.tscn"))
38+
39+
40+
static func get_fallback_style_path() -> String:
41+
return DialogicUtil.get_module_path('DefaultLayoutParts').path_join("Style_VN_Default/default_vn_style.tres")
42+
43+
44+
static func get_fallback_style() -> DialogicStyle:
45+
return load(get_fallback_style_path())
46+
47+
48+
static func get_style_path(name_or_path:String) -> String:
49+
var path := ""
50+
if name_or_path.begins_with("res://"):
51+
if not ResourceLoader.exists(name_or_path):
52+
name_or_path = ""
53+
54+
if name_or_path in style_directory:
55+
path = style_directory[name_or_path]
56+
57+
if not name_or_path:
58+
path = get_default_style_path()
59+
60+
if not name_or_path or not ResourceLoader.exists(name_or_path):
61+
return get_fallback_style_path()
62+
63+
return name_or_path
64+
65+
66+
static func start_style_preload(name_or_path:String) -> void:
67+
ResourceLoader.load_threaded_request(get_style_path(name_or_path))
68+
69+
70+
static func get_style(style_name:String) -> DialogicStyle:
71+
var path := get_style_path(style_name)
72+
if ResourceLoader.load_threaded_get_status(path) == ResourceLoader.THREAD_LOAD_LOADED:
73+
return ResourceLoader.load_threaded_get(path)
74+
75+
return load(path)
76+
77+
78+
#endregion
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://df671mnqushqc

addons/dialogic/Modules/Style/subsystem_styles.gd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ signal style_changed(info:Dictionary)
88
#region STATE
99
####################################################################################################
1010

11+
func _ready() -> void:
12+
DialogicStylesUtil.update_style_directory()
13+
14+
1115
func clear_game_state(_clear_flag := DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
1216
pass
1317

@@ -33,7 +37,7 @@ func change_style(style_name := "", is_base_style := true) -> Node:
3337
## [br] If [param state_reload] is true, the current state will be loaded into a new layout scenes nodes.
3438
## That should not be done before calling start() or load() as it would be unnecessary or cause double-loading.
3539
func load_style(style_name := "", parent: Node = null, is_base_style := true, state_reload := false) -> Node:
36-
var style := DialogicUtil.get_style_by_name(style_name)
40+
var style := DialogicStylesUtil.get_style(style_name)
3741

3842
var signal_info := {'style':style_name}
3943
dialogic.current_state_info['style'] = style_name
@@ -92,7 +96,7 @@ func create_layout(style: DialogicStyle, parent: Node = null) -> DialogicLayoutB
9296
var base_scene: DialogicLayoutBase
9397
var base_layer_info := style.get_layer_inherited_info("")
9498
if base_layer_info.path.is_empty():
95-
base_scene = DialogicUtil.get_default_layout_base().instantiate()
99+
base_scene = DialogicStylesUtil.get_default_layout_base().instantiate()
96100
else:
97101
base_scene = load(base_layer_info.path).instantiate()
98102

@@ -172,4 +176,8 @@ func get_first_node_in_layout(group_name: String) -> Node:
172176
return node
173177
return null
174178

179+
180+
func preload_style(name_or_path:String = "") -> void:
181+
DialogicStylesUtil.start_style_preload(name_or_path)
182+
175183
#endregion

addons/dialogic/Modules/StyleEditor/style_editor.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func _on_inheritance_index_pressed(index:int) -> void:
346346

347347

348348
func _on_start_styling_button_pressed() -> void:
349-
var new_style := DialogicUtil.get_fallback_style().clone()
349+
var new_style := DialogicStylesUtil.get_fallback_style().clone()
350350

351351
find_parent('EditorView').godot_file_dialog(
352352
add_style_undoable.bind(new_style),

addons/dialogic/Resources/dialogic_style.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func get_layer_info(id:String) -> Dictionary:
7373
if layer_resource.scene != null:
7474
info.path = layer_resource.scene.resource_path
7575
elif id == "":
76-
info.path = DialogicUtil.get_default_layout_base().resource_path
76+
info.path = DialogicStylesUtil.get_default_layout_base().resource_path
7777

7878
info.overrides = layer_resource.overrides.duplicate()
7979

addons/dialogic/Resources/event.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ func _get_icon() -> Resource:
443443

444444

445445
func set_default_color(value:Variant) -> void:
446+
# Skip in running games
447+
if not Engine.is_editor_hint():
448+
return
446449
dialogic_color_name = value
447450
event_color = DialogicUtil.get_color(value)
448451

0 commit comments

Comments
 (0)