Browse Source

SayDialog now supports full variable substitution when setting character names.

master
Christopher 8 years ago
parent
commit
cdb3596a62
  1. 2
      Assets/Fungus/Scripts/Commands/Say.cs
  2. 20
      Assets/Fungus/Scripts/Components/SayDialog.cs
  3. 1678
      Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

2
Assets/Fungus/Scripts/Commands/Say.cs

@ -102,7 +102,7 @@ namespace Fungus
sayDialog.SetActive(true); sayDialog.SetActive(true);
sayDialog.SetCharacter(character, flowchart); sayDialog.SetCharacter(character);
sayDialog.SetCharacterImage(portrait); sayDialog.SetCharacterImage(portrait);
string displayText = storyText; string displayText = storyText;

20
Assets/Fungus/Scripts/Components/SayDialog.cs

@ -52,6 +52,8 @@ namespace Fungus
// Most recent speaking character // Most recent speaking character
protected static Character speakingCharacter; protected static Character speakingCharacter;
protected StringSubstituter stringSubstituter = new StringSubstituter();
protected Writer GetWriter() protected Writer GetWriter()
{ {
if (writer != null) if (writer != null)
@ -125,6 +127,8 @@ namespace Fungus
// Character image is hidden by default. // Character image is hidden by default.
SetCharacterImage(null); SetCharacterImage(null);
} }
stringSubstituter.CacheSubstitutionHandlers();
} }
protected virtual void LateUpdate() protected virtual void LateUpdate()
@ -261,8 +265,7 @@ namespace Fungus
/// Sets the active speaking character. /// Sets the active speaking character.
/// </summary> /// </summary>
/// <param name="character">The active speaking character.</param> /// <param name="character">The active speaking character.</param>
/// <param name="flowchart">An optional Flowchart to use for variable substitution in the character name string.</param> public virtual void SetCharacter(Character character)
public virtual void SetCharacter(Character character, Flowchart flowchart = null)
{ {
if (character == null) if (character == null)
{ {
@ -314,12 +317,7 @@ namespace Fungus
// Use game object name as default // Use game object name as default
characterName = character.GetObjectName(); characterName = character.GetObjectName();
} }
if (flowchart != null)
{
characterName = flowchart.SubstituteVariables(characterName);
}
SetCharacterName(characterName, character.NameColor); SetCharacterName(characterName, character.NameColor);
} }
} }
@ -357,7 +355,7 @@ namespace Fungus
storyText != null && storyText != null &&
characterImage.gameObject.activeSelf) characterImage.gameObject.activeSelf)
{ {
if (startStoryTextWidth == 0) if (Mathf.Approximately(startStoryTextWidth, 0f))
{ {
startStoryTextWidth = storyText.rectTransform.rect.width; startStoryTextWidth = storyText.rectTransform.rect.width;
startStoryTextInset = storyText.rectTransform.offsetMin.x; startStoryTextInset = storyText.rectTransform.offsetMin.x;
@ -381,12 +379,14 @@ namespace Fungus
/// <summary> /// <summary>
/// Sets the character name to display on the Say Dialog. /// Sets the character name to display on the Say Dialog.
/// Supports variable substitution e.g. John {$surname}
/// </summary> /// </summary>
public virtual void SetCharacterName(string name, Color color) public virtual void SetCharacterName(string name, Color color)
{ {
if (nameText != null) if (nameText != null)
{ {
nameText.text = name; var subbedName = stringSubstituter.SubstituteStrings(name);
nameText.text = subbedName;
nameText.color = color; nameText.color = color;
} }
} }

1678
Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save