Get Key from KeyId from a StringTable / SharedTableData

Hello. I’m writing an editor script to generate voice audio clips for a string table. I got it to work some time ago, but a new string table I just added is causing some issues. In order to name the generated audio files properly, I need the ‘key’ from a table entry, but the key is always null. I tried multiple methods:

This is what worked previously:

var stringTableEntry = LocalizationSettings.StringDatabase.GetTableEntry(bank.sourceStringTable.TableReference, entry.KeyId);
var stringTableKey = stringTableEntry.Entry.SharedEntry.Key;

But the shared entry is null, so this throws an Exception. It works for other string tables though.

I also tried to get the shared table and get the key from that, like this:

Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to key {sharedData.Entries.FirstOrDefault(e => e.Id == entry.KeyId)?.Key}");
Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to source table entry {sourceTable.GetEntry(entry.KeyId)}");
Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to shared entry {sharedData.GetEntry(entry.KeyId)}");

But the key is always null, despite definitely existing in the localization string table view:

Please help me, I’m losing so much time on this.

I tried updating the localization package to 1.5.0-pre.6, but this doesn’t change anything in this case.

I cant see any reason why it would be null, are you sure it’s not the Entry that is null?
If this is an editor script then I would use our Editor api instead.
LocalizationEditorSettings can do most of what you need.

var collection = LocalizationEditorSettings.GetStringTableCollection("my collection");
var key = collection.SharedData.GetKey(entry.KeyId);

I printed all SharedTableEntries from SharedTableData, they contain keys and look fine.

The issue is that their KeyIds differ from the string table ones, that’s why it’s not matching properly.

For example, R_Alex_9 (last line in the screenshot) is 1482006498979858 in the shared table, but 1454216161959944 in the string table.

I tried the code you’ve posted, and the result is null.

var stringTableCollection = LocalizationEditorSettings.GetStringTableCollection(sharedTableName);
var key = stringTableCollection.SharedData.GetKey(entry.KeyId);
Debug.Log(key);

I guess I can change my approach and iterate all shared table data entries and get string table entries based on them, not the other way around.

Ok, for anyone dealing with something similar: the new approach seems to work. I iterate all shared table data entries and use their keys to get string table entries

// iterate keys in table
var sourceTable = bank.sourceStringTable.GetTable();
var sharedTableName = sourceTable.SharedData.TableCollectionName;
                  
var sourceTableValues = sourceTable.Values;
Debug.Log($"[TextToSpeechDatabase] Indexing {sourceTableValues.Count} entries from bank {bank.sourceStringTable.TableReference.TableCollectionName}");
                  
var sharedData = sourceTable.SharedData;
Debug.Log($"[TextToSpeechDatabase] Shared table: {sharedData.Entries.Count}");

foreach (var sharedTableEntry in sharedData.Entries)
{
    Debug.Log($"[TextToSpeechDatabase] Shared table entry: {sharedTableEntry}, matching entry: {sourceTable.GetEntry(sharedTableEntry.Key)}");
}

That doesn’t sound right. If you have string table entries with Key id values that are not in the shared table data then something has gone wrong and those entries will likely be lost at some point. They also won’t have Key names because that’s stored in the shared table data.
Did you delete the shared data entries but not the string table ones at some point?

Hey. No, I didn’t delete any string tables or anything of this sort, I’m really confused by this. Getting keys using the following method has worked fine before:

var stringTableEntry = LocalizationSettings.StringDatabase.GetTableEntry(bank.sourceStringTable.TableReference, entry.KeyId);
var stringTableKey = stringTableEntry.Entry.SharedEntry.Key;

But in this case, the shared entry is null, which I don’t think should ever happen. I guess this particular string table got corrupted somehow, or I ran into some kind of bug.

The values and amount of entries seem to match between the shared data and string table values, but the IDs are different.

Using a key from shared table data to get an entry from a string table works as expected, which is even more weird, but I’m glad it worked out and I can move on :smile:

Do you have the same entries in multiple string table collections? Maybe you are using the wrong shared table data?

I got the shared table data from the string table, like this

var sourceTable = bank.sourceStringTable.GetTable();
var sharedData = sourceTable.SharedData;

This is a string table with 2 locales. I only had an issue with this particular one. The 10 other ones didn’t cause any issues of this kind.