Skip to content

Commit 3308160

Browse files
Improve runtime resources (timelines and characters created through code) (#2537)
* Fix for dictionary field in custom portrait export overrides * Allow registering identifiers for runtime resources This makes both DialogicTimeline and DialogicCharacter extend DialogicIdentifiableResource which basically adds a get_identifier and set_identifier method. If an identifier is set at runtime for a non-saved resource, the resource is registered to the directories directly (without a path) allowing for the use of those resources in timelines from then on. This also once again allows using "invalid" character names in timelines and will now even interpret the portrait as a color, which allows to have one-off characters say something. However the portrait picker is not shown in the visual editor for those characters for now so this is kinda a text-editor exclusive rn.
1 parent 8355cac commit 3308160

18 files changed

+142
-49
lines changed

addons/dialogic/Core/DialogicGameHandler.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,6 @@ func print_debug_moment() -> void:
439439
if not current_timeline:
440440
return
441441

442-
printerr("\tAt event ", current_event_idx+1, " (",current_timeline_events[current_event_idx].event_name, ' Event) in timeline "', DialogicResourceUtil.get_unique_identifier(current_timeline.resource_path), '" (',current_timeline.resource_path,').')
442+
printerr("\tAt event ", current_event_idx+1, " (",current_timeline_events[current_event_idx].event_name, ' Event) in timeline "', current_timeline.get_identifier(), '" (',current_timeline.resource_path,').')
443443
print("\n")
444444
#endregion

addons/dialogic/Core/DialogicResourceUtil.gd

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static func add_resource_to_directory(file_path:String, directory:Dictionary) ->
6767

6868
## Returns the unique identifier for the given resource path.
6969
## Returns an empty string if no identifier was found.
70-
static func get_unique_identifier(file_path:String) -> String:
70+
static func get_unique_identifier_by_path(file_path:String) -> String:
7171
if not file_path: return ""
7272
var identifier: Variant = get_directory(file_path.get_extension()).find_key(file_path)
7373
if typeof(identifier) == TYPE_STRING:
@@ -78,12 +78,15 @@ static func get_unique_identifier(file_path:String) -> String:
7878
## Returns the resource associated with the given unique identifier.
7979
## The expected extension is needed to use the right directory.
8080
static func get_resource_from_identifier(identifier:String, extension:String) -> Resource:
81-
var path: String = get_directory(extension).get(identifier, '')
82-
if ResourceLoader.exists(path):
83-
return load(path)
81+
var value: Variant = get_directory(extension).get(identifier, '')
82+
if typeof(value) == TYPE_STRING and ResourceLoader.exists(value):
83+
return load(value)
84+
elif value is Resource:
85+
return value
8486
return null
8587

8688

89+
## Editor Only
8790
static func change_unique_identifier(file_path:String, new_identifier:String) -> void:
8891
var directory := get_directory(file_path.get_extension())
8992
var key: String = directory.find_key(file_path)
@@ -117,6 +120,21 @@ static func remove_resource(file_path:String) -> void:
117120
static func is_identifier_unused(extension:String, identifier:String) -> bool:
118121
return not identifier in get_directory(extension)
119122

123+
124+
## While usually the directory maps identifiers to paths, this method (only supposed to be used at runtime)
125+
## allows mapping resources that are not saved to an identifier.
126+
static func register_runtime_resource(resource:Resource, identifier:String, extension:String) -> void:
127+
var directory := get_directory(extension)
128+
directory[identifier] = resource
129+
set_directory(extension, directory)
130+
131+
132+
static func get_runtime_unique_identifier(resource:Resource, extension:String) -> String:
133+
var identifier: Variant = get_directory(extension).find_key(resource)
134+
if typeof(identifier) == TYPE_STRING:
135+
return identifier
136+
return ""
137+
120138
#endregion
121139

122140
#region LABEL CACHE

addons/dialogic/Core/DialogicUtil.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ static func setup_script_property_edit_node(property_info: Dictionary, value:Var
528528
TYPE_DICTIONARY:
529529
input = load("res://addons/dialogic/Editor/Events/Fields/field_dictionary.tscn").instantiate()
530530
input.property_name = property_info["name"]
531+
input.set_value(value)
531532
input.value_changed.connect(_on_export_dict_submitted.bind(property_changed))
532533
TYPE_OBJECT:
533534
input = load("res://addons/dialogic/Editor/Common/hint_tooltip_icon.tscn").instantiate()

addons/dialogic/Editor/CharacterEditor/character_editor.gd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func _open_resource(resource:Resource) -> void:
7171
load_portrait_tree()
7272

7373
loading = false
74-
character_loaded.emit(resource.resource_path)
74+
character_loaded.emit(current_resource.resource_path)
7575

76-
%CharacterName.text = DialogicResourceUtil.get_unique_identifier(resource.resource_path)
76+
%CharacterName.text = current_resource.get_identifier()
7777

7878
$NoCharacterScreen.hide()
7979
%PortraitChangeInfo.hide()
@@ -549,7 +549,7 @@ func report_name_change(item: TreeItem) -> void:
549549
editors_manager.reference_manager.add_portrait_ref_change(
550550
item.get_meta('previous_name'),
551551
%PortraitTree.get_full_item_name(item),
552-
[DialogicResourceUtil.get_unique_identifier(current_resource.resource_path)])
552+
[current_resource.get_identifier()])
553553
item.set_meta('previous_name', %PortraitTree.get_full_item_name(item))
554554
%PortraitChangeInfo.show()
555555

addons/dialogic/Editor/Common/sidebar.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func _on_right_click_menu_id_pressed(id: int) -> void:
433433
)
434434
4: # COPY IDENTIFIER
435435
DisplayServer.clipboard_set(
436-
DialogicResourceUtil.get_unique_identifier(
436+
DialogicResourceUtil.get_unique_identifier_by_path(
437437
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
438438
)
439439
)

addons/dialogic/Editor/Events/Fields/field_options_dynamic.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func _set_value(value:Variant) -> void:
5050
Modes.PRETTY_PATH:
5151
%Search.text = DialogicUtil.pretty_name(value)
5252
Modes.IDENTIFIER when value.begins_with("res://"):
53-
%Search.text = DialogicResourceUtil.get_unique_identifier(value)
53+
%Search.text = DialogicResourceUtil.get_unique_identifier_by_path(value)
5454
Modes.ANY_VALID_STRING when validation_func:
5555
%Search.text = validation_func.call(value).get('valid_text', value)
5656
_:
@@ -361,7 +361,7 @@ func _can_drop_data(position:Vector2, data:Variant) -> bool:
361361
func _drop_data(position:Vector2, data:Variant) -> void:
362362
var path := str(data.files[0])
363363
if mode == Modes.IDENTIFIER:
364-
path = DialogicResourceUtil.get_unique_identifier(path)
364+
path = DialogicResourceUtil.get_unique_identifier_by_path(path)
365365
_set_value(path)
366366
value_changed.emit(property_name, path)
367367
current_value_updated = false

addons/dialogic/Editor/Events/Fields/field_options_fixed.gd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ extends DialogicVisualEditorField
33

44
## Event block field for constant options. For varying options use ComplexPicker.
55

6-
var options: Array = []
6+
var options: Array = []:
7+
set(o):
8+
options = o
9+
if current_value != -1:
10+
set_value(current_value)
711

812
## if true, only the symbol will be displayed. In the dropdown text will be visible.
913
## Useful for making UI simpler
@@ -21,6 +25,7 @@ func _ready() -> void:
2125
call("get_popup").index_pressed.connect(index_pressed)
2226

2327

28+
2429
func _load_display_info(info:Dictionary) -> void:
2530
options = info.get('options', [])
2631
self.disabled = info.get('disabled', false)
@@ -35,7 +40,7 @@ func _set_value(value:Variant) -> void:
3540
if !symbol_only:
3641
self.text = option['label']
3742
self.icon = option.get('icon', null)
38-
current_value = value
43+
current_value = value
3944

4045

4146
func get_value() -> Variant:

addons/dialogic/Editor/Inspector/timeline_inspector_field.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func _update_property() -> void:
6161
updating = true
6262
current_value = new_value
6363
if current_value:
64-
field.set_value(DialogicResourceUtil.get_unique_identifier(current_value.resource_path))
64+
field.set_value(current_value.get_identifier())
6565
button.show()
6666
else:
6767
button.hide()

addons/dialogic/Editor/TimelineEditor/timeline_editor.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func _open_resource(resource:Resource) -> void:
7373
EditorMode.TEXT:
7474
%TextEditor.load_timeline(current_resource)
7575
$NoTimelineScreen.hide()
76-
%TimelineName.text = DialogicResourceUtil.get_unique_identifier(current_resource.resource_path)
76+
%TimelineName.text = current_resource.get_identifier()
7777
play_timeline_button.disabled = false
7878

7979

addons/dialogic/Modules/Character/event_character.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var character_identifier: String:
8282
if character_identifier == '--All--':
8383
return '--All--'
8484
if character:
85-
var identifier := DialogicResourceUtil.get_unique_identifier(character.resource_path)
85+
var identifier := character.get_identifier()
8686
if not identifier.is_empty():
8787
return identifier
8888
return character_identifier
@@ -225,7 +225,7 @@ func to_text() -> String:
225225
if action == Actions.LEAVE and character_identifier == '--All--':
226226
result_string += "--All--"
227227
elif character:
228-
var name := DialogicResourceUtil.get_unique_identifier(character.resource_path)
228+
var name := character.get_character_name()
229229

230230
if name.count(" ") > 0:
231231
name = '"' + name + '"'

0 commit comments

Comments
 (0)