Skip to content

Commit

Permalink
Add condition to choice (#960)
Browse files Browse the repository at this point in the history
Adds a condition input to the choice event. UI is very bad but functionality is there. You can also choose, what to do if the condition isn't met (Default/Hide/Disable). Default is currently just the same as Hide, but I want to add a setting for this later.

In the text format you can write the conditions like this:
```
Emilio: Wow, some options are disabled!
- This is disabled [if false] [else disable]
- This might be disabled [if some_cool_condition] [else disabeld]
- This will be hidden [if false] [else hide]
- This is always visible
```

# Other
- moved the condition execution into a function of the dialogic_game_handler, because now multiple events (and the game_handler itself) use it.
  • Loading branch information
Jowan-Spooner authored Jun 28, 2022
1 parent 754afbf commit 78db1a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
25 changes: 23 additions & 2 deletions addons/dialogic/Events/Choice/event.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('- (?<text>[^\\[]*)(\\[if (?<condition>[^\\]]+)])?\\s?(\\[else (?<else_option>[^\\]\\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:
Expand All @@ -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}})
4 changes: 1 addition & 3 deletions addons/dialogic/Events/Condition/event.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions addons/dialogic/Other/DialogicGameHandler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 78db1a4

Please sign in to comment.