Autonumber messing up? (KeyTrigger invasion has begun!)
Posted: Sat Mar 09, 2024 4:36 pm
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:
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
The workaround I've found is to delete all KeyTriggers (with GetComponents()) each time:
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
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);
}
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
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);
I just wanted to let you know about this problem.
Bye!
Marc