Changing Arguments best practice

Is there a best practice for changing string arguments before/after a StringReference has already been Localized?

Trying to piece it together, from what I can tell, you need to call:

StringReference.RefreshString() after setting arguments
but sometimes that method throws an exception “RefreshString should be used with StringChanged however no change handler has been registered.”

I’m guessing because LocalizeStringEvent may be disabled and upon doing so it removes a listener from StringChanged.

So, should I always be checking LocalizeStringEvent.IsActiveAndEnabled before setting Arguments?
or checking if StringReference.StringChanged != null?

seems strange to me.
Setting arguments shouldn’t be worried about whether something is listening or not.
If something is, great, it gets updated. If not, it doesn’t get updated.

Or is there something I’m missing?

Hmm, can’t check StringReference.StringChanged != null
it would appear I cannot force a RefreshString safely because I can’t know if anything is listening or not.
:frowning:

I see a “ForceUpdate” method, but it’s not public.

We have added a new property in the next version called HasChangeHandler which will let you check if StringChanged is null.

If you just want to update the arguments then changing the Arguments property will be enough, it will then use those arguments the next time it updates.
RefreshString is more for when the argument values may change over time, such as if an argument was a class instance and its properties were changing over time.

So at the moment you would do something like this:

  • Set Arguments
  • if the LocalizeStringEvent is IsActiveAndEnabled then call RefreshString otherwise it will do it automatically when it is enabled.

Would it help if we did an auto update when the Arguments value was set?
Would you prefer the ChangeHandler to stay active when the component is disabled?

I would prefer that it auto refresh when I set arguments, and not worry if there aren’t any listeners. Or at least, exit early rather than throw an exception.

HasChangeHandler is fine though. I can use that to build an extension method for my purposes.

LocalizeStringEvent should totally remove the ChangeHandler listener when it’s disabled. Keep that functionality.
I think the issue is the raising of an exception if no listeners are added to StringReference.
Just going on my experience with C# events and UnityEvents, if there aren’t any listeners, nothing happens, but you don’t get an exception.

Here’s how I’m currently handling a ForceRefresh through some extension methods

public static void SetEntry(this LocalizedString reference, TableEntryReference entry, bool forceUpdate = false) {

    if (LocalizationSettings.HasStringDatabase) {

        reference.SetReference(LocalizationSettings.StringDatabase.DefaultTable, entry, forceUpdate);
    }
}

public static void SetReference(this LocalizedString reference, TableReference table, TableEntryReference entry, bool forceUpdate) {

    //if forcing an update, clear the references before setting

    if (forceUpdate) {

        reference.SetReference(default, default);
    }

    reference.SetReference(table, entry);
}

public static void ForceRefresh(this LocalizedString reference) {

    reference.SetReference(reference.TableReference, reference.TableEntryReference, true);
}

A bit hacky, but it seems to be working ok

1 Like

It makes sense to remove the exception and just do nothing instead.
I’ll also add a task to add the auto-refresh if Arguments is set.
Thanks

1 Like