Page 1 of 1

Autonumber messing up? (KeyTrigger invasion has begun!)

Posted: Sat Mar 09, 2024 4:36 pm
by _marc
Hi Tony!

I'm experiencing strange behavior with the autonumber feature for the response menu of the dialogue system. At some point in the dialogue the keys select the wrong responses. I wonder if there is not a problem in the code:

Code: Select all

                    button.text = string.Format(m_processedAutonumberFormat, buttonNumber + 1, button.text);
                    var keyTrigger = button.GetComponent<UIButtonKeyTrigger>();
                    if (autonumber.regularNumberHotkeys)
                    {
                        if (keyTrigger == null) keyTrigger = button.gameObject.AddComponent<UIButtonKeyTrigger>();
                        keyTrigger.key = (KeyCode)((int)KeyCode.Alpha1 + buttonNumber);
                    }
                    if (autonumber.numpadHotkeys)
                    {
                        if (autonumber.regularNumberHotkeys || keyTrigger == null) keyTrigger = button.gameObject.AddComponent<UIButtonKeyTrigger>();
                        keyTrigger.key = (KeyCode)((int)KeyCode.Keypad1 + buttonNumber);
                    }
My understanding of the problem:
I'm using numpad hotkeys & regularNumberHotkeys.
So, 2 KeyTrigger components are added per button on the first response menu display,
Then, on the next response menu, the response buttons are reused from the pool and a 3rd KeyTrigger component is added (because autonumber.numpadHotkeys is true and autonumber.regularNumberHotkeys is also true). At this point, the key binding still works. But after several response menu displays, there are a lot of KeyTriggers components per button, and some of the components coming from the pool don't have their key updated resulting in a big mess :-)
autonumber.png
autonumber.png (49.82 KiB) Viewed 119 times
The workaround I've found is to delete all KeyTriggers (with GetComponents()) each time:

Code: Select all

                    button.text = string.Format(m_processedAutonumberFormat, buttonNumber + 1, button.text);
                    UIButtonKeyTrigger[] keyTriggers = button.GetComponents<UIButtonKeyTrigger>();
                    for (int i = 0; i < keyTriggers.Length; i++)
                    {
                        Destroy(keyTriggers[i]);
                    }
                    if (autonumber.regularNumberHotkeys)
                        button.gameObject.AddComponent<UIButtonKeyTrigger>().key = (KeyCode)((int)KeyCode.Alpha1 + buttonNumber);
                    if (autonumber.numpadHotkeys)
                        button.gameObject.AddComponent<UIButtonKeyTrigger>().key = (KeyCode)((int)KeyCode.Keypad1 + buttonNumber);
It works fine, even if it's less optimized than your initial code. Maybe you'll find a better way to fix it in a later version, I don't know.
I just wanted to let you know about this problem.

Bye!

Marc

Re: Autonumber messing up? (KeyTrigger invasion has begun!)

Posted: Sun Mar 10, 2024 7:55 am
by Tony Li
Hi Marc,

That's a bug! Thanks for pointing it out. New response menu buttons used to be instantiated every time a new menu was shown. Response buttons are now recycled from a pool to be more memory efficient. However, the recycling code isn't checking to see if the reused button has UIButtonKeyTrigger components already. I'll make the change you suggest above.