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

Change reference table in LineProvider at runtime #275

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
49 changes: 34 additions & 15 deletions Runtime/LineProviders/UnityLocalisedLineProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,56 @@ public override LocalizedLine GetLocalizedLine(Yarn.Line line)
return localizedLine;
}

public override void Start()
public override void Start() => SetTable(stringsTable, assetTable);

// We've been notified that a new strings table has been loaded.
private void OnStringTableChanged(StringTable newTable)
{
currentStringsTable = newTable;
}

// We've been notified that a new asset table has been loaded.
private void OnAssetTableChanged(AssetTable value)
{
currentAssetTable = value;
}

/// <summary>
/// Updates referenced LocalizedStringTable.
/// </summary>
public void SetTable(LocalizedStringTable stringsTable) => SetTable(stringsTable, assetTable);

/// <summary>
/// Updates referenced LocalizedAssetTable.
/// </summary>
public void SetTable(LocalizedAssetTable assetTable) => SetTable(stringsTable, assetTable);

/// <summary>
/// Updates both referenced LocalizedStringTable and LocalizedAssetTable.
/// </summary>
public void SetTable(LocalizedStringTable stringsTable, LocalizedAssetTable assetTable)
{
// doing an initial load of the strings
if (stringsTable != null)
if (stringsTable != null && this.stringsTable != stringsTable)
{
this.stringsTable = stringsTable;

// Adding an event handler to TableChanged will trigger an
// initial async load of the string table, which will call the
// handler on completion. So, we don't set currentStringsTable
// here, but instead we only ever do it in OnStringTableChanged.
stringsTable.TableChanged += OnStringTableChanged;
}

if (assetTable != null)
if (assetTable != null && this.assetTable != assetTable)
{
this.assetTable = assetTable;

// Same logic for asset table as for strings table, above.
assetTable.TableChanged += OnAssetTableChanged;
}
}

// We've been notified that a new strings table has been loaded.
private void OnStringTableChanged(StringTable newTable)
{
currentStringsTable = newTable;
}

// We've been notified that a new asset table has been loaded.
private void OnAssetTableChanged(AssetTable value)
{
currentAssetTable = value;
}

/// <summary>
/// Clears all loaded assets from the cache.
/// </summary>
Expand Down