diff --git a/addons/dialogic/Events/Choice/event.gd b/addons/dialogic/Events/Choice/event.gd index 55363c608..783306563 100644 --- a/addons/dialogic/Events/Choice/event.gd +++ b/addons/dialogic/Events/Choice/event.gd @@ -2,8 +2,12 @@ tool extends DialogicEvent class_name DialogicChoiceEvent +enum IfFalseActions {DEFAULT, HIDE, DISABLE} + # DEFINE ALL PROPERTIES OF THE EVENT var Text :String = "" +var Condition:String = "" +var IfFalseAction = IfFalseActions.DEFAULT func _execute() -> void: # I have no idea how this event works @@ -31,15 +35,30 @@ func get_as_string_to_store() -> String: var result_string = "" result_string = "- "+Text + if Condition: + result_string += " [if "+Condition+"]" + + if IfFalseAction == IfFalseActions.HIDE: + result_string += " [else hide]" + elif IfFalseAction == IfFalseActions.DISABLE: + result_string += " [else disable]" return result_string ## THIS HAS TO READ ALL THE DATA FROM THE SAVED STRING (see above) func load_from_string_to_store(string:String): + var regex = RegEx.new() + regex.compile('- (?[^\\[]*)(\\[if (?[^\\]]+)])?\\s?(\\[else (?[^\\]\\n]*)\\])?') + var result = regex.search(string.strip_edges()) - Text = string.strip_edges().trim_prefix("-").strip_edges() - + Text = result.get_string('text') + Condition = result.get_string('condition') + if result.get_string('else_option'): + IfFalseAction = { + 'default':IfFalseActions.DEFAULT, + 'hide':IfFalseActions.HIDE, + 'disable':IfFalseActions.DISABLE}.get(result.get_string('else_option'), IfFalseActions.DEFAULT) # RETURN TRUE IF THE GIVEN LINE SHOULD BE LOADED AS THIS EVENT func is_valid_event_string(string:String) -> bool: @@ -56,3 +75,5 @@ func is_valid_event_string(string:String) -> bool: func build_event_editor(): add_header_edit("Text", ValueType.SinglelineText) + add_body_edit("Condition", ValueType.SinglelineText, 'if ') + add_body_edit("IfFalseAction", ValueType.FixedOptionSelector, 'else ', '', {'selector_options':{"Default":IfFalseActions.DEFAULT, "Hide":IfFalseActions.HIDE, "Disable":IfFalseActions.DISABLE}}) diff --git a/addons/dialogic/Events/Condition/event.gd b/addons/dialogic/Events/Condition/event.gd index 56a5d959d..14cf1a110 100644 --- a/addons/dialogic/Events/Condition/event.gd +++ b/addons/dialogic/Events/Condition/event.gd @@ -13,9 +13,7 @@ func _execute() -> void: finish() return - var expr = Expression.new() - expr.parse(Condition) - var result = expr.execute() + var result = dialogic_game_handler.execute_condition(Condition) if not result: var idx = dialogic_game_handler.current_event_idx var ignore = 1 diff --git a/addons/dialogic/Other/DialogicGameHandler.gd b/addons/dialogic/Other/DialogicGameHandler.gd index 1aa7c6a15..b9dc020aa 100644 --- a/addons/dialogic/Other/DialogicGameHandler.gd +++ b/addons/dialogic/Other/DialogicGameHandler.gd @@ -171,15 +171,23 @@ func show_current_choices() -> void: var button_idx = 1 for choice_index in get_current_choice_indexes(): var choice_event = current_timeline_events[choice_index] - show_choice(button_idx, choice_event.Text, true, choice_index) - button_idx += 1 - + # check if condition is false + if not choice_event.Condition.empty() and not execute_condition(choice_event.Condition): + # check what to do in this case + if choice_event.IfFalseAction == DialogicChoiceEvent.IfFalseActions.DISABLE: + show_choice(button_idx, choice_event.Text, false, choice_index) + button_idx += 1 + # else just show it + else: + show_choice(button_idx, choice_event.Text, true, choice_index) + button_idx += 1 func show_choice(button_index:int, text:String, enabled:bool, event_index:int) -> void: var idx = 1 for node in get_tree().get_nodes_in_group('dialogic_choice_button'): if (node.choice_index == button_index) or (idx == button_index and node.choice_index == -1): node.show() node.text = parse_variables(text) + node.disabled = not enabled node.connect('pressed', self, 'choice_selected', [event_index]) if node.choice_index >0: idx = node.choice_index @@ -365,6 +373,11 @@ func get_current_portrait_info_of_character(character:DialogicCharacter) -> Arra func interpolate_volume_linearly(value, node): node.volume_db = linear2db(value) +func execute_condition(condition:String) -> bool: + var expr = Expression.new() + expr.parse(condition) + return true if expr.execute() else false + func get_autoloads() -> Array: var autoloads = [] for c in get_tree().root.get_children():