Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variable and setting nodes not updating on type change #2068

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions addons/dialogic/Editor/Events/EventBlock/event_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,16 @@ func build_editor(build_header:bool = true, build_body:bool = false) -> void:

# Some things need to be called AFTER the field is added to the tree
if editor_node is DialogicVisualEditorField:
editor_node._set_value(resource.get(p.name))
# Only set the value if the field is visible
#
# This prevents events with varied value types (event_setting, event_variable)
# from injecting incorrect types into hidden fields, which then throw errors
# in the console.
if p.has('condition') and not p.condition.is_empty():
if _evaluate_visibility_condition(p):
editor_node._set_value(resource.get(p.name))
else:
editor_node._set_value(resource.get(p.name))

editor_node.value_changed.connect(set_property)

Expand Down Expand Up @@ -305,18 +314,15 @@ func recalculate_field_visibility() -> void:
if p.location == 1:
has_any_enabled_body_content = true
else:
var expr := Expression.new()
expr.parse(p.condition)
if expr.execute([], resource):
var visible = _evaluate_visibility_condition(p)
Jowan-Spooner marked this conversation as resolved.
Show resolved Hide resolved
if visible:
if p.node != null:
p.node.show()
if p.location == 1:
has_any_enabled_body_content = true
else:
if p.node != null:
p.node.hide()
if expr.has_execute_failed():
printerr("[Dialogic] Failed executing visibility condition for '",p.get('property', 'unnamed'),"': " + expr.get_error_text())
%ExpandButton.visible = has_any_enabled_body_content


Expand All @@ -327,10 +333,32 @@ func set_property(property_name:String, value:Variant) -> void:
end_node.parent_node_changed()


func _evaluate_visibility_condition(p: Dictionary) -> bool:
var expr := Expression.new()
expr.parse(p.condition)
var result: bool
if expr.execute([], resource):
result = true
else:
result = false
if expr.has_execute_failed():
printerr("[Dialogic] Failed executing visibility condition for '",p.get('property', 'unnamed'),"': " + expr.get_error_text())
return result


func _on_resource_ui_update_needed() -> void:
for node_info in field_list:
if node_info.node and node_info.node.has_method('set_value'):
node_info.node.set_value(resource.get(node_info.property))
# Only set the value if the field is visible
#
# This prevents events with varied value types (event_setting, event_variable)
# from injecting incorrect types into hidden fields, which then throw errors
# in the console.
if node_info.has('condition') and not node_info.condition.is_empty():
if _evaluate_visibility_condition(node_info):
node_info.node.set_value(resource.get(node_info.property))
else:
node_info.node.set_value(resource.get(node_info.property))
recalculate_field_visibility()


Expand Down
66 changes: 45 additions & 21 deletions addons/dialogic/Modules/Settings/event_setting.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,36 @@ extends DialogicEvent
### Settings

enum Modes {SET, RESET, RESET_ALL}
enum SettingValueType {
STRING,
NUMBER,
VARIABLE,
EXPRESSION
}

## The name of the setting to save to.
var name: String = ""
var _value_type := 0
var _value_type := 0 :
get:
return _value_type
set(_value):
_value_type = _value
if not _suppress_default_value:
match _value_type:
SettingValueType.STRING, SettingValueType.VARIABLE, SettingValueType.EXPRESSION:
value = ""
SettingValueType.NUMBER:
value = 0
ui_update_needed.emit()

var value: Variant = ""

var mode := Modes.SET

## Used to suppress _value_type from overwriting value with a default value when the type changes
## This is only used when initializing the event_variable.
var _suppress_default_value: bool = false

