Skip to content

Commit

Permalink
Add support for randomizing groups in dialogue
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhoad committed Aug 23, 2022
1 parent b31cfcf commit bd46549
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions addons/dialogue_manager/dialogue_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var _trash: Node = Node.new()


func _ready() -> void:
randomize()

# Cache the known Node2D properties
_node_properties = ["Script Variables"]
var temp_node = Node2D.new()
Expand Down Expand Up @@ -324,6 +326,13 @@ func get_with_replacements(text: String, replacements: Array) -> String:
var value = resolve(replacement.get("expression").duplicate(true))
text = text.replace(replacement.get("value_in_text"), str(value))

# Resolve random groups
var random_regex: RegEx = RegEx.new()
random_regex.compile("\\[\\[(?<options>.*?)\\]\\]")
for found in random_regex.search_all(text):
var options = found.get_string("options").split("|")
text = text.replace("[[%s]]" % found.get_string("options"), options[rand_range(0, options.size())])

return text


Expand Down
2 changes: 2 additions & 0 deletions docs/Writing_Dialogue.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ A node will continue until another title is encountered or the end of the file.

A dialogue line is either just text or in the form of "Character: What they say".

You can add a bit of random variation with text surrounded by `[[]]`. For example, `Nathan: [[Hi|Hello|Howdy]]! I'm Nathan` would pick one from "Hi", "Hello", or "Howdy".

Dialogue lines can contain **variables** wrapped in "{{}}" (in either the character name or the dialogue). Any variables you use must be a property or method on one of your provided game states (see down below under **Settings, Runtime**).

```
Expand Down

0 comments on commit bd46549

Please sign in to comment.