Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Using ValueChanged event with custom ISource

Discussion in 'Localization Tools' started by noio, Sep 6, 2022.

  1. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    I'm trying to connect the Input System to the Localization system (to show input prompts), elaborating
    @karl_jones example from: https://forum.unity.com/threads/localisation-for-input-keys.1274087/


    I've made some headway with this, but currently I'm stuck on how to process the
    IVariableValueChanged.ValueChanged
    event within my custom
    ISource
    . I am detecting when the input device changes and firing the event on my
    IVariable
    s, but there are no subscribers to that event. Is it because of my custom implementation of
    ISource
    ?

    Even though I'm adding the
    IVariable
    to the
    FormatCache.VariableTriggers
    .



    I guess I don't understand the localization framework good enough to know how my code ties into
    LocalizedString.UpdateVariableListeners
    .

    Should I not be building a custom
    ISource
    at all? In that case, should I somehow extend
    PersistentVariablesSource
    ?

    Here's a gist with the essential files (essentially an elaboration of the code you posted above)

    https://gist.github.com/noio/5209550ebcb049da763f521027ec50f5
     
    Last edited: Sep 6, 2022
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,232
    The system only subscribes to variables that it is using in the Smart String. So adding it to the local variables is not enough, you need to actually use the source in the smart string.

    I think this may be simpler if you implemented it as a variable group instead of creating a new ISource.
    I would use IVariableGroup and then add that as a group to the GlobalVariables source.

    https://docs.unity3d.com/Packages/c...-Variables-Source.html#custom-variable-groups

    You should be able to reuse most of your code. Just make a few tweaks.
     
    Last edited: Sep 6, 2022
    noio likes this.
  3. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,232
    Yes, I think this will keep things a bit simpler. You will only need 1 class instead of 3. Depending on how you want to update you can then just have a single IVariableValueChanged on the group which will trigger an update for any input changes, or you could return a nested IVariableValueChanged from the group which could then be used to trigger updates for that input only.
     
    noio likes this.
  5. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    "You will only need 1 class instead of 3." Yeah! That makes a lot of sense. My ISource wasn't doing much except duplicating functionality anyway.

    I feel dumb ... how do I "use IVariableGroup and then add that as a group to the GlobalVariables source.".

    PersistentVariablesSource takes a "VariableGroupAsset" and what I have is just an IVariableGroup (in a ScriptableObject Asset).





    [EDIT: Okay I think I got it. Haha posted simultaneously.]

    I think I got it? The custom IVariableGroup should not be an Asset. (ScriptableObject).

    Instead, it has to be a [Serializable] Class that also implements IVariable. Then it can be added inside the Variables Group Asset as a nested Variable (Group) ?

     
    Last edited: Sep 6, 2022
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,232
    Arghh looks like you need to inherit from VariablesGroupAsset however the methods are not virtual so you wont be able to implement the behavior. :(

    Ok, instead add the variable group to your global variables asset. It will mean you need to add an extra value to the smart strings though.
    e.g {global.input.left}
    Ill look into making those methods virtual in the future.
     
    noio likes this.
  7. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    Alright, I got it working! (with the caveat of having to add {global}, but eh)

    <3

    Thanks so much for the help!

    Love the way the Input System and Localisation system are built, they're extensible but also they feel very at home inside of Unity!

    I might post it up after a bit if cleaning. (Especially regarding the way the BindingDisplayString is resolved to a sprite, because currently I have some issues with Keyboard "X" vs Gamepad "X". I guess I should use the control Path instead of the BindingDisplayString. )
     
    karl_jones likes this.
  8. Dennooo

    Dennooo

    Joined:
    May 12, 2015
    Posts:
    88
    I am currently facing a similar problem.

    I want to represent the bindings of the InputActions in the localization system. However, I can't quite follow the described explanations.

    What would be the best practice to include the InputActions in the localization system in a newer version of the localization package as either local or global variables?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,232
    Described in this thread is using the persistent variables system with a custom variable to handle input
    https://docs.unity3d.com/Packages/c...manual/Smart/Persistent-Variables-Source.html
    So create a custom IVariable and inside of GetSourceValue you convert the input name into a localized version.
     
    Dennooo likes this.
  10. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,232