################################################################################
## INITIALIZE
################################################################################
Expand All @@ -28,14 +50,14 @@ func _execute() -> void:
dialogic.Settings.reset_all()
else:
match _value_type:
0:
SettingValueType.STRING:
dialogic.Settings.set(name, value)
1:
SettingValueType.NUMBER:
dialogic.Settings.set(name, float(value))
2:
SettingValueType.VARIABLE:
if dialogic.has_subsystem('VAR'):
dialogic.Settings.set(name, dialogic.VAR.get_variable('{'+value+'}'))
3:
SettingValueType.EXPRESSION:
if dialogic.has_subsystem('VAR'):
dialogic.Settings.set(name, dialogic.VAR.get_variable(value))
finish()
Expand Down Expand Up @@ -72,11 +94,11 @@ func to_text() -> String:
string += " = "
value = str(value)
match _value_type:
0: # String
SettingValueType.STRING: # String
string += '"'+value.replace('"', '\\"')+'"'
1,3: # Float or Expression
SettingValueType.NUMBER,SettingValueType.EXPRESSION: # Float or Expression
string += str(value)
2: # Variable
SettingValueType.VARIABLE: # Variable
string += '{'+value+'}'

return string
Expand All @@ -98,19 +120,21 @@ func from_text(string:String) -> void:
mode = Modes.RESET_ALL

if result.get_string('value'):
_suppress_default_value = true
value = result.get_string('value').strip_edges()
if value.begins_with('"') and value.ends_with('"') and value.count('"')-value.count('\\"') == 2:
value = result.get_string('value').strip_edges().replace('"', '')
_value_type = 0
_value_type = SettingValueType.STRING
elif value.begins_with('{') and value.ends_with('}') and value.count('{') == 1:
value = result.get_string('value').strip_edges().trim_suffix('}').trim_prefix('{')
_value_type = 2
_value_type = SettingValueType.VARIABLE
else:
value = result.get_string('value').strip_edges()
if value.is_valid_float():
_value_type = 1
_value_type = SettingValueType.NUMBER
else:
_value_type = 3
_value_type = SettingValueType.EXPRESSION
_suppress_default_value = false


func is_valid_event(string:String) -> bool:
Expand Down Expand Up @@ -138,33 +162,33 @@ func build_event_editor():
},
]})

add_header_edit('name', ValueType.DYNAMIC_OPTIONS, {'placeholder':'Type setting', 'suggestions_func':get_settings_suggestions}, 'mode != 2')
add_header_edit('name', ValueType.DYNAMIC_OPTIONS, {'placeholder':'Type setting', 'suggestions_func':get_settings_suggestions}, 'mode != Modes.RESET_ALL')
add_header_edit('_value_type', ValueType.FIXED_OPTIONS, {'left_text':'to',
'options': [
{
'label': 'String',
'icon': ["String", "EditorIcons"],
'value': 0
'value': SettingValueType.STRING
},{
'label': 'Number',
'icon': ["float", "EditorIcons"],
'value': 1
'value': SettingValueType.NUMBER
},{
'label': 'Variable',
'icon': ["ClassList", "EditorIcons"],
'value': 2
'value': SettingValueType.VARIABLE
},{
'label': 'Expression',
'icon': ["Variant", "EditorIcons"],
'value': 3
'value': SettingValueType.EXPRESSION
}],
'symbol_only':true},
'!name.is_empty() and mode == 0')
add_header_edit('value', ValueType.SINGLELINE_TEXT, {}, '!name.is_empty() and (_value_type == 0 or _value_type == 3) and mode == 0')
add_header_edit('value', ValueType.NUMBER, {}, '!name.is_empty() and _value_type == 1 and mode == 0')
'!name.is_empty() and mode == Modes.SET')
add_header_edit('value', ValueType.SINGLELINE_TEXT, {}, '!name.is_empty() and (_value_type == SettingValueType.STRING or _value_type == SettingValueType.EXPRESSION) and mode == Modes.SET')
add_header_edit('value', ValueType.NUMBER, {}, '!name.is_empty() and _value_type == SettingValueType.NUMBER and mode == Modes.SET')
add_header_edit('value', ValueType.DYNAMIC_OPTIONS,
{'suggestions_func' : get_value_suggestions, 'placeholder':'Select Variable'},
'!name.is_empty() and _value_type == 2 and mode == 0')
'!name.is_empty() and _value_type == SettingValueType.VARIABLE and mode == Modes.SET')


func get_settings_suggestions(filter:String) -> Dictionary:
Expand Down
Loading
Loading